`functools.partial` plugin is only triggered on calls
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
partial support is too fragile and easy to fool.
To Reproduce
import typing as t
from functools import partial
# 1.
# uncomment this to get type error
# partial(lambda x, y: x + y, 10)()
# 2.
# But this silly little thing passes type-check
silly: t.Callable[[], int] = partial(lambda x, y: x + y, 10)
silly()
Expected Behavior
Fail to coerce partial into incompatible callable type.
Actual Behavior
Success: no issues found in 1 source file
Your Environment
- Mypy version used: master
- Mypy command-line flags: no
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.12
Looks like we carry information about needed arguments somewhere anyways, since 1 fails type check due to not enough arguments, so why we allow to coerce that partial into incompatible callable?
Perhaps this is due to this definition of partial:
def __call__(self, /, *args: Any, **kwargs: Any) -> _T: ...
But, for example, pyright takes that into account and fails as expected:
[nix-shell:/tmp]$ pyright issue.py
/tmp/issue.py
/tmp/issue.py:10:30 - error: Expression of type "partial[Unknown]" is incompatible with declared type "() -> int"
Type "partial[Unknown]" is incompatible with type "() -> int"
Extra parameter "y" (reportAssignmentType)
1 error, 0 warnings, 0 informations
I think it worth fixing, since we support partial and it is useful to rely on that support. It can be fixed, for example, as viewing partial result internally not as partial[int] type, but as something like
class FakePartial(Protocol[P, Ret]):
@property
def args(self) -> tuple[Any, ...]: ...
@property
def keywords(self) -> dict[any, ...]: ...
@property
def func(self) -> t.Callable[..., Ret]: ...
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Ret: ...
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
Riproduci i due esempi usando Python 3.12 e mypy master, quindi confronta il comportamento con la definizione di typeshed functools.pyi collegata nell’issue. Traccia il supporto esistente per functools.partial e le relative informazioni sugli argomenti. Il lavoro è completato quando l’assegnazione incompatibile a un callable senza argomenti viene rifiutata, mentre le chiamate valide a partial continuano a superare il controllo dei tipi.
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
- 30/100