python / python/mypy

Overriding methods with partially generic signatures leads to mypy errors

Aperta
#17,363 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

Descrizione

Bug Report

If I define an abstract method in a base class with generic parameters and one specific parameter, I cannot have a subclass implementing this method with specific parameters without mypy complaining about incompatible signatures. If I don't specify any specific parameters in the superclass I don't get an error.

To Reproduce
func has generic parameters only and I can override it with a method with a specific signature
func2 has generic parameters, but also defines a specific parameter super_arg. If I override it with a method having specific parameters only, mypy complains about incompatible signatures, although I also have super_arg in my method signature.

import abc
import typing as t


class A(abc.ABC):
    @abc.abstractmethod
    def func(self, *args: t.Any, **kwargs: t.Any) -> str: ...

    @abc.abstractmethod
    def func2(self, *args: t.Any, super_arg: int, **kwargs) -> str: ...

class B(A):
    def func(self, a: str, b: str, c: int = 0) -> str:
        return a + b + str(c)

    def func2(self, a: str, b: str, super_arg: int, c: int = 0) -> str:
        return a + b + str(super_arg) + str(c)

Expected Behavior

I would expect that mypy doesn't fail on this.

Actual Behavior

mypy raises an error because of incompatible signatures:

test.py:16: error: Signature of "func2" incompatible with supertype "A"  [override]
test.py:16: note:      Superclass:
test.py:16: note:          def func2(self, *args: Any, super_arg: int, **kwargs: Any) -> str
test.py:16: note:      Subclass:
test.py:16: note:          def func2(self, a: str, b: str, super_arg: int, c: int = ...) -> str

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11.3

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

Inizia con la riproduzione fornita in test.py ed eseguila con mypy 1.10.0 usando l’ambiente Python 3.11.3 mostrato. Traccia il percorso di controllo delle firme di override per func2 e confrontalo con func; il lavoro è completato quando l’errore di firma incompatibile segnalato è risolto senza indebolire il controllo valido degli override.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.