You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't copy.copy or deepcopy any type that inherits from _FinalTypingBase #306
Appears to be since #283, according to git bisect.
To repro:
$ python --version
Python 3.6.0b2
$ python -c 'import typing, copy; copy.copy(typing.Any)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/Cellar/python3/3.6.0b2_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/copy.py", line 106, in copy
return _reconstruct(x, None, *rv)
File "/usr/local/Cellar/python3/3.6.0b2_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/copy.py", line 274, in _reconstruct
y = func(*args)
File "/usr/local/Cellar/python3/3.6.0b2_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/copyreg.py", line 88, in __newobj__
return cls.__new__(cls, *args)
File "/usr/local/Cellar/python3/3.6.0b2_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/typing.py", line 191, in __new__
raise TypeError("Cannot instantiate %r" % cls)
TypeError: Cannot instantiate typing.Any
Motivation for copying a type: deepcopying a function's __annotations__ makes it possible to do manipulations on the annotations to get only the data you want, like type annotations for just the function arguments.
@gvanrossum Yes, this could be fixed simply by adding a trivial __reduce__ to things that are intended to be singletons. I tried this locally and it works with both pickle and deepcopy. I will make a PR for this right after I finish my work on #301#299#298 (it is almost ready, will make PR today or tomorrow).
By the way, I think we could not make subscripted generics like List[int]pickleable, since pickle identifies classes by __name__. Although, subscripted generics are deepcopyable.
@gvanrossum The problem is not with being qualified. X and X[int] have equal names (also qualified names). For some reason pickle hates this: _PickleError: can't pickle typing.X[int]: it's not the same object as typing.X
Even if try to __reduce__ (probably this is the reason why there are no subscripted generics in tests). Do you know how to overcome this limitation?
Anyway, there are no problems with copy, deepcopy; and unsubscripted generics and instances are pickleable, so that the original issue could be fixed.
(Or maybe you'd need to add pickle support to the metaclass? I'm reaching here, out of time, and I'm not sure it's worth it trying to support pickling of List[int]. (The OP just has concerns with deepcopy().)
changed the title [-]Can't `copy.copy` any type that inherits from `_FinalTypingBase`[/-][+]Can't copy.copy or deepcopy any type that inherits from _FinalTypingBase[/+]on Oct 26, 2016
I (== OP) personally won't mind waiting for the right fix if supporting both pickling and copying is an entangled issue, and if it doesn't make sense to support just copying. Currently I can workaround this issue without much impact (don't type-annotate this arg, only copy after doing that, etc.).
Activity
gvanrossum commentedon Oct 24, 2016
Hm, there are tests for pickling, I guess they are incomplete. :-( @ilevkivskyi thoughts?
ilevkivskyi commentedon Oct 24, 2016
@gvanrossum Yes, this could be fixed simply by adding a trivial
__reduce__to things that are intended to be singletons. I tried this locally and it works with bothpickleanddeepcopy. I will make a PR for this right after I finish my work on #301 #299 #298 (it is almost ready, will make PR today or tomorrow).By the way, I think we could not make subscripted generics like
List[int]pickleable, sincepickleidentifies classes by__name__. Although, subscripted generics aredeepcopyable.gvanrossum commentedon Oct 25, 2016
ilevkivskyi commentedon Oct 25, 2016
@gvanrossum The problem is not with being qualified.
XandX[int]have equal names (also qualified names). For some reasonpicklehates this:_PickleError: can't pickle typing.X[int]: it's not the same object as typing.XEven if try to
__reduce__(probably this is the reason why there are no subscripted generics in tests). Do you know how to overcome this limitation?Anyway, there are no problems with
copy,deepcopy; and unsubscripted generics and instances are pickleable, so that the original issue could be fixed.gvanrossum commentedon Oct 25, 2016
IIRC there are other APIs to override pickling. See the pickle docs, I haven't used them in ages. :-(
gvanrossum commentedon Oct 25, 2016
(Or maybe you'd need to add pickle support to the metaclass? I'm reaching here, out of time, and I'm not sure it's worth it trying to support pickling of
List[int]. (The OP just has concerns withdeepcopy().)[-]Can't `copy.copy` any type that inherits from `_FinalTypingBase`[/-][+]Can't copy.copy or deepcopy any type that inherits from _FinalTypingBase[/+]ento commentedon Oct 26, 2016
I (== OP) personally won't mind waiting for the right fix if supporting both pickling and copying is an entangled issue, and if it doesn't make sense to support just copying. Currently I can workaround this issue without much impact (don't type-annotate this arg, only copy after doing that, etc.).
Fix the problem with copy and deepcopy; also improve pickling a bit (#…
mitar commentedon Aug 14, 2017
I do not think this was really solved: https://github.com/python/typing/pull/311/files#r132876422