Skip to content

Commit

Permalink
coverage to 98%!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed Jun 9, 2024
1 parent ddbee5e commit 5bb7380
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/pyoload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,11 @@ def type_match(val: Any, spec: Union[Type, PyoloadAnnotation]) -> tuple:
kt, vt = args
elif len(args) == 1:
kt, vt = args[0], Any
else:
return (True, None)

for k, v in val.items():
k, e = type_match(k, kt)
if not k:
return (k, e)
return (False, e)
v, e = type_match(v, vt)
if not v:
return (False, e)
Expand Down Expand Up @@ -724,8 +722,6 @@ def wrapper(*pargs, **kw):
param = sign.parameters.get(k)
if param.annotation is _empty:
continue
if param.annotation is None:
continue
if isinstance(param.annotation, Cast):
args.arguments[k] = param.annotation(v)
continue
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.8029011ms
type_match vs isinstance on int:False 1.9367615000000002ms
type_match on dict[str, int]*50:True 264.507885ms
type_match on dict[str, int]*50:False 17.7091622ms
Cast str->int: 10.5837726ms
Cast complex->int | str: 0.37535457ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 12.165085790000001ms
type_match vs isinstance on int:True 1.4384856ms
type_match vs isinstance on int:False 2.3996007ms
type_match on dict[str, int]*50:True 264.8990402ms
type_match on dict[str, int]*50:False 17.6305638ms
Cast str->int: 10.889522999999999ms
Cast complex->int | str: 0.5091771ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 11.79795839ms
6 changes: 5 additions & 1 deletion src/tests/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def foo(a, b=3, c: str = "R") -> Cast(int):
return '3'


def foo1(a: Cast(str)):
def foo1(a: Cast(str)) -> None:
return a


Expand Down Expand Up @@ -113,6 +113,10 @@ def fooar(a: 'str', b: 'int'):
else:
raise Exception()

assert type_match(3, dict[str | int])
assert type_match({3: '4'}, dict[int, int])
assert type_match({'3': 4}, dict[int, int])


if __name__ == "__main__":
test_annotate()
11 changes: 9 additions & 2 deletions src/tests/test_cast.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pyoload

from pyoload import AnnotationError
from pyoload import Any
from pyoload import Cast
from pyoload import CastedAttr
from pyoload import CastingError
from pyoload import Union
from pyoload import annotate
from pyoload import type_match
from pyoload import AnnotationError
from pyoload import Union

assert pyoload.__version__ == "2.0.0"

Expand Down Expand Up @@ -47,6 +48,12 @@ def foo2(a: 'Cast(None)'):
else:
raise Exception()

try:
Cast(int | float)(1j)
except CastingError:
pass
else:
raise Exception()

if __name__ == "__main__":
test_cast()
9 changes: 9 additions & 0 deletions src/tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ def test_check():
pyoload.Checks(len=slice(3, None))((1, 2, 3))
pyoload.Checks(len=slice(3))((1, 2))

try:
Checks(type=str)(3)
except Check.CheckError:
pass
try:
Checks(isinstance=str)(3)
except Check.CheckError:
pass


if __name__ == "__main__":
test_check()

0 comments on commit 5bb7380

Please sign in to comment.