Skip to content

Fix the problem with copy and deepcopy; also improve pickling a bit #311

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

Merged
merged 2 commits into from
Oct 29, 2016

Conversation

ilevkivskyi
Copy link
Member

Fixes #306
@gvanrossum Here is the PR that I promised. 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)

@ilevkivskyi
Copy link
Member Author

ilevkivskyi commented Oct 29, 2016

OK, it looks like Python 3.2 behaves the same way as Python 2, so that I added __copy__ also to Python 3 (it is simply ignored be newer versions).

@gvanrossum gvanrossum merged commit 930109d into python:master Oct 29, 2016
@gvanrossum
Copy link
Member

Also merged into CPython.

typing.Dict[T, Any], ClassVar[int], ClassVar[List[T]], Tuple['T', 'T'],
Union['T', int], List['T'], typing.Mapping['T', int]]
for t in things + [Any]:
self.assertEqual(t, copy(t))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ilevkivskyi: Are you sure this works correctly? From documentation:

It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

Test:

class Metaclass(type):
  def __copy__(self):
    return 1

class Foo(metaclass=Metaclass):
  pass

>>> copy.copy(Foo)
<class '__main__.Foo'>
>>> Foo.__copy__()
1
>>> copy.copy(Foo) is Foo
True

I do not think your test is really testing this correctly. Nor copying is really working correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we exactly follow the docs for copy module (classes are treated as immutable, except in Python 3.2 and 2.7, this is why we have __copy__ defined), what is the problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so idea is that copy.copy should return same reference, and __copy__ is here just for compatibility with 3.2 and 2.7 to return something sane? But why is then doing self.__class__(...) and not just returning self? So why is really making a copy? Or you want in 3.2 and 2.7 to be a copy?

Should a test include that t is copy(t) on versions which are not 2.7 and 3.2?

Copy link
Member Author

@ilevkivskyi ilevkivskyi Aug 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why is really making a copy?

IIRC, to match the behaviour of 3.2 for other classes. Or maybe it is something to do with picke in 3.2, I am not sure now TBH.

Should a test include that t is copy(t) on versions which are not 2.7 and 3.2?

This is not something super-important, but if you want to make a PR (with a comment quoting copy docs), then we will add it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: #460

@ilevkivskyi ilevkivskyi deleted the fix-deepcopy branch August 14, 2017 07:34
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 this pull request may close these issues.

3 participants