From 5362bd5a47c34fbccb58468c00d19de60686506e Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 20 Mar 2023 17:48:04 -0400 Subject: [PATCH] fix: default mode (#33) --- a_sync/_flags.py | 9 +++++++-- a_sync/base.py | 10 ++++------ a_sync/singleton.py | 6 ++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/a_sync/_flags.py b/a_sync/_flags.py index de5797b9..1f834ff5 100644 --- a/a_sync/_flags.py +++ b/a_sync/_flags.py @@ -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): diff --git a/a_sync/base.py b/a_sync/base.py index 820d2430..faa1aa5f 100644 --- a/a_sync/base.py +++ b/a_sync/base.py @@ -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) @@ -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 @@ -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 diff --git a/a_sync/singleton.py b/a_sync/singleton.py index bb85fa46..56c98828 100644 --- a/a_sync/singleton.py +++ b/a_sync/singleton.py @@ -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