24
24
import collections .abc
25
25
import contextlib
26
26
import functools
27
+ import operator
27
28
import re as stdlib_re # Avoid confusion with the re we export.
28
29
import sys
29
30
import types
@@ -486,10 +487,6 @@ def __repr__(self):
486
487
return f'ForwardRef({ self .__forward_arg__ !r} )'
487
488
488
489
489
- def _find_name (mod , name ):
490
- return getattr (sys .modules [mod ], name )
491
-
492
-
493
490
class TypeVar (_Final , _Immutable , _root = True ):
494
491
"""Type variable.
495
492
@@ -535,7 +532,7 @@ def longest(x: A, y: A) -> A:
535
532
"""
536
533
537
534
__slots__ = ('__name__' , '__bound__' , '__constraints__' ,
538
- '__covariant__' , '__contravariant__' , '_def_mod' )
535
+ '__covariant__' , '__contravariant__' )
539
536
540
537
def __init__ (self , name , * constraints , bound = None ,
541
538
covariant = False , contravariant = False ):
@@ -554,7 +551,9 @@ def __init__(self, name, *constraints, bound=None,
554
551
self .__bound__ = _type_check (bound , "Bound must be a type." )
555
552
else :
556
553
self .__bound__ = None
557
- self ._def_mod = sys ._getframe (1 ).f_globals ['__name__' ] # for pickling
554
+ def_mod = sys ._getframe (1 ).f_globals ['__name__' ] # for pickling
555
+ if def_mod != 'typing' :
556
+ self .__module__ = def_mod
558
557
559
558
def __getstate__ (self ):
560
559
return {'name' : self .__name__ ,
@@ -580,7 +579,7 @@ def __repr__(self):
580
579
return prefix + self .__name__
581
580
582
581
def __reduce__ (self ):
583
- return ( _find_name , ( self ._def_mod , self . __name__ ))
582
+ return self .__name__
584
583
585
584
586
585
# Special typing constructs Union, Optional, Generic, Callable and Tuple
@@ -741,7 +740,19 @@ def __subclasscheck__(self, cls):
741
740
def __reduce__ (self ):
742
741
if self ._special :
743
742
return self ._name
744
- return super ().__reduce__ ()
743
+
744
+ if self ._name :
745
+ origin = globals ()[self ._name ]
746
+ else :
747
+ origin = self .__origin__
748
+ if (origin is Callable and
749
+ not (len (self .__args__ ) == 2 and self .__args__ [0 ] is Ellipsis )):
750
+ args = list (self .__args__ [:- 1 ]), self .__args__ [- 1 ]
751
+ else :
752
+ args = tuple (self .__args__ )
753
+ if len (args ) == 1 and not isinstance (args [0 ], tuple ):
754
+ args , = args
755
+ return operator .getitem , (origin , args )
745
756
746
757
747
758
class _VariadicGenericAlias (_GenericAlias , _root = True ):
0 commit comments