python / python/cpython

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

Open
#149,208 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-typing
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported MyType, A, and B reproducer on CPython 3.10 and 3.13, comparing it with 3.14. Trace how type-annotated attributes on the metaclass affect the derived class's annotations. Done means subclasses like B receive their own empty annotations dictionary rather than inheriting A's annotations, with behavior verified on Linux.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.