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
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ The third number is for emergencies when we need to start branches for older rel

Our backwards-compatibility policy can be found [here](https://github.com/python-attrs/cattrs/blob/main/.github/SECURITY.md).

## 25.4.0 (UNRELEASED)

- Fix structuring of nested generic classes with stringified annotations.
([#688](https://github.com/python-attrs/cattrs/pull/688))

## 25.3.0 (2025-10-07)

- **Potentially breaking**: [Abstract sets](https://docs.python.org/3/library/collections.abc.html#collections.abc.Set) are now structured into frozensets.
Expand Down
5 changes: 3 additions & 2 deletions src/cattrs/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,10 +1303,11 @@ def gen_structure_typeddict(self, cl: Any) -> Callable[[dict, Any], dict]:
def gen_structure_attrs_fromdict(
self, cl: type[T]
) -> Callable[[Mapping[str, Any], Any], T]:
attribs = fields(get_origin(cl) or cl if is_generic(cl) else cl)
origin = get_origin(cl)
attribs = fields(origin or cl if is_generic(cl) else cl)
if attrs_has(cl) and any(isinstance(a.type, str) for a in attribs):
# PEP 563 annotations - need to be resolved.
resolve_types(cl)
resolve_types(origin or cl)
attrib_overrides = {
a.name: self.type_overrides[a.type]
for a in attribs
Expand Down
23 changes: 23 additions & 0 deletions tests/test_generics_nested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Tests un/structure of nested generic classes (stringified only)"""

from __future__ import annotations

from typing import Generic, TypeVar

from attrs import define

T = TypeVar("T")


def test_structure_nested_roundtrip(genconverter):
@define(auto_attribs=True)
class Inner:
value: int

@define(auto_attribs=True)
class Container(Generic[T]):
data: T

raw = {"data": {"value": 42}}
structured = genconverter.structure(raw, Container[Inner])
assert genconverter.unstructure(structured, Container[Inner]) == raw