Skip to content

Commit

Permalink
Fix register_struc_hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jwsloan committed Jun 18, 2024
1 parent 7f1f58c commit a3c67e9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.3

- Redefine register_struc_hook in terms of register_struc_hook_func to make it work with TypedDicts

## v2.0.2

- Fixes regression in post cattrs-1.1.2 where attrs objects in `typing.Any` fields are
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cats.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,19 @@ class Foo:
Foo.struc(dict(union=dict(a="some_value")))
except Exception as exc:
raise AssertionError(f"Exception {repr(exc)} was raised when it should not have been.")




def test_struc_with_typed_dict_registered_hook():
from typing_extensions import TypedDict
class Foo(TypedDict):
bar: int

register_struc_hook(Foo, lambda x, _: x)

@Cat
class Thing:
a: bool

Thing.struc(dict(a=True))
2 changes: 1 addition & 1 deletion typecats/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""typecats"""
__version__ = "2.0.2"
__version__ = "2.0.3"
__author__ = "Peter Gaultney"
__author_email__ = "[email protected]"
4 changes: 2 additions & 2 deletions typecats/tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def get_default_converter():
return _TYPECATS_DEFAULT_CONVERTER


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


def register_unstruc_hook(*args, **kwargs):
Expand Down

0 comments on commit a3c67e9

Please sign in to comment.