Skip to content

Commit

Permalink
feat: cdef _kwargs.is_sync_c (#385)
Browse files Browse the repository at this point in the history
* feat: cdef _kwargs.is_sync_c

* feat: cnegate_if_necessary

* fix: broken cimport
  • Loading branch information
BobTheBuidler authored Nov 18, 2024
1 parent e6d2e96 commit 247f226
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions a_sync/a_sync/_flags.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cdef bint cnegate_if_necessary(str flag, object flag_value)
4 changes: 4 additions & 0 deletions a_sync/a_sync/_flags.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def negate_if_necessary(flag: str, flag_value: bool) -> bool:
See Also:
- :func:`validate_flag_value`: Validates that the flag value is a boolean.
"""
return cnegate_if_necessary(flag, flag_value)


cdef bint cnegate_if_necessary(str flag, object flag_value):
cdef bint value = validate_flag_value(flag, flag_value)
if flag in AFFIRMATIVE_FLAGS:
return value
Expand Down
1 change: 1 addition & 0 deletions a_sync/a_sync/_kwargs.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cdef bint is_sync_c(str flag, dict kwargs, bint pop_flag)
14 changes: 8 additions & 6 deletions a_sync/a_sync/_kwargs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ from typing import Optional
from libc.stdint cimport uint8_t

from a_sync import exceptions
from a_sync.a_sync import _flags
from a_sync.a_sync._flags import VIABLE_FLAGS
from a_sync.a_sync._flags cimport cnegate_if_necessary


def get_flag_name(kwargs: dict) -> Optional[str]:
Expand Down Expand Up @@ -36,7 +37,7 @@ def get_flag_name(kwargs: dict) -> Optional[str]:
See Also:
:func:`is_sync`: Determines if the operation should be synchronous based on the flag value.
"""
cdef list present_flags = [flag for flag in _flags.VIABLE_FLAGS if flag in kwargs]
cdef list present_flags = [flag for flag in VIABLE_FLAGS if flag in kwargs]
cdef uint8_t flags_count = len(present_flags)
if flags_count == 0:
return None
Expand Down Expand Up @@ -67,9 +68,10 @@ def is_sync(flag: str, kwargs: dict, pop_flag: bool = False) -> bool:
See Also:
:func:`get_flag_name`: Retrieves the name of the flag present in the kwargs.
"""
cdef object flag_value
return is_sync_c(flag, kwargs, pop_flag)
cdef bint is_sync_c(str flag, dict kwargs, bint pop_flag):
if pop_flag:
flag_value = kwargs.pop(flag)
return cnegate_if_necessary(flag, kwargs.pop(flag))
else:
flag_value = kwargs[flag]
return _flags.negate_if_necessary(flag, flag_value)
return cnegate_if_necessary(flag, kwargs[flag])
10 changes: 6 additions & 4 deletions a_sync/a_sync/abstract.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ from typing import Dict, Any, Tuple

from a_sync import exceptions
from a_sync._typing import *
from a_sync.a_sync import _flags, _kwargs, modifiers
from a_sync.a_sync import _kwargs, modifiers
from a_sync.a_sync cimport _flags
from a_sync.a_sync._kwargs cimport is_sync_c
from a_sync.a_sync._meta import ASyncMeta
from a_sync.exceptions import NoFlagsFound

Expand Down Expand Up @@ -115,7 +117,7 @@ class ASyncABC(metaclass=ASyncMeta):
)

if not cache.is_cached:
cache.value = _flags.negate_if_necessary(
cache.value = _flags.cnegate_if_necessary(
self.__a_sync_flag_name__, self.__a_sync_flag_value__
)
cache.is_cached = True
Expand All @@ -141,7 +143,7 @@ class ASyncABC(metaclass=ASyncMeta):
"""
cdef object flag
if flag := _kwargs.get_flag_name(kwargs):
return _kwargs.is_sync(<str>flag, kwargs, pop_flag=True)
return is_sync_c(<str>flag, kwargs, pop_flag=True)
raise NoFlagsFound("kwargs", kwargs.keys())

@classmethod
Expand All @@ -168,7 +170,7 @@ class ASyncABC(metaclass=ASyncMeta):
cdef object flag
cdef bint sync
if flag := _kwargs.get_flag_name(kwargs):
sync = _kwargs.is_sync(<str>flag, kwargs) # type: ignore [arg-type]
sync = is_sync_c(<str>flag, kwargs, pop_flag=False) # type: ignore [arg-type]
logger.debug(
"kwargs indicate the new instance created with args %s %s is %ssynchronous",
args,
Expand Down
3 changes: 2 additions & 1 deletion a_sync/a_sync/method.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from a_sync import exceptions
from a_sync._typing import *
from a_sync.a_sync import _helpers, _kwargs
from a_sync.a_sync._descriptor import ASyncDescriptor
from a_sync.a_sync._kwargs cimport is_sync_c
from a_sync.a_sync.function import (
ASyncFunction,
ASyncFunctionAsyncDefault,
Expand Down Expand Up @@ -761,7 +762,7 @@ class ASyncBoundMethod(ASyncFunction[P, T], Generic[I, P, T]):
"""
cdef object flag
if flag := _kwargs.get_flag_name(kwargs):
return _kwargs.is_sync(<str>flag, kwargs, pop_flag=True) # type: ignore [arg-type]
return is_sync_c(<str>flag, kwargs, pop_flag=True) # type: ignore [arg-type]
elif self.default:
return self.default == "sync"
elif self.__bound_to_a_sync_instance__:
Expand Down

0 comments on commit 247f226

Please sign in to comment.