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 2, 2024
1 parent 32616e9 commit ed95007
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/py_overload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class Values(tuple):

def __call__(self, val):
return val in self

def __str__(self):
return 'Values(' + ', '.join(map(repr, self)) + ')'

__repr__ = __str__


def get_name(funcOrCls):
return funcOrCls.__module__+ '.' + funcOrCls.__qualname__
return funcOrCls.__module__ + '.' + funcOrCls.__qualname__


class TypeChecker:
Expand All @@ -54,8 +54,8 @@ 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'\n{e!s}'
f'{type(e)} while using typechecker method: {get_name(self.func)}' +
f'\n{e!s}',
) from e


Expand All @@ -68,7 +68,7 @@ def __call__(self, val):
return self.type(val)
except Exception as e:
raise CastingError(
f'Exception while casting: {val!r} to {self.type}'
f'Exception while casting: {val!r} to {self.type}',
) from e


Expand Down Expand Up @@ -99,7 +99,7 @@ def resolveAnnotations(anno, np, scope=None):
anno[k] = eval(v, np, np)
except Exception as e:
raise AnnotationResolutionError(
f'Exception: {e!s} while resolving annotation {v!r} of {scope}'
f'Exception: {e!s} while resolving annotation {v!r} of {scope}',
) from e


Expand All @@ -117,13 +117,13 @@ def wrapper(*args, **kw):
try:
if func.__defaults__:
for i, v in enumerate(reversed(func.__defaults__)):
vals[names[-1-i]] = v
vals[names[-1 - i]] = v
for i, v in enumerate(args):
vals[names[i]] = v
vals.update(kw)
except IndexError as e:
raise AnnotationError(
f'Was function {get_name(func)} properly annotated?'
f'Was function {get_name(func)} properly annotated?',
) from e

errors = []
Expand All @@ -138,7 +138,7 @@ def wrapper(*args, **kw):
errors.append(
AnnotationError(
f'Value: {v!r} does not match annotation: {anno[k]!r}' +
f' for argument {k!r} of function {get_name(func)}'
f' for argument {k!r} of function {get_name(func)}',
)
)
if len(errors) > 0:
Expand All @@ -148,7 +148,7 @@ def wrapper(*args, **kw):
if "return" in anno:
if not typeMatch(ret, anno["return"]):
raise AnnotationError(
f"return value {ret!r} does not match annotation: {anno['return']} of function {get_name(func)}"
f'return value {ret!r} does not match annotation: {anno['return']} of function {get_name(func)}',
)
return ret
return wrapper
Expand Down Expand Up @@ -177,7 +177,7 @@ def wrapper(*args, **kw):
break
else:
raise OverloadError(
f'No overload of function: {get_name(func)} matches types of arguments'
f'No overload of function: {get_name(func)} matches types of arguments',
)
return val
return wrapper
Expand All @@ -198,8 +198,8 @@ def annotateClass(cls):
annotate(
getattr(
cls,
x
)
x,
),
)
)
def new_setter(self, name, value):
Expand All @@ -210,7 +210,7 @@ def new_setter(self, name, value):
anno[name] = type(value)
if not typeMatch(value, anno[name]):
raise AnnotationError(
f'value {value!r} does not match annotation of attribute: {name!r}:{anno[name]!r} of object of class {get_name(cls)}'
f'value {value!r} does not match annotation of attribute: {name!r}:{anno[name]!r} of object of class {get_name(cls)}',
)
else:
return setter(self, name, value)
Expand Down

0 comments on commit ed95007

Please sign in to comment.