Skip to content

Commit c8608b8

Browse files
committed
Add changelog entry and typing test
Signed-off-by: Tin Tvrtkovic <tinchester@gmail.com>
1 parent 6b0a57c commit c8608b8

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
3030
([#748](https://github.com/python-attrs/cattrs/pull/748))
3131
- The [union passthrough strategy](https://catt.rs/en/stable/strategies.html#union-passthrough) now supports PEP 695 type aliases as union members.
3232
([#753](https://github.com/python-attrs/cattrs/pull/753))
33+
- {meth}`BaseConverter.register_structure_hook_factory` and {meth}`BaseConverter.register_unstructure_hook_factory` now properly return the factory when used as decorators.
34+
([#724](https://github.com/python-attrs/cattrs/pull/724))
3335

3436
## 26.1.0 (2026-02-18)
3537

tests/test_typing_structure.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,33 @@ converter = Converter()
102102
value: int = converter.structure("1", int)
103103
bad: str = converter.structure("1", int) # mypy-error: [assignment]
104104
```
105+
106+
## Hook factory decorators preserve factory types
107+
108+
```python
109+
from collections.abc import Callable
110+
from typing import Any
111+
112+
from cattrs import Converter
113+
114+
115+
converter = Converter()
116+
117+
118+
def accepts_int(cl: Any) -> bool:
119+
return cl is int
120+
121+
122+
@converter.register_unstructure_hook_factory(accepts_int)
123+
def unstructure_factory(cl: type[int]) -> Callable[[int], str]:
124+
return str
125+
126+
127+
@converter.register_structure_hook_factory(accepts_int)
128+
def structure_factory(cl: type[int]) -> Callable[[str, type[int]], int]:
129+
return lambda value, _: int(value)
130+
131+
132+
reveal_type(unstructure_factory) # revealed: def (cl: type[int]) -> def (int) -> str
133+
reveal_type(structure_factory) # revealed: def (cl: type[int]) -> def (str, type[int]) -> int
134+
```

0 commit comments

Comments
 (0)