Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed Jun 8, 2024
1 parent baf82cc commit b1d0603
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/pyoload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ def annotate(
:returns: the wrapper function
"""
if not hasattr(func, '__annotations__'):
return func
if isclass(func):
return annotateClass(func)
if len(func.__annotations__) == 0:
Expand Down Expand Up @@ -717,7 +719,7 @@ def wrapper(*pargs, **kw):
if len(errors) > 0:
raise AnnotationErrors(errors)

ret = func(*pargs, **kw)
ret = func(**args.arguments)

if sign.return_annotation is not _empty:
ann = sign.return_annotation
Expand Down Expand Up @@ -748,10 +750,12 @@ def unannotate(func: Callable) -> Callable:
def unannotable(func: Callable) -> Callable:
func = unannotate(func)
func.__pyod_annotable__ = False
return func


def annotable(func: Callable) -> Callable:
func.__pyod_annotable__ = True
return func


def is_annotable(func):
Expand Down
14 changes: 7 additions & 7 deletions src/tests/logs.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type_match vs isinstance on int:True 1.0719632ms
type_match vs isinstance on int:False 1.2783177ms
type_match on dict[str, int]*50:True 278.8694041ms
type_match on dict[str, int]*50:False 17.2757097ms
Cast str->int: 7.049113999999999ms
Cast complex->int | str: 0.29363386ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 8.06158008ms
type_match vs isinstance on int:True 0.9860181ms
type_match vs isinstance on int:False 1.2102531ms
type_match on dict[str, int]*50:True 286.9993945ms
type_match on dict[str, int]*50:False 17.3531196ms
Cast str->int: 7.2130632ms
Cast complex->int | str: 0.27411661ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 8.13545716ms
22 changes: 22 additions & 0 deletions src/tests/test_annotate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pyoload

from pyoload import Cast
from pyoload import annotable
from pyoload import annotate
from pyoload import is_annotable
from pyoload import is_annoted
from pyoload import unannotable
from pyoload import unannotate

assert pyoload.__version__ == "2.0.0"

Expand All @@ -12,8 +18,24 @@ def foo(a, b=3, c: str = "R") -> int:
return 3


def foo1(a: Cast(str)):
return a


def test_annotate():
foo(2)
assert annotate(foo1)(3) == '3'
assert unannotate(annotate(foo1))(3) == 3
assert annotate(unannotable(foo1))(3) == 3
assert annotate(unannotable(foo1))(3) == 3
assert annotate(unannotable(foo1), force=True)(3) == '3'
assert annotate(annotable(unannotable(foo1)))(3) == '3'
assert is_annotable(foo1)
assert is_annotable(annotable(foo1))
assert is_annotable(annotable(unannotable(foo1)))
assert not is_annotable(unannotable(foo1))
assert is_annoted(annotate(foo1, force=True))
assert not is_annoted(foo1)


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions src/tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def test_check():
if pyoload.get_name(check).split('.')[0] == 'tests':
continue
pyoload.Checks(**{name: NotImplemented})(24)
pyoload.Checks(**{name: int})(11)
pyoload.Checks(**{name: 3})(11)
except pyoload.Check.CheckError:
pass
else:
Expand Down

0 comments on commit b1d0603

Please sign in to comment.