Detect missing or incorrect overload defaults
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Descrizione
Feature
Is it in-scope for either mypy or stubtest to detect incorrect or missing defaults in overloads?
Incorrect default
from typing import Literal, overload, reveal_type
@overload
def foo(a: Literal[True] = ...) -> None: ...
@overload
def foo(a: Literal[False]) -> int: ...
def foo(a: bool = False) -> None | int:
return 1 if not a else None
reveal_type(foo())
Here, the default for a is False, yet the overload has a: Literal[True] = ..., meaning that foo() will be revealed to be of type None instead of type int
Missing overload example
from typing import Literal, overload
@overload
def foo(a: Literal[True]) -> None: ...
@overload
def foo(a: Literal[False]) -> int: ...
def foo(a: bool = False) -> None | int:
return 1 if not a else None
The default for a is False, so the second overload should probably include a: Literal[False] = ...?
Pitch
I tried putting together a little static analysis tool to detect simple cases of the above: https://github.com/MarcoGorelli/fix-overload-defaults
It only uses ast parsing, and is very simple, so it can't detect much, but it was already enough to find issues in a few libraries where I tried it:
- https://github.com/numpy/numpy/pull/29331
- https://github.com/numpy/numpy/pull/29340
- https://github.com/pola-rs/polars/pull/23442
- https://github.com/pola-rs/polars/pull/23413
- https://github.com/narwhals-dev/narwhals/pull/2778
So, I figured that this might be broadly useful?
I'm much more bothered about detecting incorrect defaults than potentially missing ones. Might this be in-scope for mypy or mypy.stubtest?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando i controlli degli overload esistenti di mypy e mypy.stubtest, quindi confrontali con lo strumento basato su AST fix-overload-defaults collegato nell’issue. Convalida il comportamento usando i due esempi foo e determina se il rilevamento di defaults errati, defaults mancanti o entrambi rientra nell’ambito.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100