Ken Jin <kenjin4...@gmail.com> added the comment:
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"]' ``` ---------- nosy: +gvanrossum, kj, levkivskyi versions: +Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue43646> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com