Skip to content

Commit

Permalink
fix: import Self from typing_extensions for backward compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Aug 7, 2024
1 parent 9e51aab commit e029387
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
3 changes: 1 addition & 2 deletions koerce/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion koerce/tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion koerce/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
133 changes: 133 additions & 0 deletions koerce/tests/test_y.py
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

0 comments on commit e029387

Please sign in to comment.