Skip to content

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

Merged
gvanrossum merged 2 commits into
python:masterfrom
ilevkivskyi:fix-deepcopy
Oct 29, 2016
Merged

Fix the problem with copy and deepcopy; also improve pickling a bit#311
gvanrossum merged 2 commits into
python:masterfrom
ilevkivskyi:fix-deepcopy

Conversation

@ilevkivskyi

Copy link
Copy Markdown
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

ilevkivskyi commented Oct 29, 2016

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Member

Also merged into CPython.

Comment thread src/test_typing.py
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

@ilevkivskyi ilevkivskyi Aug 14, 2017

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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