Author andreash
Recipients andreash, gvanrossum, kj
Date 2022-01-13.21:49:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Consider the following: 

    NewT = typing.NewType("NewT", typing.List[typing.Optional['Z']] )

    class Z:

Now get_type_hints() does not resolve the ForwardRef within NewType (but it does so for TypedDict, dataclasses, NamedTuple).

Neither of the following works.

    class dummy:
        test: NewT

    print( NewT.__supertype__.__args__[0].__args__[0]__.__forward_evaluated__ )
    # --> False
Note: investigating the return value of get_type_hints does not change the outcome. 
get_type_hints() patches ForwardRefs in-place.

    # --> TypeError   is not a module, class, method, or function

For Python 3.10+ a workaround exists, but requires access to implementation details of NewType:
   class dummy:
       test: NewT.__supertype__
   get_type_hints( dummy, globalns=sys.modules[NewT.__module__].__dict__, localns=None )

Possible solution could be 
 A) to extent `get_type_hints` to explicitly handle NewType (basically call _eval_type for the __supertype__ member).
    That makes approach 2) work (but not 1)
 or B) to extend _eval_type() to handle process NewType as well. This would make 1) work (but not 2).

I guess, since NewType is supposed to be semantically a subclass of the referred type, 2) is probably the preferred approach, which would suggest A). 

Strictly speaking this issue exits in all Python versions that have NewType, but it is easier to fix in 3.10 because there NewType has the __module__ member.
Date User Action Args
2022-01-13 21:49:34andreashsetrecipients: + andreash, gvanrossum, kj
2022-01-13 21:49:34andreashsetmessageid: <>
2022-01-13 21:49:34andreashlinkissue46369 messages
2022-01-13 21:49:34andreashcreate