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

Signature of "pop" incompatible with supertype "MutableSequence" for deque subclass #17649

Open
sveinugu opened this issue Aug 6, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@sveinugu
Copy link
Contributor

sveinugu commented Aug 6, 2024

Bug Report

Mypy incorrectly reports method signature override inconsistencies with MutableSequence for subclasses of deque, even though the subclass method signature is consistent with the method signature in the deque class. I experienced this for the pop() method, but I suspect this might be the case for other methods where the deque signature overrides the MutableSequence signature, if any.

To Reproduce

Mypy reports an error with the following minimal example:

from collections import deque
from typing import Any


class MyDeque(deque):
    def pop(self) -> Any:
        return super().pop()

Expected Behavior

No error reported for the above code

Actual Behavior

The error reported is as follows:

mypy_pop_error.py:6: error: Signature of "pop" incompatible with supertype "MutableSequence"  [override]
mypy_pop_error.py:6: note:      Superclass:
mypy_pop_error.py:6: note:          def pop(self, index: int = ...) -> Any
mypy_pop_error.py:6: note:      Subclass:
mypy_pop_error.py:6: note:          def pop(self) -> Any

This makes no sense to me as while MutableSequence does specify a variant of pop() with an index parameter (see https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types), the index parameter is not supported by deque (see https://docs.python.org/3/library/collections.html#collections.deque). This is also confirmed by mypy if we e.g. change the method to:

class MyDeque(deque):
    def pop(self, index: int = -1) -> Any:
        return super().pop(index)

Then mypy correctly reports:

mypy_pop_error.py:7: error: Too many arguments for "pop" of "deque"  [call-arg]

This is consistent with runtime:

d = MyDeque([1, 2, 3])
d.pop()

Which results in:

(...)
  File "/PATH/TO/mypy_pop_error.py", line 7, in pop
    return super().pop(index)
TypeError: MyDeque.pop() takes no arguments (1 given)

Your Environment

  • Mypy version used: mypy 1.11.1 (compiled: yes)
  • Mypy command-line flags: --config-file=
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.8
@sveinugu sveinugu added the bug mypy got something wrong label Aug 6, 2024
@sveinugu
Copy link
Contributor Author

sveinugu commented Aug 24, 2024

I get a similar error for __setitem__ and __delitem__, e.g.:

class MyDeque(deque):
    def __setitem__(self, __key: SupportsIndex, __value: _ObjT) -> None:
        return super().__setitem__(__key, __value)

    def __delitem__(self, __key: SupportsIndex) -> None:
        return super().__delitem__(__key)

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

No branches or pull requests

1 participant