Skip to content

Commit

Permalink
Update __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed May 4, 2024
1 parent 7772c51 commit 81571d1
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/pyoload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_name(funcOrCls):
return funcOrCls.__module__ + '.' + funcOrCls.__qualname__


class TypeChecker:
class Validator:
def __init__(self, func):
if not callable(func):
raise TypeError(self.__class__.__init__.__qualname__)
Expand All @@ -54,18 +54,32 @@ def __call__(self, val):
return self.func()
except Exception as e:
raise AnnotationError(
f'{type(e)} while using typechecker method: {get_name(self.func)}' +
f'{type(e)} while using validator method: {get_name(self.func)}' +
f'\n{e!s}',
) from e


class Cast:
@staticmethod
def cast(val, type):
if issubclass(type, Union):
type = type.__args__
if isinstance(type, tuple):
for x in type:
try:
return Cast.cast(val, x)
except:
pass
else:
raise CastingError()
return type(val) if not isinstance(val, type) else val
def __init__(self, type):
self.type = type

def __call__(self, val):

try:
return self.type(val)
return Cast.cast(self.type, val)
except Exception as e:
raise CastingError(
f'Exception while casting: {val!r} to {self.type}',
Expand All @@ -75,14 +89,16 @@ def __call__(self, val):
def typeMatch(val, spec):
if spec == Any or spec is None:
return True
if isinstance(val, tuple):
return isinstance(val, tuple)
if isinstance(spec, Values):
return spec(val)
elif isinstance(spec, TypeChecker):
return spec(val)
elif isinstance(spec, GenericAlias):
if not isinstance(val, spec.__origin__):
return False
sub = Union[*spec.__args__]
sub = spec.__args__
for val in val:
if not typeMatch(val, sub):
return False
Expand Down

0 comments on commit 81571d1

Please sign in to comment.