From 17e8b01dd3ea8d642c62d8053586fb94f0947c78 Mon Sep 17 00:00:00 2001 From: ken-morel Date: Sun, 9 Jun 2024 07:55:35 +0100 Subject: [PATCH] increasing coverage --- src/pyoload/__init__.py | 2 +- src/tests/logs.yaml | 14 +++++++------- src/tests/test_annotate.py | 8 ++++++++ src/tests/test_check.py | 21 ++++++++++++++++++--- src/tests/test_speed.py | 1 - 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/pyoload/__init__.py b/src/pyoload/__init__.py index f3da5c8..90acee4 100644 --- a/src/pyoload/__init__.py +++ b/src/pyoload/__init__.py @@ -607,7 +607,7 @@ def type_match(val: Any, spec: Union[Type, PyoloadAnnotation]) -> tuple: if len(args) == 2: kt, vt = args elif len(args) == 1: - kt, vt = Any, args[1] + kt, vt = args[0], Any else: return (True, None) diff --git a/src/tests/logs.yaml b/src/tests/logs.yaml index 2343356..41326ed 100644 --- a/src/tests/logs.yaml +++ b/src/tests/logs.yaml @@ -1,7 +1,7 @@ -type_match vs isinstance on int:True 1.1989630999999998ms -type_match vs isinstance on int:False 1.5850945ms -type_match on dict[str, int]*50:True 274.8791448ms -type_match on dict[str, int]*50:False 17.1258055ms -Cast str->int: 7.047547499999999ms -Cast complex->int | str: 0.26991929ms -Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 8.10111156ms +type_match vs isinstance on int:True 1.6092411ms +type_match vs isinstance on int:False 1.4381614ms +type_match on dict[str, int]*50:True 271.9251959ms +type_match on dict[str, int]*50:False 17.4513811ms +Cast str->int: 7.204096ms +Cast complex->int | str: 0.43346336ms +Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 8.15403988ms diff --git a/src/tests/test_annotate.py b/src/tests/test_annotate.py index c4000b2..14bf4e5 100644 --- a/src/tests/test_annotate.py +++ b/src/tests/test_annotate.py @@ -5,6 +5,7 @@ from pyoload import annotate from pyoload import is_annotable from pyoload import is_annoted +from pyoload import type_match from pyoload import unannotable from pyoload import unannotate @@ -55,6 +56,13 @@ def footy(a: 'Nothing here'): else: raise Exception() + try: + assert type_match({3: None}, dict[str]) == (True, None) + except Exception: + assert type_match({3: None}, dict[int]) == (True, None) + else: + raise Exception() + obj = MyCLass() try: obj.b = 3 diff --git a/src/tests/test_check.py b/src/tests/test_check.py index 165516f..4a01c13 100644 --- a/src/tests/test_check.py +++ b/src/tests/test_check.py @@ -12,7 +12,8 @@ @annotate class foo: - foo = CheckedAttr(len=(3, None)) + foo = CheckedAttr(len=slice(3, None)) + foow = CheckedAttr(len=(3, None)) bar: Checks(ge=3) def __init__(self: Any, bar: Checks(func=bool)) -> Any: @@ -41,6 +42,20 @@ def test_check(): obj = foo(2) obj.bar = 3 + obj.foo = ('1', 2, 3) + try: + obj.foow = None + except Exception: + pass + else: + raise Exception() + try: + obj.foo = ('1', 2) + except Check.CheckError: + pass + else: + raise Exception() + try: obj.bar = 2.9 except AnnotationError: @@ -107,10 +122,10 @@ def test_check(): continue pyoload.Checks(**{name: int})(11) except pyoload.Check.CheckError: - if name in ('func', 'type'): + if name in ('func', 'type', 'isinstance'): raise Exception() else: - if name not in ('func', 'type'): + if name not in ('func', 'type', 'isinstance'): raise Exception(name, check) try: if pyoload.get_name(check).split(".")[0] == "tests": diff --git a/src/tests/test_speed.py b/src/tests/test_speed.py index 9bb7daa..7600181 100644 --- a/src/tests/test_speed.py +++ b/src/tests/test_speed.py @@ -11,7 +11,6 @@ def test_speed(): - return f = open(src / "logs.yaml", "w") speedtype_match(f) speedCast(f)