@@ -143,7 +143,6 @@ class _TypingBase(metaclass=TypingMeta, _root=True):
143143
144144 __slots__ = ()
145145
146-
147146 def __init__ (self , * args , ** kwds ):
148147 pass
149148
@@ -158,7 +157,7 @@ def __new__(cls, *args, **kwds):
158157 isinstance (args [1 ], tuple )):
159158 # Close enough.
160159 raise TypeError ("Cannot subclass %r" % cls )
161- return object .__new__ (cls )
160+ return super () .__new__ (cls )
162161
163162 # Things that are not classes also need these.
164163 def _eval_type (self , globalns , localns ):
@@ -177,7 +176,11 @@ def __call__(self, *args, **kwds):
177176
178177
179178class _FinalTypingBase (_TypingBase , _root = True ):
180- """Mix-in class to prevent instantiation."""
179+ """Mix-in class to prevent instantiation.
180+
181+ Prevents instantiation unless _root=True is given in class call.
182+ It is used to create pseudo-singleton instances Any, Union, Tuple, etc.
183+ """
181184
182185 __slots__ = ()
183186
@@ -273,7 +276,7 @@ def __init__(self, name, type_var, impl_type, type_checker):
273276 assert isinstance (name , str ), repr (name )
274277 assert isinstance (impl_type , type ), repr (impl_type )
275278 assert not isinstance (impl_type , TypingMeta ), repr (impl_type )
276- assert isinstance (type_var , (type , _TypingBase ))
279+ assert isinstance (type_var , (type , _TypingBase )), repr ( type_var )
277280 self .name = name
278281 self .type_var = type_var
279282 self .impl_type = impl_type
@@ -375,9 +378,13 @@ def _type_repr(obj):
375378class _Any (_FinalTypingBase , _root = True ):
376379 """Special type indicating an unconstrained type.
377380
378- - Any object is an instance of Any.
379- - Any class is a subclass of Any.
380- - As a special case, Any and object are subclasses of each other.
381+ - Any is compatible with every type.
382+ - Any assumed to have all methods.
383+ - All values assumed to be instances of Any.
384+
385+ Note that all the above statements are true from the point of view of
386+ static type checkers. At runtime, Any should not be used with instance
387+ or class checks.
381388 """
382389
383390 __slots__ = ()
@@ -502,7 +509,7 @@ def inner(*args, **kwds):
502509 try :
503510 return cached (* args , ** kwds )
504511 except TypeError :
505- pass # Do not duplicate real errors .
512+ pass # All real errors ( not unhashable args) are raised below .
506513 return func (* args , ** kwds )
507514 return inner
508515
@@ -542,16 +549,10 @@ class Manager(Employee): pass
542549 Union[Manager, int, Employee] == Union[int, Employee]
543550 Union[Employee, Manager] == Employee
544551
545- - Corollary: if Any is present it is the sole survivor, e.g.::
546-
547- Union[int, Any] == Any
548-
549552 - Similar for object::
550553
551554 Union[int, object] == object
552555
553- - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
554-
555556 - You cannot subclass or instantiate a union.
556557
557558 - You cannot write Union[X][Y] (what would it mean?).
@@ -589,14 +590,11 @@ def __new__(cls, parameters=None, *args, _root=False):
589590 assert not all_params , all_params
590591 # Weed out subclasses.
591592 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
592- # If Any or object is present it will be the sole survivor.
593- # If both Any and object are present, Any wins.
594- # Never discard type variables, except against Any.
593+ # If object is present it will be sole survivor among proper classes.
594+ # Never discard type variables.
595595 # (In particular, Union[str, AnyStr] != AnyStr.)
596596 all_params = set (params )
597597 for t1 in params :
598- if t1 is Any :
599- return Any
600598 if not isinstance (t1 , type ):
601599 continue
602600 if any (isinstance (t2 , type ) and issubclass (t1 , t2 )
@@ -662,7 +660,7 @@ def __subclasscheck__(self, cls):
662660class _Optional (_FinalTypingBase , _root = True ):
663661 """Optional type.
664662
665- Optional[X] is equivalent to Union[X, type( None) ].
663+ Optional[X] is equivalent to Union[X, None].
666664 """
667665
668666 __slots__ = ()
0 commit comments