Skip to content

Commit

Permalink
fix: default mode (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Mar 20, 2023
1 parent 4db5abb commit 5362bd5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 7 additions & 2 deletions a_sync/_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
NEGATIVE_FLAGS = {'asynchronous'}
VIABLE_FLAGS = AFFIRMATIVE_FLAGS | NEGATIVE_FLAGS

"""
A-Sync uses 'flags' to indicate whether objects / fn calls will be sync or async.
You can use any of the provided flags, whichever makes most sense for your use case.
"""

def negate_if_necessary(flag: str, flag_value: bool):
validate_flag_value(flag, flag_value)
if flag in AFFIRMATIVE_FLAGS:
return bool(flag_value)
elif flag in NEGATIVE_FLAGS:
return bool(not flag_value)
from a_sync.exceptions import InvalidFlag
raise InvalidFlag(flag)
raise exceptions.InvalidFlag(flag)

def validate_flag_value(flag: str, flag_value: Any) -> bool:
if not isinstance(flag_value, bool):
Expand Down
10 changes: 4 additions & 6 deletions a_sync/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __a_sync_flag_name__(self) -> str:
def __a_sync_flag_value__(self) -> bool:
flag = self.__a_sync_flag_name__
if not isinstance(flag, str):
raise TypeError(flag, type(flag))
raise exceptions.InvalidFlag(flag)
flag_value = getattr(self, flag)
if not isinstance(flag_value, bool):
raise exceptions.InvalidFlagValue(flag, flag_value)
Expand All @@ -39,11 +39,10 @@ def __a_sync_flag_value__(self) -> bool:
@classmethod # type: ignore [misc]
def __a_sync_default_mode__(cls) -> bool:
flag = cls.__get_a_sync_flag_name_from_signature()
flag_value = cls.__a_sync_flag_default_value_from_signature
flag_value = cls.__a_sync_flag_default_value_from_signature()
return _flags.negate_if_necessary(flag, flag_value) # type: ignore [arg-type]

@classmethod
#@property # TODO debug why making this into a classproperty breaks it
def __get_a_sync_flag_name_from_signature(cls) -> str:
if cls.__name__ == "ASyncGenericBase":
return None
Expand All @@ -56,11 +55,10 @@ def __get_a_sync_flag_name_from_signature(cls) -> str:
return present_flags[0]

@classmethod # type: ignore [misc]
@property
def __a_sync_flag_default_value_from_signature(cls) -> bool:
signature = inspect.signature(cls.__init__)
flag = cls.__get_a_sync_flag_name_from_signature()
flag_value = signature.parameters[flag].default
if flag_value is inspect._empty: # type: ignore [attr-defined]
raise Exception("The implementation for 'cls' uses an arg to specify sync mode, instead of a kwarg. We are unable to proceed. I suppose we can extend the code to accept positional arg flags if necessary")
return _flags.validate_flag_value(flag, flag_value)
raise NotImplementedError("The implementation for 'cls' uses an arg to specify sync mode, instead of a kwarg. We are unable to proceed. I suppose we can extend the code to accept positional arg flags if necessary")
return flag_value
6 changes: 6 additions & 0 deletions a_sync/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
from a_sync._meta import ASyncSingletonMeta

class ASyncGenericSingleton(ASyncGenericBase, metaclass=ASyncSingletonMeta):
"""
Subclass this class if you want Singleton-esque ASync objects.
They work kind of like a typical Singleton would, but you will have two instances instead of one:
- one sync instance
- one async instance
"""
pass

0 comments on commit 5362bd5

Please sign in to comment.