python / python/mypy

False positive: "overloaded signature will never be matched" with Self

Aperta
#17,702 0 commenti 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

When combining Self and base class overloads, mypy incorrectly warns about the former being "the same or broader" as the latter. However, those overloads are both actually used.

I do know that this can be solved with a single TypeVar - the case is extracted from a more complicated code where similar TypeVar solution doesn't seem to apply.

To Reproduce

playground

from __future__ import annotations

from typing import Self, overload

class Base:
    @overload
    def fn(self, other: Self) -> Self: ...
    @overload
    def fn(self, other: Base) -> Base: ...
    def fn(self, other: Base) -> Base:
        return other


class X(Base): ...
class Y(Base): ...

reveal_type(X().fn(X()))
reveal_type(X().fn(Y()))
reveal_type(Y().fn(X()))
reveal_type(Y().fn(Y()))

Expected Behavior

No errors reported

Actual Behavior

main.py:10: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader  [misc]
main.py:18: note: Revealed type is "__main__.X"
main.py:19: note: Revealed type is "__main__.Base"
main.py:20: note: Revealed type is "__main__.Base"
main.py:21: note: Revealed type is "__main__.Y"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.1 and master (playground)
  • Mypy command-line flags: none and --strict - both reproduce
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11, 3.12 - does not affect

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 dalla riproduzione collegata di mypy-play e dall’esempio di overload nell’issue, quindi segui il modo in cui mypy verifica le firme di overload sovrapposte che coinvolgono Self e le classi base. Il lavoro è completo quando l’esempio non segnala alcun errore di falso positivo e conserva i quattro tipi rivelati mostrati nel report.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 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.