Skip to content
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

Validity of collections.deque as typing.Sequence #12047

Open
einarwar opened this issue May 27, 2024 · 2 comments
Open

Validity of collections.deque as typing.Sequence #12047

einarwar opened this issue May 27, 2024 · 2 comments
Labels
stubs: false negative Type checkers do not report an error, but should

Comments

@einarwar
Copy link

einarwar commented May 27, 2024

Hi

Based on this discussion on pyright: microsoft/pyright#7989 (comment)

A collections.deque is a valid typing.Sequence. However, typing.Sequence allows being sliceable, while a collections.deque does not support slicing. This makes room to make errors that is not caught by static type checkers like pyright and mypy.

It seems like this normally would be reported, but the errors are suppressed by type-ignore comments found here.

Is there any other way of handling this so that trying to slice a deque would result in a type-error?

@einarwar einarwar changed the title Validity of collections.abc.deque as typing.Sequence Validity of collections.deque as typing.Sequence May 27, 2024
@srittau
Copy link
Collaborator

srittau commented May 27, 2024

Considering that deque doesn't derive from (Mutable)Sequence at runtime, we should at least try to remove that base class. This might be too disruptive, but an exploratory PR would be welcome.

@srittau srittau added the stubs: false negative Type checkers do not report an error, but should label May 27, 2024
@AlexWaygood
Copy link
Member

AlexWaygood commented May 27, 2024

See python/cpython#113313 for some previous discussion on this. Considering that isinstance(deque(), MutableSequence) evaluates to True at runtime, and that this is the only respect in which deque is not a true Sequence, I think we've probably currently got the best compromise available. But exploratory PRs to gauge the possible impact of a change are of course always welcome :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false negative Type checkers do not report an error, but should
Projects
None yet
Development

No branches or pull requests

3 participants