@@ -1374,6 +1374,12 @@ def _no_init(self, *args, **kwargs):
13741374 if type (self )._is_protocol :
13751375 raise TypeError ('Protocols cannot be instantiated' )
13761376
1377+ def _callee (depth = 2 , default = None ):
1378+ try :
1379+ return sys ._getframe (depth ).f_globals ['__name__' ]
1380+ except (AttributeError , ValueError ): # For platforms without _getframe()
1381+ return default
1382+
13771383
13781384def _allow_reckless_class_checks (depth = 3 ):
13791385 """Allow instance and class checks for special stdlib modules.
@@ -2350,7 +2356,7 @@ class body be required.
23502356TypedDict .__mro_entries__ = lambda bases : (_TypedDict ,)
23512357
23522358
2353- def NewType ( name , tp ) :
2359+ class NewType :
23542360 """NewType creates simple unique types with almost zero
23552361 runtime overhead. NewType(name, tp) is considered a subtype of tp
23562362 by static type checkers. At runtime, NewType(name, tp) returns
@@ -2369,12 +2375,23 @@ def name_by_id(user_id: UserId) -> str:
23692375 num = UserId(5) + 1 # type: int
23702376 """
23712377
2372- def new_type (x ):
2378+ def __init__ (self , name , tp ):
2379+ self .__name__ = name
2380+ self .__qualname__ = name
2381+ self .__module__ = _callee (default = 'typing' )
2382+ self .__supertype__ = tp
2383+
2384+ def __repr__ (self ):
2385+ return f'{ self .__module__ } .{ self .__qualname__ } '
2386+
2387+ def __call__ (self , x ):
23732388 return x
23742389
2375- new_type .__name__ = name
2376- new_type .__supertype__ = tp
2377- return new_type
2390+ def __or__ (self , other ):
2391+ return Union [self , other ]
2392+
2393+ def __ror__ (self , other ):
2394+ return Union [other , self ]
23782395
23792396
23802397# Python-version-specific alias (Python 2: unicode; Python 3: str)
0 commit comments