Skip to content

Class __annotations__ has wrong values if class attr has the same name as it's provided type annotation #98876

Closed
@C0oo1D

Description

@C0oo1D

Bug report

class Example1:
    str: str = 'text'
    
Example1.__annotations__
>>> {'str': 'text'}
class Example2:
    attr1: str = 'text'
    int: int = 5
    str: str = 'some str'
    attr2: int = 150
    
Example2.__annotations__
{'attr1': <class 'str'>, 'int': 5, 'str': 'some str', 'attr2': 5}

I understand that using such names for attributes is not the best idea, but still the behavior with rewrites is strange.
It is also strange that I did not found anything similar in existing issues, because I have been observed that behavior for a long time..

Environment

  • CPython versions tested on: 3.8.10, 3.9.13, 3.10.4, 3.11.0
  • OS: Windows 10 x64

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions