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)
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 anAttributeErrorif the base class has not defined the class variable.Version:
mypy 1.15.0 (compiled: yes)