Skip to content

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

@zhukovgreen

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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions