From 247f2264203e41613533243563c24db9cdc82836 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 18 Nov 2024 02:16:35 -0400 Subject: [PATCH] feat: cdef _kwargs.is_sync_c (#385) * feat: cdef _kwargs.is_sync_c * feat: cnegate_if_necessary * fix: broken cimport --- a_sync/a_sync/_flags.pxd | 1 + a_sync/a_sync/_flags.pyx | 4 ++++ a_sync/a_sync/_kwargs.pxd | 1 + a_sync/a_sync/_kwargs.pyx | 14 ++++++++------ a_sync/a_sync/abstract.pyx | 10 ++++++---- a_sync/a_sync/method.pyx | 3 ++- 6 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 a_sync/a_sync/_flags.pxd create mode 100644 a_sync/a_sync/_kwargs.pxd diff --git a/a_sync/a_sync/_flags.pxd b/a_sync/a_sync/_flags.pxd new file mode 100644 index 00000000..5913e49e --- /dev/null +++ b/a_sync/a_sync/_flags.pxd @@ -0,0 +1 @@ +cdef bint cnegate_if_necessary(str flag, object flag_value) \ No newline at end of file diff --git a/a_sync/a_sync/_flags.pyx b/a_sync/a_sync/_flags.pyx index f24af900..fa57ee29 100644 --- a/a_sync/a_sync/_flags.pyx +++ b/a_sync/a_sync/_flags.pyx @@ -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 diff --git a/a_sync/a_sync/_kwargs.pxd b/a_sync/a_sync/_kwargs.pxd new file mode 100644 index 00000000..073c0d4f --- /dev/null +++ b/a_sync/a_sync/_kwargs.pxd @@ -0,0 +1 @@ +cdef bint is_sync_c(str flag, dict kwargs, bint pop_flag) \ No newline at end of file diff --git a/a_sync/a_sync/_kwargs.pyx b/a_sync/a_sync/_kwargs.pyx index 1e46f8ba..79783446 100644 --- a/a_sync/a_sync/_kwargs.pyx +++ b/a_sync/a_sync/_kwargs.pyx @@ -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]: @@ -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 @@ -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) \ No newline at end of file + return cnegate_if_necessary(flag, kwargs[flag]) \ No newline at end of file diff --git a/a_sync/a_sync/abstract.pyx b/a_sync/a_sync/abstract.pyx index e9220060..55893c09 100644 --- a/a_sync/a_sync/abstract.pyx +++ b/a_sync/a_sync/abstract.pyx @@ -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 @@ -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 @@ -141,7 +143,7 @@ class ASyncABC(metaclass=ASyncMeta): """ cdef object flag if flag := _kwargs.get_flag_name(kwargs): - return _kwargs.is_sync(flag, kwargs, pop_flag=True) + return is_sync_c(flag, kwargs, pop_flag=True) raise NoFlagsFound("kwargs", kwargs.keys()) @classmethod @@ -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(flag, kwargs) # type: ignore [arg-type] + sync = is_sync_c(flag, kwargs, pop_flag=False) # type: ignore [arg-type] logger.debug( "kwargs indicate the new instance created with args %s %s is %ssynchronous", args, diff --git a/a_sync/a_sync/method.pyx b/a_sync/a_sync/method.pyx index 2b217b4a..9631ed81 100644 --- a/a_sync/a_sync/method.pyx +++ b/a_sync/a_sync/method.pyx @@ -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, @@ -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(flag, kwargs, pop_flag=True) # type: ignore [arg-type] + return is_sync_c(flag, kwargs, pop_flag=True) # type: ignore [arg-type] elif self.default: return self.default == "sync" elif self.__bound_to_a_sync_instance__: