Skip to content

include_subclasses fails for classes with generics. AttributeError: __subclasses__. Did you mean: '__subclasscheck__'? #648

Closed

Description

@zhukovgreen

When a parent class with a generics passed to include_subclasses, then:

  1. You need to specify the generic type, otherwise getting
E   cattrs.errors.StructureHandlerNotFoundError: Missing type for generic argument T, specify it when structuring.
  1. 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:

import abc
from typing import Generic, TypeVar

import attrs
import cattrs
from cattrs.strategies import include_subclasses

T = TypeVar("T")


@attrs.define()
class Parent(
    Generic[T],
    metaclass=abc.ABCMeta,
):
    arg: T


class Child(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)


def test_structuring():
    assert c.structure({"arg": 5}, Child) == Child("5")

Activity

  1. zhukovgreen commented on May 21, 2025

    @zhukovgreen
    ContributorAuthor

    I will be happy to work on a fix here, just would like to make sure there is no similar issue/work already ongoing

  2. Tinche commented on May 21, 2025

    @Tinche
    Member

    Good catch. Feel free to try for a fix. Not sure it should work for c.structure({...}, Child) though, shouldn't it be Child[str]?

  3. zhukovgreen commented on May 21, 2025

    @zhukovgreen
    ContributorAuthor

    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:

    @attrs.define()
    class Parent(
        Generic[T],
        metaclass=abc.ABCMeta,
    ):
        arg: T
    
    
    class Child(Parent[str]): ...
  4. added a commit that references this issue on May 22, 2025
    25a31ca
  5. zhukovgreen commented on May 22, 2025

    @zhukovgreen
    ContributorAuthor

    @Tinche pls approve the CI workflow

  6. added a commit that references this issue on May 26, 2025
    d68583a
  7. added a commit that references this issue on Jun 4, 2025
    ea4c311
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions