Issue 46807: Wrong class __annotations__ when field name and type are equal

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