Skip to content

Commit

Permalink
Fix BaseConverter.register_{un,}structure_hook type signature (#582)
Browse files Browse the repository at this point in the history
Fix `BaseConverter.register_{un,}structure_hook` type signature
  • Loading branch information
Tinche authored Sep 22, 2024
1 parent 255a8f9 commit bf46025
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit bf46025

Please sign in to comment.