diff --git a/src/pyoload/__init__.py b/src/pyoload/__init__.py index 294af50..466787f 100644 --- a/src/pyoload/__init__.py +++ b/src/pyoload/__init__.py @@ -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) @@ -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 diff --git a/src/tests/logs.yaml b/src/tests/logs.yaml index 17bf44e..60bf44a 100644 --- a/src/tests/logs.yaml +++ b/src/tests/logs.yaml @@ -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 diff --git a/src/tests/test_annotate.py b/src/tests/test_annotate.py index c66a62c..4bebe89 100644 --- a/src/tests/test_annotate.py +++ b/src/tests/test_annotate.py @@ -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 @@ -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() diff --git a/src/tests/test_cast.py b/src/tests/test_cast.py index 3040d66..c672c49 100644 --- a/src/tests/test_cast.py +++ b/src/tests/test_cast.py @@ -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" @@ -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() diff --git a/src/tests/test_check.py b/src/tests/test_check.py index 6a348b8..57a0a32 100644 --- a/src/tests/test_check.py +++ b/src/tests/test_check.py @@ -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()