Skip to content

Commit

Permalink
fix(annots): do not erase __annotations__ classvar (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Sep 2, 2024
1 parent 458b36f commit fd8a65a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion koerce/annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def __new__(
slots: list[str] = list(dct.pop("__slots__", []))
module: str | None = dct.pop("__module__", None)
qualname: str = dct.pop("__qualname__", clsname)
annotations: dict[str, Any] = dct.pop("__annotations__", {})
annotations: dict[str, Any] = dct.get("__annotations__", {})
if module is None:
self_qualname = None
else:
Expand Down
8 changes: 8 additions & 0 deletions koerce/tests/test_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,14 @@ class Between(BetweenSimple):
# assert obj.copy(lower=8) == obj2


def test_annotable_keeps_annotations_classvar():
class MyClass(Annotable):
a: int
b: str

assert MyClass.__annotations__ == {"a": "int", "b": "str"}


def test_annotable_with_bound_typevars_properly_coerce_values():
v = MyValue(1.1, 2.2, 3.3)
assert isinstance(v.integer, MyInt)
Expand Down
2 changes: 1 addition & 1 deletion koerce/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_get_type_boundvars() -> None:


def test_get_type_boundvars_unable_to_deduce() -> None:
msg = "Unable to deduce corresponding type attributes..."
msg = "Unable to deduce corresponding attributes..."
with pytest.raises(ValueError, match=msg):
get_type_boundvars(MyDict[int, str])

Expand Down
4 changes: 3 additions & 1 deletion koerce/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def get_type_boundvars(typ: Any) -> dict[TypeVar, tuple[str, type]]:

if params:
raise ValueError(
f"Unable to deduce corresponding type attributes for the following type variables: {params}"
f"Unable to deduce corresponding attributes for type parameters of {typ}.\n"
f"Missing attributes with typehints for the following type variables: {params}.\n"
f"Available type hints: {hints}"
)

return result
Expand Down

0 comments on commit fd8a65a

Please sign in to comment.