Skip to content

Commit

Permalink
attrs structure hook factory: generate one-argument hooks (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche authored Nov 8, 2024
1 parent b1d580f commit 3126f3f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 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.
9 changes: 5 additions & 4 deletions src/cattrs/converters.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations

from collections import Counter, deque
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from collections.abc import Mapping as AbcMapping
from collections.abc import MutableMapping as AbcMutableMapping
from dataclasses import Field
from enum import Enum
from inspect import Signature
from inspect import signature as inspect_signature
from pathlib import Path
from typing import Any, Callable, Optional, Tuple, TypeVar, overload
from typing import Any, Optional, Tuple, TypeVar, overload

from attrs import Attribute, resolve_types
from attrs import has as attrs_has
Expand Down Expand Up @@ -82,7 +82,6 @@
from .fns import Predicate, identity, raise_error
from .gen import (
AttributeOverride,
DictStructureFn,
HeteroTupleUnstructureFn,
IterableUnstructureFn,
MappingUnstructureFn,
Expand Down Expand Up @@ -641,7 +640,9 @@ def _unstructure_union(self, obj: Any) -> Any:

# Python primitives to classes.

def _gen_structure_generic(self, cl: type[T]) -> DictStructureFn[T]:
def _gen_structure_generic(
self, cl: type[T]
) -> SimpleStructureHook[Mapping[str, Any], T]:
"""Create and return a hook for structuring generics."""
return make_dict_structure_fn(
cl, self, _cattrs_prefer_attrib_converters=self._prefer_attrib_converters
Expand Down
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 3126f3f

Please sign in to comment.