python / python/mypy

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

Open
#17,649 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with mypy_pop_error.py using the stated mypy command and minimal deque subclass. Trace how the deque and MutableSequence signatures are compared for overrides. Done means the example produces no override error while retaining deque's runtime behavior that rejects an index argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.