Skip to content
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

Can't copy.copy or deepcopy any type that inherits from _FinalTypingBase #306

Closed
ento opened this issue Oct 24, 2016 · 8 comments
Closed
Milestone

Comments

@ento
Copy link

ento commented Oct 24, 2016

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.

annotations = copy.deepcopy(func.__annotations__)
annotations.pop('return', None)
@gvanrossum
Copy link
Member

Hm, there are tests for pickling, I guess they are incomplete. :-( @ilevkivskyi thoughts?

@gvanrossum gvanrossum added the bug label Oct 24, 2016
@gvanrossum gvanrossum added this to the 3.5.3 milestone Oct 24, 2016
@ilevkivskyi
Copy link
Member

ilevkivskyi commented 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 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
Copy link
Member

gvanrossum commented Oct 25, 2016 via email

@ilevkivskyi
Copy link
Member

@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.

@gvanrossum
Copy link
Member

gvanrossum commented Oct 25, 2016

IIRC there are other APIs to override pickling. See the pickle docs, I haven't used them in ages. :-(

@gvanrossum
Copy link
Member

(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().)

@ento ento 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 Oct 26, 2016
@ento
Copy link
Author

ento commented 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.).

gvanrossum pushed a commit that referenced this issue Oct 29, 2016
…311)

Fixes #306

I also improved pickling a bit, but as you could see from tests, the list of things that could be pickled is much shorter than list of those that can be copied/deepcopied (note that copy module in Python 3 treats classes as immutable, and therefore return the class unchanged)
@mitar
Copy link
Contributor

mitar commented Aug 14, 2017

I do not think this was really solved: https://github.com/python/typing/pull/311/files#r132876422

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants