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.

classification
Title: get_type_hints throws for local class reference
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Fabian.M, gvanrossum
Priority: normal Keywords:

Created on 2021-01-04 16:55 by Fabian.M, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg384332 - (view) Author: Fabian.M (Fabian.M) Date: 2021-01-04 16:55
The following code throws with "NameError: name 'Nested' is not defined".

For reference, it works well when moving class definitions out of the local scope.


import typing
T = typing.TypeVar('T')

def test():
    class Nested(typing.Generic[T]):
        pass

    class Test(typing.Generic[T]):
      nested: Nested[T]

    typing.get_type_hints(Test) # this throws

test()
msg384345 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-01-04 19:58
This is a known limitation. To get this to work you have to pass globals() and locals():

    typing.get_type_hints(Test, globals(), locals())
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86990
2021-01-04 19:58:43gvanrossumsetstatus: open -> closed

nosy: + gvanrossum
messages: + msg384345

resolution: wont fix
stage: resolved
2021-01-04 16:55:18Fabian.Mcreate