-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Documentation for object.__getitem__ is over-specified. #113313
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
Comments
Cc. @rhettinger |
Previous discussion at: |
I was reading through the linked issues and I kind of understand the matters of practicality vs purity, and the fact that concrete classes sometimes deviate a little bit from their ABCs. from collections import deque
from collections.abc import Sequence
def foo(seq: Sequence):
seq[:]
foo(deque()) Given that I'm not doing anything naughty with casts or trying to bend typing to my will and given that type-checking passes, shouldn't the code work? |
Ideally, yes. We do the best we can at typeshed by overriding the stub for None of this is to say that we should definitely change |
FWIW, this isn't a bug in CPython. A class registered with collections.abc.Sequence or MutableSequence is not required to support slicing. There has never been a requirement that deques support slicing. We only added indexing late in game and that was mainly only to support We could make this a feature request. I've worked on in it this past and it was a big project, much more complicated than it seemed at first. In the end, I dropped it because there isn't much of a need. Most of what users want deques for is already satisfied by the current API. We expand APIs to satisfy compelling needs, not to make type annotations more parallel (the tail wagging the dog). As @AlexWaygood notes, TypeShed chose its assumptions based on what worked well for most users most of the time, but it definitely did not create a requirement to change the concrete implementation. Any objections to marking this as closed? |
I'm not particularly interested or invested in having
I may have misinterpreted the docs, but the
(My original issue has the complete “documentation paper trail” that links everything together.) Is it a case, then, that the If you think I misinterpreted the docs, would you be so kind as to help me understand where in the chain of links I followed above I made a mistake? I hope you understand my persistence comes from the fact that, from where I currently stand, it looks like code and documentation are at odds, which seems to imply one needs to be tweaked. |
It would reasonable to add "(and optionally slices)" to the |
I've uploaded a PR because the current wording over-specifies the API and doesn't reflect reality. FWIW, for much of its history, the term "sequence" was very general and used loosely. It was meant to distinguish between the two most common patterns of using When When Later, type annotations came along. For the most part, they were able to describe reality, but it didn't fit perfectly and occassional approximations had to be made (for example, that is why a function annotated with When sequence is spelled with a capital S, it means either |
…ences. (pythongh-113377) (cherry picked from commit 6a5b473) Co-authored-by: Raymond Hettinger <[email protected]>
…ences. (pythongh-113377) (cherry picked from commit 6a5b473) Co-authored-by: Raymond Hettinger <[email protected]>
collections.deque
is a sequence but doesn't implement the full sequence protocol (doesn't support slices)
I didn't know that there used to be a dunder method Thanks for your thorough reply, @rhettinger, and thanks for handling this. |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
collections.deque
is a sequence but doesn't implement the full sequence protocol (doesn't support slices).collections.deque
is a sequence:The docs on
collections.abc.Sequence
say this is an ABC for sequences, linking to the docs glossary entry on sequences:In turn, the glossary entry says sequences must implement
__getitem__
:Finally, the
__getitem__
docs say that sequences must accept integers and slice objects:(Excerpts taken from the Python 3.12.1 documentation on 20/12/2023.)
I see one of three resolutions:
issubclass(deque, Sequence)
returnsFalse
deque
__getitem__
to say “For sequence types, the accepted keys should be integers (and optionally slice objects).”I don't know which one is the “best”.
CPython versions tested on:
3.8, 3.12
Operating systems tested on:
macOS
Linked PRs
The text was updated successfully, but these errors were encountered: