Skip to content
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
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
([#748](https://github.com/python-attrs/cattrs/pull/748))
- The [union passthrough strategy](https://catt.rs/en/stable/strategies.html#union-passthrough) now supports PEP 695 type aliases as union members.
([#753](https://github.com/python-attrs/cattrs/pull/753))
- {meth}`BaseConverter.register_structure_hook_factory` and {meth}`BaseConverter.register_unstructure_hook_factory` now properly return the factory when used as decorators.
([#724](https://github.com/python-attrs/cattrs/pull/724))

## 26.1.0 (2026-02-18)

Expand Down
2 changes: 2 additions & 0 deletions src/cattrs/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ def decorator(factory):
self._unstructure_func.register_func_list(
[(predicate, factory, True)]
)
return factory

return decorator

Expand Down Expand Up @@ -581,6 +582,7 @@ def decorator(factory):
self._structure_func.register_func_list(
[(predicate, factory, True)]
)
return factory

return decorator
self._structure_func.register_func_list(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_typing_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,33 @@ converter = Converter()
value: int = converter.structure("1", int)
bad: str = converter.structure("1", int) # mypy-error: [assignment]
```

## Hook factory decorators preserve factory types

```python
from collections.abc import Callable
from typing import Any

from cattrs import Converter


converter = Converter()


def accepts_int(cl: Any) -> bool:
return cl is int


@converter.register_unstructure_hook_factory(accepts_int)
def unstructure_factory(cl: type[int]) -> Callable[[int], str]:
return str


@converter.register_structure_hook_factory(accepts_int)
def structure_factory(cl: type[int]) -> Callable[[str, type[int]], int]:
return lambda value, _: int(value)


reveal_type(unstructure_factory) # revealed: def (cl: type[int]) -> def (int) -> str
reveal_type(structure_factory) # revealed: def (cl: type[int]) -> def (str, type[int]) -> int
```
Loading