You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
include_subclasses fails for classes with generics. AttributeError: __subclasses__. Did you mean: '__subclasscheck__'? #648
When a parent class with a generics passed to include_subclasses, then:
You need to specify the generic type, otherwise getting
E cattrs.errors.StructureHandlerNotFoundError: Missing type for generic argument T, specify it when structuring.
If generic type is specified, you get the following issue
__________________________________________ ERROR collecting test_demo_bug.py __________________________________________
test_demo_bug.py:23: in <module>
include_subclasses(Parent[str], c)
.venv/lib/python3.13/site-packages/cattrs/strategies/_subclasses.py:72: in include_subclasses
parent_subclass_tree = tuple(_make_subclasses_tree(cl))
.venv/lib/python3.13/site-packages/cattrs/strategies/_subclasses.py:15: in _make_subclasses_tree
sscl for scl in cl.__subclasses__() for sscl in _make_subclasses_tree(scl)
/Users/.pyenv/versions/3.13.3/lib/python3.13/typing.py:1366: in __getattr__
raise AttributeError(attr)
E AttributeError: __subclasses__. Did you mean: '__subclasscheck__'?
Quick reproducer:
importabcfromtypingimportGeneric, TypeVarimportattrsimportcattrsfromcattrs.strategiesimportinclude_subclassesT=TypeVar("T")
@attrs.define()classParent(
Generic[T],
metaclass=abc.ABCMeta,
):
arg: TclassChild(Parent[str]): ...
c=cattrs.Converter()
# include_subclasses(Parent, c) - this fails with cattrs.errors.StructureHandlerNotFoundError Missing type for generic argument T, specify it when structuring.include_subclasses(Parent[str], c)
deftest_structuring():
assertc.structure({"arg": 5}, Child) ==Child("5")
OK, great. I'll try to fix then. Regarding Child[str] - tried - the same issue. Also I updated the snippet. More typical definition of child is like this, when you have generic parent with not generic child:
Activity
zhukovgreen commentedon May 21, 2025
I will be happy to work on a fix here, just would like to make sure there is no similar issue/work already ongoing
Tinche commentedon May 21, 2025
Good catch. Feel free to try for a fix. Not sure it should work for
c.structure({...}, Child)though, shouldn't it beChild[str]?zhukovgreen commentedon May 21, 2025
OK, great. I'll try to fix then. Regarding Child[str] - tried - the same issue. Also I updated the snippet. More typical definition of child is like this, when you have generic parent with not generic child:
Support generic parents in include_subclasses strategy
zhukovgreen commentedon May 22, 2025
@Tinche pls approve the CI workflow
Support generic parents in include_subclasses strategy
Support generic parents in include_subclasses strategy (python-attrs#650