Skip to content

Subclass missing own class variables inside __init_subclass__ #1086

Description

@aatle

Say a base class has defined __init_subclass__, and a class subclasses the base class. __init_subclass__ gets invoked automatically.
Inside the method, the subclass is missing any class variables declared on the subclass.
Other things such as methods and instance variable descriptors are not missing. The class variable works properly outside of __init_subclass__.

Since the subclass class variable is missing in __init_subclass__, accessing it either gives the class variable of the base class if defined, or an AttributeError if the base class has not defined the class variable.

from typing import ClassVar

class Base:
    name: ClassVar[str] = "base"

    def __init_subclass__(cls, **kwargs):
        print(cls, cls.name)

class Sub(Base):
    name: ClassVar[str] = "sub"
$ mypyc main.py
...

$ python -c "import main"
<class 'module.Sub'> base

Version:
mypy 1.15.0 (compiled: yes)

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions