diff --git a/koerce/patterns.py b/koerce/patterns.py index bca61c0..fe4ce58 100644 --- a/koerce/patterns.py +++ b/koerce/patterns.py @@ -11,13 +11,12 @@ ForwardRef, Literal, Optional, - Self, TypeVar, Union, ) import cython -from typing_extensions import GenericMeta, get_original_bases +from typing_extensions import GenericMeta, Self, get_original_bases # TODO(kszucs): would be nice to cimport Signature and Builder from .builders import Builder, Deferred, Variable, builder diff --git a/koerce/tests/test_patterns.py b/koerce/tests/test_patterns.py index b83e702..a7c63fb 100644 --- a/koerce/tests/test_patterns.py +++ b/koerce/tests/test_patterns.py @@ -11,13 +11,13 @@ List, Literal, Optional, - Self, Sequence, TypeVar, Union, ) import pytest +from typing_extensions import Self from koerce.builders import Call, Deferred, Variable from koerce.patterns import ( diff --git a/koerce/tests/test_utils.py b/koerce/tests/test_utils.py index 41f43c8..a09635d 100644 --- a/koerce/tests/test_utils.py +++ b/koerce/tests/test_utils.py @@ -1,9 +1,10 @@ from __future__ import annotations import inspect -from typing import Dict, Generic, List, Optional, Self, TypeVar, Union +from typing import Dict, Generic, List, Optional, TypeVar, Union import pytest +from typing_extensions import Self from koerce.utils import ( get_type_boundvars, diff --git a/koerce/tests/test_y.py b/koerce/tests/test_y.py new file mode 100644 index 0000000..e2f0227 --- /dev/null +++ b/koerce/tests/test_y.py @@ -0,0 +1,133 @@ +from __future__ import annotations + +from dataclasses import dataclass +from inspect import Signature as InspectSignature +from typing import Generic + +import ibis +from coercer import NoMatch, dict_of, instance_of +from pydantic_core import SchemaValidator +from typing_extensions import TypeVar + +from koerce.patterns import InstanceOf, ObjectOf, ObjectOfN, PatternMap +from koerce.utils import Signature + +T = TypeVar("T") +S = TypeVar("S") +U = TypeVar("U") + + +class A(Generic[T, S, U]): + a: int + b: str + + t: T + s: S + + @property + def u(self) -> U: # type: ignore + ... + + +@dataclass +class Person: + name: str + age: int + is_developer: bool = True + has_children: bool = False + + +v = SchemaValidator( + { + "type": "typed-dict", + "fields": { + "name": { + "type": "typed-dict-field", + "schema": { + "type": "str", + }, + }, + "age": { + "type": "typed-dict-field", + "schema": { + "type": "int", + }, + }, + "is_developer": { + "type": "typed-dict-field", + "schema": { + "type": "bool", + }, + }, + "has_children": { + "type": "typed-dict-field", + "schema": { + "type": "bool", + }, + }, + }, + } +) + +p = Person(name="Samuel", age=35, is_developer=True, has_children=False) + +# V = dict_of({"name": str, "age": int, "is_developer": bool}) + +data = {"name": "Samuel", "age": 35, "is_developer": True, "has_children": False} + + +ITS = 10 + + +# def test_pydantic(benchmark): +# r1 = benchmark.pedantic( +# v.validate_python, args=(data,), iterations=ITS, rounds=20000 +# ) +# assert r1 == data + + +# def test_koerce(benchmark): +# pat = PatternMap( +# {"name": str, "age": int, "is_developer": bool, "has_children": bool} +# ) +# r2 = benchmark.pedantic(pat.apply, args=(data, {}), iterations=ITS, rounds=20000) +# assert r2 == data + + +# def test_coercer(benchmark): +# pat = dict_of({"name": str, "age": int, "is_developer": bool, "has_children": bool}) +# r2 = benchmark.pedantic(pat.apply, args=(data, {}), iterations=ITS, rounds=20000) +# assert r2 == data + + +######################################## + + +# def test_instance_of(): +# pattern = instance_of(int) +# assert pattern.apply(1) == 1 +# assert pattern.apply(4.0) is NoMatch + + +def func(x: int, y: str, *args: int, z: float = 3.14, **kwargs) -> float: ... + + +args = (1, "a", 2, 3, 4) +kwargs = dict(z=3.14, w=5, q=6) +expected = {"x": 1, "y": "a", "args": (2, 3, 4), "z": 3.14, "kwargs": {"w": 5, "q": 6}} + + +def test_inspect(benchmark): + sig = InspectSignature.from_callable(func) + r = benchmark.pedantic( + sig.bind, args=args, kwargs=kwargs, iterations=ITS, rounds=20000 + ) + assert r.arguments == expected + + +def test_signature(benchmark): + sig = Signature.from_callable(func) + r = benchmark.pedantic( + sig.bind, args=args, kwargs=kwargs, iterations=ITS, rounds=20000 + ) + assert r == expected