Skip to content

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

Closed
@ento

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)

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