python / python/mypy

overload with wrong return type is ignored

Aperta
#17,857 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-overloads
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

When one of the alternatives of an overload has a return type T which is a subtype of the return type of the annotation U but that the body of the function returns a type V which is a subtype of U but not T, no error is reported

To Reproduce

from typing import overload

@overload
def f(a: int) -> str:
    ...

# this is completely wrong
@overload
def f(a: str) -> bool:
    ...


def f(a: str | int) -> str | bool:
    return "Hello"

reveal_type(f("some string"))

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=83c5a353e087d8bbe03d542e7340b4b5

Expected Behavior

mypy should complain that the body of the function returns an str which is incompatible with type bool (overload 2)

More generally, for each overload signature, mypy should check that the body of the function typechecks with the overload signature.

Actual Behavior

mypy reports no error

main.py:16: note: Revealed type is "builtins.bool"
Success: no issues found in 1 source file

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 il riproduttore Python mostrato in main.py ed eseguilo con la configurazione strict di mypy-play collegata. Traccia come le firme di overload vengono verificate rispetto al tipo restituito dell’implementazione. Il lavoro è completato quando mypy segnala il ritorno str incompatibile per overload 2 e la regressione è coperta da un test.

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
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.