-
Hi Setup: Take the following example from collections import deque
from collections.abc import Sequence
d = deque([1,2,3,4,5])
def slicing_func_with_sequence(seq: Sequence[int]) -> Sequence[int]:
return seq[1:-1]
print(my_slicing_func_with_sequence(d)) # Raises TypeError: index must be integer, not 'slice' No type-issue is raised when trying to use a deque as an argument to a function that takes a In the following, the input argument is typed as a deque: from collections import deque
from collections.abc import Sequence
from typing import reveal_type
d = deque([1,2,3,4,5])
def slicing_func_with_deque(seq: deque[int]) -> deque[int]:
x = seq[1:-1]
reveal_type(x) # Type of "x" is "int"
return x
Now two type errors are issued as expected, indicating that this indeed does not work:
While I am aware that wanting to slice a I am not sure if this is a pyright problem or rather a problem in typeshed, python or something that should be handled by e.g |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This isn't a bug in pyright, which is working correctly given the type information provided to it. This is arguably a bug in the implementation of Type checkers like pyright and mypy will detect and report LSP violations like this, but in this case the maintainers of typeshed have suppressed the error with a You could ask the typeshed maintainers if they can think of a better way to handle this, but I can't think of any off the top of my head. You could also report this as a bug in stdlib's implementation of |
Beta Was this translation helpful? Give feedback.
This isn't a bug in pyright, which is working correctly given the type information provided to it.
This is arguably a bug in the implementation of
deque
which doesn't honor the Liskov Substitution Principle. The LSP is a fundamental rule in object-oriented programming, anddeque
is violating it. Thedeque
class inherits fromMutableSequence
, but it doesn't implement all of the functionality ofMutableSequence
.Type checkers like pyright and mypy will detect and report LSP violations like this, but in this case the maintainers of typeshed have suppressed the error with a
# type: ignore
comment. See here.You could ask the typeshed maintainers if they can think of a better way to handle thi…