Skip to content

Commit

Permalink
feat: optimize negate_if_necessary (#401)
Browse files Browse the repository at this point in the history
* feat: optimize negate_if_necessary

* feat: optimize negate_if_necessary
  • Loading branch information
BobTheBuidler authored Nov 20, 2024
1 parent fdc61a3 commit 4c4f85d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion a_sync/a_sync/_flags.pxd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cdef bint negate_if_necessary(str flag, object flag_value)
cdef bint negate_if_necessary(str flag, bint flag_value)
14 changes: 4 additions & 10 deletions a_sync/a_sync/_flags.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from a_sync import exceptions
from a_sync.a_sync.flags import AFFIRMATIVE_FLAGS, NEGATIVE_FLAGS


cdef bint negate_if_necessary(str flag, object flag_value):
cdef bint negate_if_necessary(str flag, bint flag_value):
"""Negate the flag value if necessary based on the flag type.
This function checks if the provided flag is in the set of affirmative or negative flags
Expand All @@ -31,6 +31,7 @@ cdef bint negate_if_necessary(str flag, object flag_value):
The potentially negated flag value.
Raises:
TypeError: If the provided flag value is not of bint type.
exceptions.InvalidFlag: If the flag is not recognized.
Examples:
Expand All @@ -43,12 +44,10 @@ cdef bint negate_if_necessary(str flag, object flag_value):
See Also:
- :func:`validate_flag_value`: Validates that the flag value is a boolean.
"""
if not isinstance(flag_value, bool):
raise exceptions.InvalidFlagValue(flag, flag_value)
if flag in AFFIRMATIVE_FLAGS:
return <bint>flag_value
return flag_value
elif flag in NEGATIVE_FLAGS:
return not <bint>flag_value
return not flag_value
raise exceptions.InvalidFlag(flag)


Expand All @@ -72,11 +71,6 @@ cdef bint validate_flag_value(str flag, object flag_value):
>>> validate_flag_value('sync', True)
True
>>> validate_flag_value('asynchronous', 'yes')
Traceback (most recent call last):
...
exceptions.InvalidFlagValue: Invalid flag value for 'asynchronous': 'yes'
See Also:
- :func:`negate_if_necessary`: Negates the flag value if necessary based on the flag type.
"""
Expand Down
6 changes: 5 additions & 1 deletion a_sync/a_sync/_kwargs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ cdef bint is_sync(str flag, dict kwargs, bint pop_flag):
:func:`get_flag_name`: Retrieves the name of the flag present in the kwargs.
"""
if pop_flag:
# NOTE: we should techincally raise InvalidFlagValue here but I dont want to set flag_value to a var
return negate_if_necessary(flag, kwargs.pop(flag))
else:
return negate_if_necessary(flag, kwargs[flag])
try:
return negate_if_necessary(flag, kwargs[flag])
except TypeError as e:
raise exceptions.InvalidFlagValue(flag, kwargs[flag]) from e.__cause__
27 changes: 21 additions & 6 deletions a_sync/a_sync/abstract.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import abc
import logging
from typing import Dict, Any, Tuple

from a_sync import exceptions
from a_sync._typing import *
from a_sync.a_sync cimport _kwargs
from a_sync.a_sync._flags cimport negate_if_necessary
Expand Down Expand Up @@ -101,9 +102,16 @@ class ASyncABC(metaclass=ASyncMeta):
)

if not cache.is_cached:
cache.value = negate_if_necessary(
self.__a_sync_flag_name__, self.__a_sync_flag_value__
)
try:
cache.value = negate_if_necessary(
self.__a_sync_flag_name__, self.__a_sync_flag_value__
)
except TypeError as e:
raise exceptions.InvalidFlagValue(
self.__a_sync_flag_name__,
self.__a_sync_flag_value__,
) from e.__cause__

cache.is_cached = True
self.__a_sync_should_await_cache__ = cache
return cache.value
Expand Down Expand Up @@ -133,9 +141,16 @@ class ASyncABC(metaclass=ASyncMeta):
)

if not cache.is_cached:
cache.value = negate_if_necessary(
self.__a_sync_flag_name__, self.__a_sync_flag_value__
)
try:
cache.value = negate_if_necessary(
self.__a_sync_flag_name__, self.__a_sync_flag_value__
)
except TypeError as e:
raise exceptions.InvalidFlagValue(
self.__a_sync_flag_name__,
self.__a_sync_flag_value__,
) from e.__cause__

cache.is_cached = True
self.__a_sync_should_await_cache__ = cache
return cache.value
Expand Down
11 changes: 9 additions & 2 deletions a_sync/a_sync/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ class ASyncGenericBase(ASyncABC):
except exceptions.NoFlagsFound:
flag = _get_a_sync_flag_name_from_class_def(cls)
flag_value = _get_a_sync_flag_value_from_class_def(cls, flag)
return negate_if_necessary(flag, flag_value) # type: ignore [arg-type]
try:
return negate_if_necessary(flag, flag_value) # type: ignore [arg-type]
except TypeError as e:
raise exceptions.InvalidFlagValue(flag, flag_value) from e.__cause__

# we need an extra var so we can log it
cdef bint sync
Expand All @@ -130,7 +133,11 @@ class ASyncGenericBase(ASyncABC):
flag = _get_a_sync_flag_name_from_class_def(cls)
flag_value = _get_a_sync_flag_value_from_class_def(cls, flag)

sync = negate_if_necessary(flag, flag_value) # type: ignore [arg-type]
try:
sync = negate_if_necessary(flag, flag_value) # type: ignore [arg-type]
except TypeError as e:
raise exceptions.InvalidFlagValue(flag, flag_value) from e.__cause__

logger._log(
logging.DEBUG,
"`%s.%s` indicates default mode is %ssynchronous",
Expand Down

0 comments on commit 4c4f85d

Please sign in to comment.