-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add SimpleSequence as preferred alias for SupportsLenAndGetItem #6099
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
Conversation
This protocol represents a sequence as defined in the Python documentation, as opposed to collections.abc.Sequence.
Maybe we can find a better name? But it should have |
I think the name should be super obvious, so that you immediately know what it does when you see it somewhere in the code, such as |
There already is a concept in Python that expresses this, "sequence". We should use this name to make it clear that this protocol actually reflect that concept. |
What about functions that only require the sequence to have a |
Actually nevermind. A type like "what docs describe as As a side note, I generally consider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intended to include this in my other review, but I think I clicked the wrong button.
Edit: Now my first review comment was lost somehow, I'll just write it here instead of trying to add another review and losing something again :) The new name should say "it is sequence the concept, not the class" or "it has __len__
and __getitem__
". So something like SequenceAccordingToDocs
, but that isn't a great name either.
def __len__(self) -> int: ... | ||
def __getitem__(self, __k: int) -> _T_co: ... | ||
|
||
# obsolete, use SimpleSequence instead |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just delete it? It was not marked with # stable
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess if it's used in third party stubs (there seems to be the one single case), under some circumstances you could run into silent loss of type checking. This already ends up happening though, e.g. see #5786 and #5751, so I'd also be inclined to just delete it.
If we don't delete it, we should alias here, though, and still update the annoy
stubs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The annoy stubs were my main concern. If we update them, they won't work, until type checkers have been updated with the latest typeshed. I'd suggest keeping it for a few months.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel strongly about this name vs SupportsLenAndGetItem. Typing users I interact with still get confused about just Sequence vs List :-)
|
As a maintainer, I would love it to be |
How about |
Yet another option would bo something like |
I don't like I'm OK with most other options presented here, but I prefer |
Diff from mypy_primer, showing the effect of this PR on open source code: bidict (https://github.com/jab/bidict)
- bidict/_base.py:395: error: Argument 1 to "reversed" has incompatible type "MutableMapping[KT, VT]"; expected "SupportsLenAndGetItem[KT]" [arg-type]
+ bidict/_base.py:395: error: Argument 1 to "reversed" has incompatible type "MutableMapping[KT, VT]"; expected "SimpleSequence[KT]" [arg-type]
|
I don't think we have consensus here, so closing as per #6563 (comment) |
This protocol represents a sequence as defined in the Python
documentation, as opposed to collections.abc.Sequence.