diff --git a/src/cattrs/gen.py b/src/cattrs/gen.py index aedc4abf..db5c74e5 100644 --- a/src/cattrs/gen.py +++ b/src/cattrs/gen.py @@ -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 diff --git a/tests/test_converter_inheritance.py b/tests/test_converter_inheritance.py index 4b694b6e..cb6db5de 100644 --- a/tests/test_converter_inheritance.py +++ b/tests/test_converter_inheritance.py @@ -1,3 +1,5 @@ +import collections +import typing from typing import Type import attr @@ -43,3 +45,58 @@ 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)