Skip to content

Add Sequence to documentation #6536

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

Closed
alexchamberlain opened this issue Mar 12, 2019 · 5 comments
Closed

Add Sequence to documentation #6536

alexchamberlain opened this issue Mar 12, 2019 · 5 comments

Comments

@alexchamberlain
Copy link

Apologies in advance if I've failed to search the docs. As far as I can tell, Sequence isn't formally documented - it would fit quite nicely in Predefined Protocols I think.

I will look to make this change myself over the next couple of days, but I wanted to raise the issue first in case I had a complete fail with the docs.

@alexchamberlain
Copy link
Author

@JelleZijlstra
Copy link
Member

It's not listed as a Protocol because it's not actually a Protocol. There might be another place in the mypy docs where it could usefully be mentioned though.

@ilevkivskyi
Copy link
Member

Yes, it is not a protocol. The remainder is essentially a duplicate of #31

@vectro
Copy link

vectro commented Mar 1, 2023

Apologies for necro-posting, but why isn't it a protocol? The fact that it's not means you can't do something like:

@typing.runtime_checkable
class HashableProtocol(
    typing.Protocol[T_co], typing.Hashable, typing.Sequence[T_co]
):
    pass

@TeamSpen210
Copy link
Contributor

@vectro, the reason the more complicated ABCs aren't protocols is that they don't just imply that specific method signatures exist, they imply behaviours that can't just be inferred. For instance if len(seq) > i, seq[i] won't raise an exception. In particular, distinguishing between Sequence and Mapping isn't possible just from methods. See this paragraph in the collections.abc docs:

... Interfaces specify semantics and relationships between methods that cannot be inferred solely from the presence of specific method names. For example, knowing that a class supplies __getitem__, __len__, and __iter__ is insufficient for distinguishing a Sequence from a Mapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants