Skip to content

Commit

Permalink
attrs structure hook factory: generate one-argument hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Nov 8, 2024
1 parent b1d580f commit ba3221d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ The old behavior can be restored by explicitly passing in the old hook fallback
# Or
>>> c = BaseConverter(structure_fallback_factory=lambda _: raise_error)
```

### `cattrs.gen.MappingStructureFn` and `cattrs.gen.DictStructureFn` removal

The internal `cattrs.gen.MappingStructureFn` and `cattrs.gen.DictStructureFn` types were replaced by a more general type, `cattrs.SimpleStructureHook[In, T]`.
If you were using `MappingStructureFn`, use `SimpleStructureHook[Mapping[Any, Any], T]` instead.
If you were using `DictStructureFn`, use `SimpleStructureHook[Mapping[str, Any], T]` instead.
17 changes: 7 additions & 10 deletions src/cattrs/gen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import re
from collections.abc import Iterable, Mapping
from typing import TYPE_CHECKING, Any, Callable, Final, Literal, TypeVar
from collections.abc import Callable, Iterable, Mapping
from typing import TYPE_CHECKING, Any, Final, Literal, TypeVar

from attrs import NOTHING, Attribute, Factory
from typing_extensions import NoDefault
Expand Down Expand Up @@ -69,7 +69,7 @@ def override(

def make_dict_unstructure_fn_from_attrs(
attrs: list[Attribute],
cl: type,
cl: type[T],
converter: BaseConverter,
typevar_map: dict[str, Any] = {},
_cattrs_omit_if_default: bool = False,
Expand Down Expand Up @@ -282,12 +282,9 @@ def make_dict_unstructure_fn(
del already_generating.working_set


DictStructureFn = Callable[[Mapping[str, Any], Any], T]


def make_dict_structure_fn_from_attrs(
attrs: list[Attribute],
cl: type,
cl: type[T],
converter: BaseConverter,
typevar_map: dict[str, Any] = {},
_cattrs_forbid_extra_keys: bool | Literal["from_converter"] = "from_converter",
Expand All @@ -299,7 +296,7 @@ def make_dict_structure_fn_from_attrs(
_cattrs_use_alias: bool = False,
_cattrs_include_init_false: bool = False,
**kwargs: AttributeOverride,
) -> DictStructureFn[T]:
) -> SimpleStructureHook[Mapping[str, Any], T]:
"""
Generate a specialized dict structuring function for a list of attributes.
Expand Down Expand Up @@ -663,7 +660,7 @@ def make_dict_structure_fn_from_attrs(
globs[k] = v

total_lines = [
f"def {fn_name}(o, _, {internal_arg_line}):",
f"def {fn_name}(o, _=__cl, {internal_arg_line}):",
*lines,
*post_lines,
*instantiation_lines,
Expand Down Expand Up @@ -695,7 +692,7 @@ def make_dict_structure_fn(
_cattrs_use_alias: bool = False,
_cattrs_include_init_false: bool = False,
**kwargs: AttributeOverride,
) -> DictStructureFn[T]:
) -> SimpleStructureHook[Mapping[str, Any], T]:
"""
Generate a specialized dict structuring function for an attrs class or
dataclass.
Expand Down

0 comments on commit ba3221d

Please sign in to comment.