Guido van Rossum <gu...@python.org> added the comment:

Ah, I see the issue. I stepped through get_type_hints() using pdb, and it does 
have a special case for when it encounters a string: it wraps it in a 
ForwardRef and proceeds from there:

https://github.com/python/cpython/blob/cef0a5458f254c2f8536b928dee25862ca90ffa6/Lib/typing.py#L1806-L1807

But list['N'] isn't a string, so it doesn't trigger this case. If you were to 
use "list[N]" instead, it works:

>>> from typing import get_type_hints
>>> class N:
...   c: "list[N]"
... 
>>> get_type_hints(N)
{'c': list[__main__.N]}
>>>

But I suppose you have a reason you (or your users) don't want to do that.

We could probably add a special case where it checks for types.GenericAlias and 
goes through __args__, replacing strings by ForwardRefs.

But would that be enough? The algorithm would have to recursively dive into 
__args__ to see if there's a string hidden deep inside, e.g. list[tuple[int, 
list["N"]]].

And what if the user writes something hybrid, like List[list["N"]]? What other 
cases would we need to cover?

And can we sell this as a bugfix for 3.10, or will this be a new feature in 
3.11?

How will it interact with from __future__ import annotations?

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41370>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to