This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Should MutableSequence provide .copy()?
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: JelleZijlstra, cheryl.sabella, r.david.murray, rhettinger, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2018-05-15 15:09 by JelleZijlstra, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6965 merged JelleZijlstra, 2018-05-18 04:09
Messages (7)
msg316664 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2018-05-15 15:09
https://docs.python.org/3.7/library/stdtypes.html#mutable-sequence-types lists .copy() among the methods provided by mutable sequences. However, MutableSequence does not actually define .copy(): https://github.com/python/cpython/blob/master/Lib/_collections_abc.py#L945.

Should we add .copy() to the ABC or remove the promise that all mutable sequences implement .copy()?
msg316672 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-15 15:38
The ABCs are different from the standard types provided by python itself.  The former are a minimal subset of the methods provided by the latter.  So I think the answer is neither.
msg316673 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-15 15:40
On the other hand, that section does reference the ABC directly, so I can see the point of your question.  So I guess I'm not sure what the answer is...I don't think copy is an essential part of the ABC, but I'm not sure.
msg316697 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-15 19:07
MutableSequence defines an interface. Adding a new method will break all classes that implemented this protocol but not the new method.

And what should .copy() return? list subclasses return an exact list, bytearray subclasses return an exact bytearray, but deque subclasses try to create an instance of the same type. Not all mutable sequences can be copyable. The constructor is not the part of the protocol.

The same problems are in MutableSet and MutableMapping. This is why they don't provide copy() methods (but concrete classes set and dict do).
msg316744 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-05-15 23:35
> Should we add .copy() to the ABC or remove the promise that all mutable sequences implement .copy()?

The second option would be best.  Let's just clarify that copy() isn't part of the MutableSequence API.

The first option isn't really a choice be it would break existing uses that don't implement copy and because the ABC have a reliable way to create a new instance using the given abstract methods (it has no way of even knowing whether the data is stored locally, in a database, or updated through a REST API, it may not even be possible to reliably create an independent instance).
msg316749 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2018-05-16 00:27
Makes sense. I can provide a patch to the docs.
msg342826 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-05-19 00:18
New changeset 9892f454d11b7ea9ba394a115b3e6f48ef6f78fe by Cheryl Sabella (Jelle Zijlstra) in branch 'master':
bpo-33519: clarify that .copy() is not part of the MutableSequence ABC (GH-6965)
https://github.com/python/cpython/commit/9892f454d11b7ea9ba394a115b3e6f48ef6f78fe
History
Date User Action Args
2022-04-11 14:59:00adminsetgithub: 77700
2019-05-19 00:18:57cheryl.sabellasetstatus: open -> closed
type: enhancement
resolution: fixed
stage: patch review -> resolved
2019-05-19 00:18:08cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg342826
2018-05-18 04:09:52JelleZijlstrasetkeywords: + patch
stage: patch review
pull_requests: + pull_request6623
2018-05-16 00:27:41JelleZijlstrasetmessages: + msg316749
2018-05-15 23:35:15rhettingersetmessages: + msg316744
2018-05-15 19:07:54serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg316697
2018-05-15 15:40:43r.david.murraysetmessages: + msg316673
2018-05-15 15:38:20r.david.murraysetnosy: + r.david.murray
messages: + msg316672
2018-05-15 15:09:27JelleZijlstracreate