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
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ History
* Fix structuring bare ``typing.Tuple`` on Pythons lower than 3.9.
(`#218 https://github.com/python-attrs/cattrs/issues/218`_)

* Fix a wrong ``AttributeError`` of an missing ``__parameters__`` attribute. This could happen
when inheriting certain generic classes – for example ``typing.*`` classes are affected.
(`#217 <https://github.com/python-attrs/cattrs/issues/217>`_)

1.10.0 (2022-01-04)
-------------------
Expand Down
12 changes: 11 additions & 1 deletion src/cattrs/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,17 @@ def _generate_mapping(
cl: Type, old_mapping: Dict[str, type]
) -> Dict[str, type]:
mapping = {}
for p, t in zip(get_origin(cl).__parameters__, get_args(cl)):

# 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 old_mapping

for p, t in zip(parameters, get_args(cl)):
if isinstance(t, TypeVar):
continue
mapping[p.__name__] = t
Expand Down
61 changes: 61 additions & 0 deletions tests/test_converter_inheritance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import collections
import typing
from typing import Type

import attr
Expand Down Expand Up @@ -43,3 +45,62 @@ class B(A):

# This should still work, but using the new hook instead.
assert converter.structure({"i": 1}, B) == B(2)


@pytest.mark.parametrize("converter_cls", [Converter, GenConverter])
@pytest.mark.parametrize(
"typing_cls", [typing.Hashable, typing.Iterable, typing.Reversible]
)
def test_inherit_typing(converter_cls: Type[Converter], typing_cls):
"""Stuff from typing.* resolves to runtime to collections.abc.*.

Hence, typing.* are of a special alias type which we want to check if
cattrs handles them correctly.
"""
converter = converter_cls()

@attr.define
class A(typing_cls):
i: int = 0

def __hash__(self):
return hash(self.i)

def __iter__(self):
return iter([self.i])

def __reversed__(self):
return iter([self.i])

assert converter.structure({"i": 1}, A) == A(i=1)


@pytest.mark.parametrize("converter_cls", [Converter, GenConverter])
@pytest.mark.parametrize(
"collections_abc_cls",
[
collections.abc.Hashable,
collections.abc.Iterable,
collections.abc.Reversible,
],
)
def test_inherit_collections_abc(
converter_cls: Type[Converter], collections_abc_cls
):
"""As extension of test_inherit_typing, check if collections.abc.* work."""
converter = converter_cls()

@attr.define
class A(collections_abc_cls):
i: int = 0

def __hash__(self):
return hash(self.i)

def __iter__(self):
return iter([self.i])

def __reversed__(self):
return iter([self.i])

assert converter.structure({"i": 1}, A) == A(i=1)