From b025a91e89f38da17ca57bed7fc73db871b5f737 Mon Sep 17 00:00:00 2001 From: Timotej Bernat Date: Mon, 22 Apr 2024 16:22:21 -0600 Subject: [PATCH] Added dynamic lookup dicts of builtin types, removed ArrayLike check --- polymerist/genutils/typetools/categorical.py | 30 ++++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/polymerist/genutils/typetools/categorical.py b/polymerist/genutils/typetools/categorical.py index e47b2ae..ab65901 100644 --- a/polymerist/genutils/typetools/categorical.py +++ b/polymerist/genutils/typetools/categorical.py @@ -1,12 +1,15 @@ '''Type-hinting for Union-bound categories of types''' -from typing import Any, Callable, Literal, Type, Union +from typing import Any, Callable, Container, Iterable, Sequence, Type, TypeAlias, Union from numpy import number as np_number from numpy.typing import ArrayLike +import builtins +from inspect import isclass from .parametric import U +# TYPECHECKING FOR CUSTOM UNION TYPE ALIASES def _union_member_factory(union : U) -> Callable[[Any], bool]: '''Factory for making Uion-membership-checking functions''' def isinunion(var : Any) -> bool: @@ -14,9 +17,24 @@ def isinunion(var : Any) -> bool: return isinstance(var, union.__args__) return isinunion -Numeric = Union[int, float, complex, np_number] -JSONSerializable = Union[str, bool, int, float, tuple, list, dict] +Numeric : TypeAlias = Union[int, float, complex, np_number] +StringLike : TypeAlias = Union[str, bytes, bytearray] +JSONSerializable : TypeAlias = Union[str, bool, int, float, tuple, list, dict] -isnumeric = _union_member_factory(Numeric) -isarraylike = _union_member_factory(ArrayLike) -isjsonserializable = _union_member_factory(JSONSerializable) \ No newline at end of file +_TYPE_UNIONS : tuple[Type] = (Numeric, StringLike, JSONSerializable) +for klass in _TYPE_UNIONS: + globals[f'is{klass.__name__.lower()}'] = _union_member_factory(klass) # register to module-level scope + + +# REGISTRIES OF ALL BUILTIN TYPES WITH GIVEN BASE CLASS BEHAVIOR +BUILTIN_TYPES : dict[str, Type] = {} +_BUILTIN_BASES_TO_CHECK : tuple[Type] = (Exception, Callable, Container, Iterable, Sequence) +for klass in _BUILTIN_BASES_TO_CHECK: + class_reg : dict[str, Type] = {} + for builtin_name in dir(builtins): + builtin_obj = getattr(builtins, builtin_name) + if isclass(builtin_obj): + BUILTIN_TYPES[builtin_obj.__name__] = builtin_obj + if issubclass(builtin_obj, klass): + class_reg[builtin_obj.__name__] = builtin_obj + globals()[f'BUILTIN_{klass.__name__.upper()}S'] = class_reg # register to module-level scope \ No newline at end of file