Skip to content

Metaclasses with type annotated attributes do not add __annotations__ = {} to derived classes. #149208

@matthewwardrop

Description

@matthewwardrop

Bug report

Bug description:

This can cause odd behaviour for subclasses which then propagate their parent's annotations rather than their own.

class MyType(type):
    METACLASS_VAR: int = 1

class A(metaclass=MyType):
    value: str = "HI"

class B(A):
    pass

B.__annotations__  # {"value": str} instead of {}; but if you remove the type annotation in `MyType`, you get {}

This issue only seems to affect versions prior to 3.14

CPython versions tested on:

3.10, 3.13, 3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions