Skip to content

Commit 385067e

Browse files
authored
Fix typealias structuring (#485)
* Fix typealias structuring * Upgrade ruff
1 parent d7f6d6f commit 385067e

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

pdm.lock

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cattrs/_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def is_tuple(type):
262262
if sys.version_info >= (3, 12):
263263
from typing import TypeAliasType
264264

265-
def is_type_alias(type: Any) -> bool: # noqa: F811
265+
def is_type_alias(type: Any) -> bool:
266266
"""Is this a PEP 695 type alias?"""
267267
return isinstance(type, TypeAliasType)
268268

src/cattrs/converters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def _find_type_alias_structure_hook(self, type: Any) -> StructureHook:
486486
if res == self._structure_call:
487487
# we need to replace the type arg of `structure_call`
488488
return lambda v, _, __base=base: self._structure_call(v, __base)
489-
return res
489+
return lambda v, _, __base=base: res(v, __base)
490490

491491
def _structure_final_factory(self, type):
492492
base = get_final_base(type)

tests/test_pep_695.py

+16
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,19 @@ def test_type_aliases_overwrite_base_hooks(converter: BaseConverter):
7272

7373
assert converter.structure(1, my_int) == 11
7474
assert converter.unstructure(100, my_int) == 80
75+
76+
77+
def test_type_alias_with_children(converter: BaseConverter):
78+
"""A type alias that chains to a hook that requires the type parameter works."""
79+
80+
class TestClass:
81+
pass
82+
83+
def structure_testclass(val, type):
84+
assert type is TestClass
85+
return TestClass
86+
87+
converter.register_structure_hook(TestClass, structure_testclass)
88+
89+
type TestAlias = TestClass
90+
assert converter.structure(None, TestAlias) is TestClass

0 commit comments

Comments
 (0)