-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: import Self from typing_extensions for backward compatiblity
- Loading branch information
Showing
4 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |