python / python/mypy

Overload validation does not properly handle implied subtype relationships (e.g. int and float)

Aperta
#10,143 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

I'm working to make pyright's overload validation work consistently with mypy's.

Mypy's overload validation behavior is spec'ed here: https://github.com/python/typing/issues/253#issuecomment-389262904

As part of this validation, mypy reports errors when there is overlap between two overloads that return different return types. The following test case shows four such examples. Mypy properly reports the error in three of the four cases, but it misses the case where there is an implied subtype relationship, as between int and float.

# pyright: strict

from typing import Literal, Union, overload

class Parent: ...
class Child(Parent): ...

# Test 1: Literal subtype
@overload
def foo1(x: Literal[3]) -> int: ...
@overload
def foo1(x: int) -> str: ...

# Test 2: Subclass subtype
@overload
def foo2(x: Child) -> str: ...
@overload
def foo2(x: Parent) -> int: ...

# Test 3: Implicit subtype
@overload
def foo3(x: int) -> str: ...  # Mypy does not report error here
@overload
def foo3(x: float) -> int: ...

# Test 4: Union subtype
@overload
def foo4(x: int) -> str: ...
@overload
def foo4(x: Union[int, str]) -> int: ...

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 riproducendo i quattro esempi di overload dell’issue e confronta le diagnosi di mypy con la typing specification collegata. Traccia quindi il percorso di validazione di overload responsabile dei controlli di sovrapposizione dei sottotipi, poi verifica che il caso int/float venga diagnosticato in modo coerente con i casi literal, subclass e union.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.