Repository navigation
Description
- cattrs version: 1.10.0
- Python version: 3.7
- Operating System: Linux 5.16.1-1 OpenSUSE Thumbleweed
There is an exception AttributeError: type object 'CLASS' has no attribute '__parameters__' when you try to structure an attrs Class which inherts typing.CLASS
Reproduce
Have a look at this example where CLASS is Hashable:
import attr
import typing
import cattrs
import collections
converter = cattrs.GenConverter()
@attr.define
class A(collections.abc.Hashable): # typing.Hashable should be preferred over collections.abc.Hashable
attr1: str = ''
attr2: int = 0
def __hash__(self) -> int:
return hash(self.attr2)
@attr.define
class B(typing.Hashable):
attr1: str = ''
attr2: int = 0
def __hash__(self) -> int:
return hash(self.attr2)
def main() -> None:
converter.structure(dict(attr1='hello', attr2=42), A) # OK
converter.structure(dict(attr1='hello', attr2=42), B) # AttributeError: type object 'Hashable' has no attribute '__parameters__'
if __name__ == '__main__':
main()Output
Traceback (most recent call last):
File "/home/raabf/sl/demo.py", line 40, in <module>
main()
File "/home/raabf/sl/demo.py", line 37, in main
converter.structure(dict(attr1='hello', attr2=42), B) # AttributeError: type object 'Hashable' has no attribute '__parameters__'
File "/home/raabf/.pyenv/versions/sl37/lib/python3.7/site-packages/cattr/converters.py", line 300, in structure
return self._structure_func.dispatch(cl)(obj, cl)
File "/home/raabf/.pyenv/versions/sl37/lib/python3.7/site-packages/cattr/dispatch.py", line 49, in _dispatch
return self._function_dispatch.dispatch(cl)
File "/home/raabf/.pyenv/versions/sl37/lib/python3.7/site-packages/cattr/dispatch.py", line 126, in dispatch
return handler(typ)
File "/home/raabf/.pyenv/versions/sl37/lib/python3.7/site-packages/cattr/converters.py", line 745, in gen_structure_attrs_fromdict
**attrib_overrides,
File "/home/raabf/.pyenv/versions/sl37/lib/python3.7/site-packages/cattr/gen.py", line 224, in make_dict_structure_fn
mapping = _generate_mapping(base, mapping)
File "/home/raabf/.pyenv/versions/sl37/lib/python3.7/site-packages/cattr/gen.py", line 195, in _generate_mapping
for p, t in zip(get_origin(cl).__parameters__, get_args(cl)):
AttributeError: type object 'Hashable' has no attribute '__parameters__'
Note that in the above code snippet I use typing.Hashable just as an example, the same exception occurs if you inherit something other from typing.*, such as typing.Reversable or typing.Iterable.
Offending Part
The offending part in cattrs is:
def make_dict_structure_fn(
cl: Type[T],
converter: "Converter",
_cattrs_forbid_extra_keys: bool = False,
_cattrs_use_linecache: bool = True,
_cattrs_prefer_attrib_converters: bool = False,
**kwargs,
) -> Callable[[Mapping[str, Any]], T]:
"""Generate a specialized dict structuring function for an attrs class."""
mapping = {}
if is_generic(cl):
base = get_origin(cl)
mapping = _generate_mapping(cl, mapping)
cl = base
for base in getattr(cl, "__orig_bases__", ()):
if is_generic(base) and not str(base).startswith("typing.Generic"):
mapping = _generate_mapping(base, mapping)
breakwhich again calls
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)):
if isinstance(t, TypeVar):
continue
mapping[p.__name__] = t
if not mapping:
return old_mapping
return mappingwhere __parameters__ is accessed. During the exception cl is typing.Hashable, which resolves to collections.abc.Hashable by get_origin(cl).
Reason
The use of typing.Hashable is correct. As far as I understand, during static analyisis from tools such as mypy it acts as type checking, during runtime it is resolved to collections.abc.Hashable where the class is implemented.
I personally rely on inheriting from typing.Hashable instead of collections.abc.Hashable, or else mypy would do wrong type checking.
I do not understand what cattrs is doing, so I just can make some guesses:
- The resolving from
typing.Hashabletocollections.abc.Hashableat runtime (i.e.typing.Hashablehascollections.abc.Hashableas its origin) confuses cattrs. - Some check before fails and
_generate_mappingshould have been never called withtyping.Hashable - In general you cannot assume that all origins have the
__parameters__attribute, i.e. usegetattr(get_origin(cl), '__parameters__', ())instead.
Reference for getting parameters
The library typing_inspect has an get_parameters function to get __parameters__. The function is a bit longer and handles some corner cases , I do not know if this helps with problem reported in this issue here or can be utilized here.
Activity
Tinche commentedon Feb 2, 2022
Interesting.
Before I dive deeper into this, since I had a Python 3.11 virtual env activated I tried your code in that and it worked. So this might've gotten fixed in Python at some point? 3.8 seems to also error, 3.9 works.
raabf commentedon Feb 2, 2022
Can confirm, did not expect a different bahaviour on different python versions, since I thought this is something by design.
However, I am not sure what exactly got ‘fixed‘ in python 3.9.
collections.abc.Hashablestill does not have a__parameters__attribute, but the error does not occur any more because_generate_mappingis not called any more. That again is caused becauseis_generic(base)inif is_generic(base) and not str(base).startswith("typing.Generic"):of the functionmake_dict_structure_fnreturnsFalsesince python 3.9.Tinche commentedon Feb 2, 2022
Yeah, this is development using Python runtime types in a nutshell ;) If you look around on the issue tracker, you'll find an issue introduced between 3.9.9 and 3.9.10.
get_argsfromtypingalso has its idiosyncrasies between versions. If we can fix this issue withgetattr(get_origin(cl), '__parameters__', ())I'm very open to merging that in since it's a super minimal change.Fix __parameters__ access in gen._generate_mapping
raabf commentedon Feb 3, 2022
Using
getattr(get_origin(cl), '__parameters__', ())as it makes _generate_mapping a no-op if the__parameters__do not exist. And It seems to fix my current problem. Hence I made #221 .is_generic
Still as a follow up question I must ask if
cattrs._compat.is_generic()is a correct implementation?I compared that with
typing_inspection.is_generic_typeWhich always returns True:So
typing_inspect.is_generic_type()is always True, although there is no special handling betwenn the python versions, butcattrs._compat.is_generic()behaves differently.raabf commentedon Feb 3, 2022
Beside that I thought about it, and I think #221 is correct behaviour, because if a class is generic it must not neccessary have
__parameters__.For example
With
B.__parameters__there is noAttributeErrorlike on accessingtyping.Hashable.__parameters__but the list is still empty and hence_generate_mappingwould be in effect a no-op.Fix __parameters__ access in gen._generate_mapping
Fix __parameters__ access in gen._generate_mapping
Fix __parameters__ access in gen._generate_mapping
Tinche commentedon Feb 8, 2022
Hm can this be closed?
raabf commentedon Feb 8, 2022
Yea thanks, as far as I see now, the MR #221 should handle all cases where a Generic is not complete, i.e. does not have an
__parameters__.chore(deps): Update cattrs requirement from <1.11,>=1.8 to >=1.8,<22.…
chore(deps): Update cattrs requirement from <22.2,>=1.8 to >=1.8,<22.…