python / python/mypy

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

Offen
#17,649 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere den Bericht mit mypy_pop_error.py unter Verwendung des angegebenen mypy-Befehls und einer minimalen deque-Unterklasse. Verfolge, wie die Signaturen von deque und MutableSequence für Überschreibungen verglichen werden. Als erledigt gilt die Aufgabe, wenn das Beispiel keinen Überschreibungsfehler erzeugt und dabei das Laufzeitverhalten von deque beibehält, das ein Indexargument ablehnt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.