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.

Author gvanrossum
Recipients AlexWaygood, BTaskaya, JelleZijlstra, eric.smith, gvanrossum, joperez, kj, levkivskyi, lukasz.langa, miss-islington, n_rosenstein, sobolevn
Date 2022-01-25.20:52:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1643143924.67.0.651863769052.issue41370@roundup.psfhosted.org>
In-reply-to
Content
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?
History
Date User Action Args
2022-01-25 20:52:04gvanrossumsetrecipients: + gvanrossum, eric.smith, lukasz.langa, levkivskyi, JelleZijlstra, miss-islington, n_rosenstein, BTaskaya, sobolevn, joperez, kj, AlexWaygood
2022-01-25 20:52:04gvanrossumsetmessageid: <1643143924.67.0.651863769052.issue41370@roundup.psfhosted.org>
2022-01-25 20:52:04gvanrossumlinkissue41370 messages
2022-01-25 20:52:04gvanrossumcreate