Author andreash
Recipients andreash, gvanrossum, kj
Date 2022-01-13.21:49:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1642110574.72.0.371429646126.issue46369@roundup.psfhosted.org>
In-reply-to
Content
Consider the following: 

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

    class Z:
        pass


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


Neither of the following works.

1)  
    class dummy:
        test: NewT

    get_type_hints(test,None,None)
    
    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.


2) 
    get_type_hints(NewT,None,None)
    # --> 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.
History
Date User Action Args
2022-01-13 21:49:34andreashsetrecipients: + andreash, gvanrossum, kj
2022-01-13 21:49:34andreashsetmessageid: <1642110574.72.0.371429646126.issue46369@roundup.psfhosted.org>
2022-01-13 21:49:34andreashlinkissue46369 messages
2022-01-13 21:49:34andreashcreate