Can't assign default argument value to type when using ParamSpec
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
I have a function that takes a Callable with ParamSpec, and I want to set a default for this parameter to a type.
To Reproduce
from typing import Callable, Concatenate, ParamSpec
P = ParamSpec("P")
class Foo:
def __init__(self, a: int, /, b: str) -> None:
self.a = a
self.b = b
def func(cls: Callable[Concatenate[int, P], Foo] = Foo) -> Callable[P, Foo]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Foo:
return cls(1, *args, **kwargs)
return wrapper
However, mypy complains:
main.py:11: error: Incompatible default for argument "cls" (default has type "type[Foo]", argument has type "Callable[[int, **P], Foo]") [assignment]
Expected Behavior
I expect mypy to accept this default value, since removing the default and calling func works fine:
def func(cls: Callable[Concatenate[int, P], Foo]) -> Callable[P, Foo]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Foo:
return cls(1, *args, **kwargs)
return wrapper
func(Foo)("b")
Your Environment
- Mypy version used: 1.5.1
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11
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 riproduttore main.py e gli esempi mypy-play collegati, quindi segui il controllo del Callable predefinito basato su ParamSpec. Confronta le definizioni di func con e senza valore predefinito. Il lavoro è completato quando l’errore segnalato di valore predefinito incompatibile è risolto per questo esempio e il comportamento è coperto da un test di regressione appropriato.
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
- 42/100