Skip to content

Error when copying instances of Generic subclasses #498

@izquierdo

Description

@izquierdo

Trying to do the following

import copy
from typing import Generic, TypeVar

T = TypeVar('T')

class Test(Generic[T]):
    def __init__(self, t: T) -> None:
        self.t = t

test: Test[int] = Test(5)
copy.copy(test)

produces

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    copy.copy(test)
  File "/usr/lib/python3.6/copy.py", line 88, in copy
    return copier(x)
TypeError: __copy__() takes 1 positional argument but 2 were given

whereas there is no error if not inheriting from Generic.

The most similar issue to this one is #458 but that snippet is trying to call __copy__ directly instead of copy.copy. There is also #306 but that one is trying to copy the type instead of an instance, and also switching the above snippet to copy.deepcopy does work. Finally, there is #468, it's possible that a fix for this issue would fall within its scope.

Metadata

Metadata

Assignees

Labels

No labels
No labels
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions