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.1.1 (UNRELEASED)

- Fixed `AttributeError: no attribute '__parameters__'` while structuring attrs classes that inherit from parametrized generic aliases from `collections.abc`.
([#654](https://github.com/python-attrs/cattrs/issues/654) [#655](https://github.com/python-attrs/cattrs/pull/655))

## 25.1.0 (2025-05-31)

- **Potentially breaking**: The converters raise {class}`StructureHandlerNotFoundError` more eagerly (on hook creation, instead of on hook use).
Expand Down
9 changes: 8 additions & 1 deletion src/cattrs/gen/_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ def generate_mapping(cl: type, old_mapping: dict[str, type] = {}) -> dict[str, t
origin = get_origin(cl)

if origin is not None:
parameters = origin.__parameters__
# To handle the cases where classes in the typing module are using
# the GenericAlias structure but aren't a Generic and hence
# end up in this function but do not have an `__parameters__`
# attribute. These classes are interface types, for example
# `typing.Hashable`.
parameters = getattr(get_origin(cl), "__parameters__", None)
if parameters is None:
return dict(old_mapping)

for p, t in zip(parameters, get_args(cl)):
if isinstance(t, TypeVar):
Expand Down
9 changes: 7 additions & 2 deletions tests/test_converter_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class B(A):
assert converter.structure({"i": 1}, B) == B(2)


@pytest.mark.parametrize("typing_cls", [Hashable, Iterable, Reversible])
@pytest.mark.parametrize("typing_cls", [Hashable, Iterable, Reversible, Iterable[int]])
def test_inherit_typing(converter: BaseConverter, typing_cls):
"""Stuff from typing.* resolves to runtime to collections.abc.*.

Expand All @@ -67,7 +67,12 @@ def __reversed__(self):

@pytest.mark.parametrize(
"collections_abc_cls",
[collections.abc.Hashable, collections.abc.Iterable, collections.abc.Reversible],
[
collections.abc.Hashable,
collections.abc.Iterable,
collections.abc.Reversible,
collections.abc.Iterable[int],
],
)
def test_inherit_collections_abc(converter: BaseConverter, collections_abc_cls):
"""As extension of test_inherit_typing, check if collections.abc.* work."""
Expand Down