Skip to content

Commit 3126f3f

Browse files
authored
attrs structure hook factory: generate one-argument hooks (#597)
1 parent b1d580f commit 3126f3f

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

docs/migrations.md

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ The old behavior can be restored by explicitly passing in the old hook fallback
1919
# Or
2020
>>> c = BaseConverter(structure_fallback_factory=lambda _: raise_error)
2121
```
22+
23+
### `cattrs.gen.MappingStructureFn` and `cattrs.gen.DictStructureFn` removal
24+
25+
The internal `cattrs.gen.MappingStructureFn` and `cattrs.gen.DictStructureFn` types were replaced by a more general type, `cattrs.SimpleStructureHook[In, T]`.
26+
If you were using `MappingStructureFn`, use `SimpleStructureHook[Mapping[Any, Any], T]` instead.
27+
If you were using `DictStructureFn`, use `SimpleStructureHook[Mapping[str, Any], T]` instead.

src/cattrs/converters.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import annotations
22

33
from collections import Counter, deque
4-
from collections.abc import Iterable
4+
from collections.abc import Callable, Iterable
55
from collections.abc import Mapping as AbcMapping
66
from collections.abc import MutableMapping as AbcMutableMapping
77
from dataclasses import Field
88
from enum import Enum
99
from inspect import Signature
1010
from inspect import signature as inspect_signature
1111
from pathlib import Path
12-
from typing import Any, Callable, Optional, Tuple, TypeVar, overload
12+
from typing import Any, Optional, Tuple, TypeVar, overload
1313

1414
from attrs import Attribute, resolve_types
1515
from attrs import has as attrs_has
@@ -82,7 +82,6 @@
8282
from .fns import Predicate, identity, raise_error
8383
from .gen import (
8484
AttributeOverride,
85-
DictStructureFn,
8685
HeteroTupleUnstructureFn,
8786
IterableUnstructureFn,
8887
MappingUnstructureFn,
@@ -641,7 +640,9 @@ def _unstructure_union(self, obj: Any) -> Any:
641640

642641
# Python primitives to classes.
643642

644-
def _gen_structure_generic(self, cl: type[T]) -> DictStructureFn[T]:
643+
def _gen_structure_generic(
644+
self, cl: type[T]
645+
) -> SimpleStructureHook[Mapping[str, Any], T]:
645646
"""Create and return a hook for structuring generics."""
646647
return make_dict_structure_fn(
647648
cl, self, _cattrs_prefer_attrib_converters=self._prefer_attrib_converters

src/cattrs/gen/__init__.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

33
import re
4-
from collections.abc import Iterable, Mapping
5-
from typing import TYPE_CHECKING, Any, Callable, Final, Literal, TypeVar
4+
from collections.abc import Callable, Iterable, Mapping
5+
from typing import TYPE_CHECKING, Any, Final, Literal, TypeVar
66

77
from attrs import NOTHING, Attribute, Factory
88
from typing_extensions import NoDefault
@@ -69,7 +69,7 @@ def override(
6969

7070
def make_dict_unstructure_fn_from_attrs(
7171
attrs: list[Attribute],
72-
cl: type,
72+
cl: type[T],
7373
converter: BaseConverter,
7474
typevar_map: dict[str, Any] = {},
7575
_cattrs_omit_if_default: bool = False,
@@ -282,12 +282,9 @@ def make_dict_unstructure_fn(
282282
del already_generating.working_set
283283

284284

285-
DictStructureFn = Callable[[Mapping[str, Any], Any], T]
286-
287-
288285
def make_dict_structure_fn_from_attrs(
289286
attrs: list[Attribute],
290-
cl: type,
287+
cl: type[T],
291288
converter: BaseConverter,
292289
typevar_map: dict[str, Any] = {},
293290
_cattrs_forbid_extra_keys: bool | Literal["from_converter"] = "from_converter",
@@ -299,7 +296,7 @@ def make_dict_structure_fn_from_attrs(
299296
_cattrs_use_alias: bool = False,
300297
_cattrs_include_init_false: bool = False,
301298
**kwargs: AttributeOverride,
302-
) -> DictStructureFn[T]:
299+
) -> SimpleStructureHook[Mapping[str, Any], T]:
303300
"""
304301
Generate a specialized dict structuring function for a list of attributes.
305302
@@ -663,7 +660,7 @@ def make_dict_structure_fn_from_attrs(
663660
globs[k] = v
664661

665662
total_lines = [
666-
f"def {fn_name}(o, _, {internal_arg_line}):",
663+
f"def {fn_name}(o, _=__cl, {internal_arg_line}):",
667664
*lines,
668665
*post_lines,
669666
*instantiation_lines,
@@ -695,7 +692,7 @@ def make_dict_structure_fn(
695692
_cattrs_use_alias: bool = False,
696693
_cattrs_include_init_false: bool = False,
697694
**kwargs: AttributeOverride,
698-
) -> DictStructureFn[T]:
695+
) -> SimpleStructureHook[Mapping[str, Any], T]:
699696
"""
700697
Generate a specialized dict structuring function for an attrs class or
701698
dataclass.

0 commit comments

Comments
 (0)