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

Error when copying instances of Generic subclasses #498

Closed
izquierdo opened this issue Nov 4, 2017 · 2 comments · Fixed by #502
Closed

Error when copying instances of Generic subclasses #498

izquierdo opened this issue Nov 4, 2017 · 2 comments · Fixed by #502
Assignees

Comments

@izquierdo
Copy link

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.

@ilevkivskyi
Copy link
Member

Indeed, this is fixed by #468 (a.k.a. PEP 560), but the fix will be available only in Python 3.7. Therefore, I think it makes sense to also fix it in the backport (the crash also happens on Python 3.4).

@ilevkivskyi ilevkivskyi self-assigned this Nov 16, 2017
ilevkivskyi added a commit that referenced this issue Nov 25, 2017
piotrmaslanka added a commit to piotrmaslanka/satella that referenced this issue Mar 10, 2020
piotrmaslanka added a commit to piotrmaslanka/satella that referenced this issue Mar 10, 2020
@hynekcer
Copy link

hynekcer commented Apr 14, 2020

A solution that works:

class MyClass(Generic[T]):
    __copy__ = None
    ...

This is safe:

  1. The value __copy__ = None has the same effect for the function copy.copy() like an undefined __copy__ attribute. A possible problem is if it would overshadow a right __copy__ attribute.
  2. The "Generic" type is the last ancestor of MyClass. (The only possible problem would be if it is used as generic mixin for a class with a __copy__ method, but it can be easily solved by a super(...).__copy__())

A general hack can be also

from typing import Generic

if sys.version_info[:2] == (3, 5):
    setattr(Generic, '__copy__', None)

(edited)

It works in some of the first modules of an application before the first copy.

Maybe this is also possible to do in typing_extensions.

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

Successfully merging a pull request may close this issue.

3 participants