Message389643
Wow, thank you for the excellent bug report! Surprisingly, issue42904 's patch fixes this problem as well. I've spent a few hours trying to debug this, and here's some findings:
TLDR:
3.9.1 - 3.10 added ForwardRef recursive evaluation. A guess is that since get_type_hints has always been passing the wrong locals() for classes as mentioned in issue42904, that may cause nested ForwardRefs to be resolved incorrectly.
Observations:
a.py:
```
class Root:
a: List["Person"]
```
On first call of get_type_hints(Root), _eval_type evals in the following order:
```
# These print the repr, globals['__name__'], locals['__name__'], and code object (only for ForwardRef __forward_code__)
ForwardRef: ForwardRef("List['Person']"), a, None, <code object <module> at 0x0503A7A8, file "<string>", line 1>
GenericAlias: typing.List[ForwardRef('Person')], a, a
ForwardRef: ForwardRef('Person'), a, a, <code object <module> at 0x0503AAF0, file "<string>", line 1>
NA: <class 'a.Person'>
```
Then on a second repeated call of get_type_hints(Root), _eval_type does:
```
ForwardRef: ForwardRef("List['Person']"), a, None, <code object <module> at 0x0503A7A8, file "<string>", line 1>
GenericAlias: typing.List[ForwardRef('Person')], a, a
ForwardRef: ForwardRef('Person'), a, a, <code object <module> at 0x0503AAF0, file "<string>", line 1>
```
It's skipping the last evaluation of <class 'a.Person'>. This also occurs if I call it with RootB after Root.
I managed to reproduce this in 3.9.1-2 as well. The only requirement is to change the example to
```
class Root:
a: 'List["Person"]'
``` |
|
Date |
User |
Action |
Args |
2021-03-28 18:51:20 | kj | set | recipients:
+ kj, gvanrossum, levkivskyi, tefra |
2021-03-28 18:51:20 | kj | set | messageid: <1616957480.84.0.38852437208.issue43646@roundup.psfhosted.org> |
2021-03-28 18:51:20 | kj | link | issue43646 messages |
2021-03-28 18:51:20 | kj | create | |
|