python / python/cpython

`copy.copy` ignores methods of the object which are not part of the class

Open
#132,272 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:
from types import MethodType
from copy import copy, deepcopy

class MyClass:
    def __copy__(self):
        print("Call __copy__ of class")
        return self

    def __deepcopy__(self, memo):
        print("Call __deepcopy__ of class")
        return self

obj1 = MyClass()
obj2 = MyClass()

def copy_func(self):
    print("New copy function")
    return self

def deepcopy_func(self, memo):
    print("New deepcopy function")
    return self

obj11 = copy(obj1)
# > Call __copy__ of class
obj12 = deepcopy(obj1)
# > Call __deepcopy__ of class

obj21 = copy(obj2)
# > Call __copy__ of class
obj22 = deepcopy(obj2)
# > Call __deepcopy__ of class

obj1.__copy__ = copy_func.__get__(obj1)
obj1.__deepcopy__ = deepcopy_func.__get__(obj1)

obj2.__copy__ = MethodType(copy_func, obj2)
obj2.__deepcopy__ = MethodType(deepcopy_func, obj2)

obj13 = copy(obj1)
# > Call __copy__ of class
obj14 = deepcopy(obj1)
# > New deepcopy function

obj23 = copy(obj2)
# > Call __copy__ of class
obj24 = deepcopy(obj2)
# > New deepcopy function

Knowing, that binding additional methods to an object after instantiation might not be recommended, the behavior shown above confuses me. My expectation is that copy.copy and copy.deepcopy both use the __copy__/__deepcopy__ method of the actual object passed to it (code). However, copy.copy does not, but rather uses the __copy__ method of the class which could be different from that of the object (code).

Historically, the behavior of copy.copy and copy.deepcopy was first identical (c06e3acc735a6e9cf28d0f511493bcfd8829117d, using the method of the class in both cases), but lateron changed (e690883ccf8081e5baab0e9d71f596f26245b569).

Apart from the differences between the behavior of copy.copy and copy.deepcopy w.r.t. the __copy__/__deepcopy__ methods, the copy.copy method itself takes two different approaches when looking for methods to create the copy: __copy__ is searched inside the class, and in case it is not found, the __reduce_ex__/__reduce__ are looked up in the object (code).

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Lib/copy.py at the copy and reduction lookup paths linked in the report, then run the provided reproducer on the supported Python version. Review the historical commits mentioned to understand why copy.copy and copy.deepcopy diverged. Done means the behavior for instance-bound copy and deepcopy methods is resolved consistently and covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.