This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Wrong class __annotations__ when field name and type are equal
Type: behavior Stage: resolved
Components: Parser Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Wrong type when missname dataclass attribute with existing variable name
View: 36363
Assigned To: Nosy List: AlexWaygood, JelleZijlstra, kgubaev, larry, lys.nikolaou, pablogsal, xtreak
Priority: normal Keywords:

Created on 2022-02-20 13:05 by kgubaev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg413586 - (view) Author: Konstantin (kgubaev) Date: 2022-02-20 13:05
In [18]: class Str(str):
    ...:     pass

In [19]: class Class:
    ...:     Str: str
    ...: 
    ...: 
    ...: Class.__annotations__
Out[19]: {'Str': str}

In [20]: class Class:
    ...:     Str: str = ""
    ...: 
    ...: 
    ...: Class.__annotations__
Out[20]: {'Str': str}

In [21]: class Class:
    ...:     Str: Str = ""
    ...: 
    ...: 
    ...: Class.__annotations__      # Wrong!
Out[21]: {'Str': ''}

In [22]: class Class:
    ...:     Str: Str
    ...: 
    ...: 
    ...: Class.__annotations__
Out[22]: {'Str': __main__.Str}

It reproduced all the version which support annotations as part of the core (I tested python 3.6..3.10.2)
msg413590 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2022-02-20 15:09
This looks similar to https://bugs.python.org/issue36363
msg413593 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2022-02-20 15:36
Yeah, it's the same behavior.  In that class, you have defined Str to be "", then you reference Str in the annotation, and its value is "".  Whatever you set Str to in the example in [21], that's the value of the annotation.

GvR closed the previous report as "not a bug", so I'm gonna do that here too.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90963
2022-02-20 15:36:40larrysetmessages: + msg413593
2022-02-20 15:19:08serhiy.storchakasetstatus: open -> closed
superseder: Wrong type when missname dataclass attribute with existing variable name
resolution: duplicate
stage: resolved
2022-02-20 15:09:50xtreaksetnosy: + xtreak
messages: + msg413590
2022-02-20 13:14:49AlexWaygoodsetnosy: + JelleZijlstra
2022-02-20 13:10:21AlexWaygoodsetnosy: + larry, AlexWaygood
2022-02-20 13:05:31kgubaevcreate