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
Should MutableSequence provide .copy()? #77700
Comments
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()? |
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. |
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. |
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). |
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). |
Makes sense. I can provide a patch to the docs. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: