diff --git a/koerce/__init__.py b/koerce/__init__.py index 820de5f..59f0e93 100644 --- a/koerce/__init__.py +++ b/koerce/__init__.py @@ -2,7 +2,7 @@ import sys -from ._internal import * +from ._internal import * # noqa: F403 class _Variable(Deferred): @@ -84,6 +84,8 @@ def koerce( The value to match. context Arbitrary mapping of values to be used while matching. + allow_coercion + Whether to allow coercion of values to match the pattern. Returns ------- diff --git a/koerce/_internal.py b/koerce/_internal.py index 65a4d01..0897ea3 100644 --- a/koerce/_internal.py +++ b/koerce/_internal.py @@ -1,7 +1,7 @@ from __future__ import annotations -from .annots import * -from .builders import * -from .patterns import * +from .annots import * # noqa: F403 +from .builders import * # noqa: F403 +from .patterns import * # noqa: F403 compiled = False diff --git a/koerce/annots.py b/koerce/annots.py index c33993e..092a787 100644 --- a/koerce/annots.py +++ b/koerce/annots.py @@ -740,7 +740,7 @@ def __new__( else: is_initable = initable for parent in bases: - try: # noqa: SIM105 + try: spec = parent.__spec__ except AttributeError: continue diff --git a/koerce/patterns.py b/koerce/patterns.py index 0ae8958..be8eeb4 100644 --- a/koerce/patterns.py +++ b/koerce/patterns.py @@ -803,9 +803,9 @@ def match(self, value, ctx: Context): return True if isinstance(value, str): lowered = value.lower() - if lowered == "true" or lowered == "1": + if lowered in ["true", "1"]: return True - elif lowered == "false" or lowered == "0": + elif lowered in ["false", "0"]: return False raise MatchError(self, value) @@ -2569,6 +2569,10 @@ def pattern( obj The object to create a pattern from. Can be a pattern, a type, a callable, a mapping, an iterable or a value. + allow_coercion + Whether to allow type coercion during matching. + self_qualname + The qualified name of the class that is being constructed. Examples -------- diff --git a/koerce/tests/test_annots.py b/koerce/tests/test_annots.py index 4c3d9ba..528b8ed 100644 --- a/koerce/tests/test_annots.py +++ b/koerce/tests/test_annots.py @@ -9,6 +9,7 @@ Annotated, Any, Callable, + ClassVar, Generic, Mapping, Optional, @@ -996,10 +997,7 @@ def __eq__(self, other) -> bool: return NotImplemented if len(self) != len(other): return False - for a, b in zip(self, other): - if a != b: - return False - return True + return all(a == b for a, b in zip(self, other)) # AnnotableMeta doesn't extend ABCMeta, so we need to register the class @@ -1048,10 +1046,7 @@ def __eq__(self, other) -> bool: return NotImplemented if len(self) != len(other): return False - for key in self: - if self[key] != other[key]: - return False - return True + return all(self[key] == other[key] for key in self) def items(self): for key in self: @@ -2180,7 +2175,7 @@ class User(Annotable): id: int name: str = "Jane Doe" age: int | None = None - children: list[str] = [] + children: ClassVar[list[str]] = [] assert User.__spec__.initable is False assert User.__spec__.immutable is False diff --git a/koerce/tests/test_builders.py b/koerce/tests/test_builders.py index 107bb39..845a671 100644 --- a/koerce/tests/test_builders.py +++ b/koerce/tests/test_builders.py @@ -25,7 +25,6 @@ Var, builder, deferrable, - resolve, ) _ = Deferred(Var("_")) diff --git a/koerce/tests/test_y.py b/koerce/tests/test_y.py index 80804ab..9c0d564 100644 --- a/koerce/tests/test_y.py +++ b/koerce/tests/test_y.py @@ -2,7 +2,7 @@ from dataclasses import dataclass from inspect import Signature as InspectSignature -from typing import Generic +from typing import ClassVar, Generic import pytest @@ -161,21 +161,21 @@ class PUser(BaseModel): id: int name: str = "Jane Doe" age: int | None = None - children: list[str] = [] + children: ClassVar[list[str]] = [] class KUser(Annotable): id: int name: str = "Jane Doe" age: int | None = None - children: list[str] = [] + children: ClassVar[list[str]] = [] class MUser(msgspec.Struct): id: int name: str = "Jane Doe" age: int | None = None - children: list[str] = [] + children: ClassVar[list[str]] = [] data = {"id": 1, "name": "Jane Doe", "age": None, "children": []} diff --git a/koerce/utils.py b/koerce/utils.py index f93f756..8450d77 100644 --- a/koerce/utils.py +++ b/koerce/utils.py @@ -4,7 +4,7 @@ import sys import typing from collections.abc import Hashable, Mapping, Sequence, Set -from typing import Any, ClassVar, ForwardRef, Optional, TypeVar +from typing import Any, ForwardRef, Optional, TypeVar from typing_extensions import Self