-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-32873: Treat type variables and special typing forms as immutable by copy and pickle #6216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5932a32
277b490
e915739
44321a4
6833335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,8 +285,17 @@ def __init_subclass__(self, *args, **kwds): | |
if '_root' not in kwds: | ||
raise TypeError("Cannot subclass special typing classes") | ||
|
||
class _Immutable: | ||
"""Mixin to indicate that object should not be copied.""" | ||
|
||
class _SpecialForm(_Final, _root=True): | ||
def __copy__(self): | ||
return self | ||
|
||
def __deepcopy__(self, memo): | ||
return self | ||
|
||
|
||
class _SpecialForm(_Final, _Immutable, _root=True): | ||
"""Internal indicator of special typing constructs. | ||
See _doc instance attribute for specific docs. | ||
""" | ||
|
@@ -328,8 +337,8 @@ def __hash__(self): | |
def __repr__(self): | ||
return 'typing.' + self._name | ||
|
||
def __copy__(self): | ||
return self # Special forms are immutable. | ||
def __reduce__(self): | ||
return self._name | ||
|
||
def __call__(self, *args, **kwds): | ||
raise TypeError(f"Cannot instantiate {self!r}") | ||
|
@@ -496,7 +505,11 @@ def __repr__(self): | |
return f'ForwardRef({self.__forward_arg__!r})' | ||
|
||
|
||
class TypeVar(_Final, _root=True): | ||
def _find_name(mod, name): | ||
return getattr(sys.modules[mod], name) | ||
|
||
|
||
class TypeVar(_Final, _Immutable, _root=True): | ||
"""Type variable. | ||
|
||
Usage:: | ||
|
@@ -536,10 +549,12 @@ def longest(x: A, y: A) -> A: | |
T.__covariant__ == False | ||
T.__contravariant__ = False | ||
A.__constraints__ == (str, bytes) | ||
|
||
Note that only type variables defined in global scope can be pickled. | ||
""" | ||
|
||
__slots__ = ('__name__', '__bound__', '__constraints__', | ||
'__covariant__', '__contravariant__') | ||
'__covariant__', '__contravariant__', '_def_mod') | ||
|
||
def __init__(self, name, *constraints, bound=None, | ||
covariant=False, contravariant=False): | ||
|
@@ -558,6 +573,7 @@ def __init__(self, name, *constraints, bound=None, | |
self.__bound__ = _type_check(bound, "Bound must be a type.") | ||
else: | ||
self.__bound__ = None | ||
self._def_mod = sys._getframe(1).f_globals['__name__'] # for pickling | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the type variable is defined in some non-global scope (even inside a class) this won't work. I think that's fine (we need to support those type variables but they don't need to be picklable) but I wonder if this deserves at least a mention in the docstring. I also notice that for the same reason a type variable defined in some non-global scope cannot be copy()'s, but it can be deepcopy()'d. That seems a little weird. |
||
|
||
def __getstate__(self): | ||
return {'name': self.__name__, | ||
|
@@ -582,6 +598,9 @@ def __repr__(self): | |
prefix = '~' | ||
return prefix + self.__name__ | ||
|
||
def __reduce__(self): | ||
return (_find_name, (self._def_mod, self.__name__)) | ||
|
||
|
||
# Special typing constructs Union, Optional, Generic, Callable and Tuple | ||
# use three special attributes for internal bookkeeping of generic types: | ||
|
@@ -724,6 +743,11 @@ def __subclasscheck__(self, cls): | |
raise TypeError("Subscripted generics cannot be used with" | ||
" class and instance checks") | ||
|
||
def __reduce__(self): | ||
if self._special: | ||
return self._name | ||
return super().__reduce__() | ||
|
||
|
||
class _VariadicGenericAlias(_GenericAlias, _root=True): | ||
"""Same as _GenericAlias above but for variadic aliases. Currently, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Treat type variables and special typing forms as immutable by copy and | ||
pickle. This fixes several minor issues and inconsistencies, and improves | ||
backwards compatibility with Python 3.6. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see TPB and TPV mentioned in the for-loop below?