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 BaseConverter.register_{un,}structure_hook type signature #582

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ The third number is for emergencies when we need to start branches for older rel

Our backwards-compatibility policy can be found [here](https://github.com/python-attrs/cattrs/blob/main/.github/SECURITY.md).

## 24.1.2 (UNRELEASED)

- Fix {meth}`BaseConverter.register_structure_hook` and {meth}`BaseConverter.register_unstructure_hook` type hints.
([#581](https://github.com/python-attrs/cattrs/issues/581) [#582](https://github.com/python-attrs/cattrs/pull/582))

## 24.1.1 (2024-09-11)

- Fix {meth}`BaseConverter.register_structure_hook_factory` and {meth}`BaseConverter.register_unstructure_hook_factory` type hints.
Expand Down
14 changes: 9 additions & 5 deletions src/cattrs/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
bound="HookFactory[StructureHook] | ExtendedStructureHookFactory[Converter]",
)

UnstructureHookT = TypeVar("UnstructureHookT", bound=UnstructureHook)
StructureHookT = TypeVar("StructureHookT", bound=StructureHook)


class UnstructureStrategy(Enum):
"""`attrs` classes unstructuring strategies."""
Expand Down Expand Up @@ -308,7 +311,7 @@ def unstruct_strat(self) -> UnstructureStrategy:
)

@overload
def register_unstructure_hook(self) -> Callable[[UnstructureHook], None]: ...
def register_unstructure_hook(self, cls: UnstructureHookT) -> UnstructureHookT: ...

@overload
def register_unstructure_hook(self, cls: Any, func: UnstructureHook) -> None: ...
Expand All @@ -335,7 +338,7 @@ def register_unstructure_hook(
cls = next(iter(sig.parameters.values())).annotation
self.register_unstructure_hook(cls, func)

return None
return func

if attrs_has(cls):
resolve_types(cls)
Expand Down Expand Up @@ -440,10 +443,10 @@ def get_unstructure_hook(
)

@overload
def register_structure_hook(self) -> Callable[[StructureHook], None]: ...
def register_structure_hook(self, cl: StructureHookT) -> StructureHookT: ...

@overload
def register_structure_hook(self, cl: Any, func: StructuredValue) -> None: ...
def register_structure_hook(self, cl: Any, func: StructureHook) -> None: ...

def register_structure_hook(
self, cl: Any, func: StructureHook | None = None
Expand All @@ -469,7 +472,7 @@ def register_structure_hook(
func = cl
sig = signature(func)
self.register_structure_hook(sig.return_annotation, func)
return
return func

if attrs_has(cl):
resolve_types(cl)
Expand All @@ -481,6 +484,7 @@ def register_structure_hook(
self._structure_func.register_func_list([(lambda t: t is cl, func)])
else:
self._structure_func.register_cls_list([(cl, func)])
return None

def register_structure_hook_func(
self, check_func: Predicate, func: StructureHook
Expand Down