Cannot infer type argument with TypeVarTuple and Callback protocol
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
To Reproduce
# mypy: enable-incomplete-feature=NewGenericSyntax
from collections.abc import Callable
from typing import Protocol
class ActionType(Protocol):
def __call__(self, var: str, context: int = 2) -> None: ...
class Job[*_Ts]:
def __init__(self, target: Callable[[*_Ts], None]) -> None:
self.target = target
def run_job[*_Ts](job: Job[*_Ts], *args: *_Ts) -> None: ...
def a1(action: ActionType) -> None:
job = Job(action)
run_job(job, "Hello") # -> error
Actual Behavior
error: Cannot infer type argument 1 of "run_job" [misc]
Expected Behavior
No error. Mypy should be able to tell that context has a default value and is thus optional. It already works from pure Callables (without the intermediate generic class).
def run_job_2[*_Ts](action: Callable[[*_Ts], None], *args: *_Ts) -> None: ...
def a2(action: ActionType) -> None:
run_job_2(action, "Hello") # works fine
Your Environment
- Mypy version used:
mypy 1.11.0+dev.177c8ee7b8166b3dcf89c034a676ef5818edbc38 (compiled: no)(current master) - Mypy command-line flags:
--enable-incomplete-feature=NewGenericSyntax(the bug exists with the old generic syntax as well) - Python version used:
3.12
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 con il reproducer e confronta il percorso run_job che fallisce con l’esempio funzionante run_job_2 usando Python 3.12 e il flag NewGenericSyntax. Traccia l’inferenza dei tipi per TypeVarTuple attraverso Job e il callback ActionType; il lavoro è completato quando il primo esempio inferisce i propri argomenti senza errori e il comportamento esistente rimane intatto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100