python / python/mypy

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

Aperta
#17,649 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il report con mypy_pop_error.py usando il comando mypy indicato e una sottoclasse minima di deque. Traccia il modo in cui le firme di deque e MutableSequence vengono confrontate per gli override. Il lavoro è completato quando l’esempio non produce errori di override e conserva il comportamento a runtime di deque, che rifiuta un argomento indice.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.