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 d470b1d commit a7a019c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/py_overload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def wrapper(*args, **kw):
raise InternalAnnotationError()
errors.append(
AnnotationError(
f'Value: {v!r} does not match annotation: {anno[k]!r} for argument {k!r} of function {get_name(func)}'
f'Value: {v!r} does not match annotation: {anno[k]!r}' +
f' for argument {k!r} of function {get_name(func)}'
)
)
if len(errors) > 0:
Expand All @@ -151,7 +152,7 @@ def overload(func, name=None):
if name is None or not isinstance(name, str):
name = get_name(func)

if not name in __overloads__:
if name not in __overloads__:
__overloads__[name] = []

__overloads__[name].append(annotate(func, True))
Expand All @@ -172,6 +173,7 @@ def wrapper(*args, **kw):
return val
return wrapper


def annotateClass(cls):
if isinstance(cls, bool):
return partial(annotate, recur=cls)
Expand All @@ -195,15 +197,16 @@ def new_setter(self, name, value):
if any(isinstance(x, str) for x in anno.values()):
resolveAnnotations(anno, globals(), get_name(cls))

if not name in anno:
if name not in anno:
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)
cls.__setattr__ = new_setter
return cls


__version__ = '1.0.0'

0 comments on commit a7a019c

Please sign in to comment.