Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix register_struc_hook #19

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 additions & 0 deletions tests/test_cats.py
Original file line number Diff line number Diff line change
@@ -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__ = "pgaultney@xoi.io"
4 changes: 2 additions & 2 deletions typecats/tc.py
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +120 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to investigate this further- newer versions of cattrs support typeddicts (23.1.0 and up) - I don't know what implications changing register_struc_hook has but I'd be wary of it



def register_unstruc_hook(*args, **kwargs):