Skip to content

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

Closed

Description

@ento

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)

Activity

  1. gvanrossum commented on Oct 24, 2016

    @gvanrossum
    Member

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

  2. added this to the 3.5.3 milestone on Oct 24, 2016
  3. ilevkivskyi commented on Oct 24, 2016

    @ilevkivskyi
    Member

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

  4. gvanrossum commented on Oct 25, 2016

    @gvanrossum
    Member
  5. ilevkivskyi commented on Oct 25, 2016

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

  6. gvanrossum commented on Oct 25, 2016

    @gvanrossum
    Member

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

  7. gvanrossum commented on Oct 25, 2016

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

  8. 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
  9. ento commented on Oct 26, 2016

    @ento
    Author

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

  10. added a commit that references this issue on Oct 29, 2016
    930109d
  11. mitar commented on Aug 14, 2017

    @mitar
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions