classification
Title: Should MutableSequence provide .copy()?
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jelle Zijlstra, r.david.murray, rhettinger, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2018-05-15 15:09 by Jelle Zijlstra, last changed 2018-05-18 04:09 by Jelle Zijlstra.

Pull Requests
URL Status Linked Edit
PR 6965 open Jelle Zijlstra, 2018-05-18 04:09
Messages (6)
msg316664 - (view) Author: Jelle Zijlstra (Jelle Zijlstra) * 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 (Jelle Zijlstra) * Date: 2018-05-16 00:27
Makes sense. I can provide a patch to the docs.
History
Date User Action Args
2018-05-18 04:09:52Jelle Zijlstrasetkeywords: + patch
stage: patch review
pull_requests: + pull_request6623
2018-05-16 00:27:41Jelle Zijlstrasetmessages: + 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:27Jelle Zijlstracreate