Skip to content

Commit 06c155e

Browse files
committed
Fix register_struc_hook
1 parent 7f1f58c commit 06c155e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

tests/test_cats.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ class Foo:
113113
Foo.struc(dict(union=dict(a="some_value")))
114114
except Exception as exc:
115115
raise AssertionError(f"Exception {repr(exc)} was raised when it should not have been.")
116+
117+
118+
119+
120+
def test_struc_with_typed_dict_registered_hook():
121+
from typing_extensions import TypedDict
122+
class Foo(TypedDict):
123+
bar: int
124+
125+
register_struc_hook(Foo, lambda x, _: x)
126+
127+
@Cat
128+
class Thing:
129+
a: bool
130+
131+
Thing.struc(dict(a=True))

typecats/tc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ def get_default_converter():
117117
return _TYPECATS_DEFAULT_CONVERTER
118118

119119

120-
def register_struc_hook(*args, **kwargs):
120+
def register_struc_hook(typ, func, **_):
121121
"""Use this to register cattrs structuring hooks on the internal cattrs Converter"""
122-
_TYPECATS_DEFAULT_CONVERTER.register_structure_hook(*args, **kwargs)
122+
_TYPECATS_DEFAULT_CONVERTER.register_structure_hook_func(lambda t: t == typ, func)
123123

124124

125125
def register_unstruc_hook(*args, **kwargs):

0 commit comments

Comments
 (0)