Skip to content

PEP 649: incorrect __annotations__ when type with same name is created inside class #127701

@randolf-scholz

Description

@randolf-scholz

Bug report

Bug description:

I played around with __annotations__, and noticed that it returns an incorrect result when a type is defined both inside and outside the class scope: [mypy-playground]

class Bar: ...

class Foo:
    x: Bar = Bar()

    class Bar: ...
    y: Bar = Bar()

reveal_type(Foo.x)  # __main__.Bar
reveal_type(Foo.y)  # __main__.Foo.Bar
assert isinstance(Foo.x, Bar)  # ✅
assert isinstance(Foo.y, Foo.Bar)  # ✅

However, Foo.__annotations__ returns {'x': <class '__main__.Foo.Bar'>, 'y': <class '__main__.Foo.Bar'>}

CPython versions tested on:

3.14.0~a2 from deadsnakes PPA

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions