Annotation of `Callable` and `function`
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 am experiencing the following problem: I have a function that takes a list of functions with a set default argument. I am annotating this variable with List[Callable] which leads to the following error when I supply a defined list of functions as default argument.
In code, I am e.g. doing this:
from typing import Callable
def fun(x):
return x
def fun2(x, y):
return 2 * x + y
default_functions = [fun, fun2]
def fun3(func: list[Callable] = default_functions):
return func
def fun4(func: list[Callable] = [fun, fun2]):
return func
Expected Behavior
I would have expected that both function definitions are correct: the functions fun1, fun2 are of type Callable and putting them into a list should fulfifl the type annotation.
Actual Behavior
Running mypy on this leads to:
scripts/mypy_callable.py:15: error: Incompatible default for argument "func" (default has type "List[function]", argument has type "List[Callable[..., Any]]") [assignment]
Found 1 error in 1 file (checked 1 source file)
which refers to fun3.
So, there are three things I don't understand here:
- Why is there a difference between fun3 and fun4?
- As far as I understand, function is a subtype of Callable so why wouldn't a default value of type List[function] be acceptable to mypy here?
- If I accept this behavior, how would I annotate the
funcargument withList[function]? Where do I getfunctionfrom? As far as I can see, it is not part of standard Python nor in thetypinglibrary.
Your Environment
(mypy version 0.991, python 3.10.8)
- Mypy version used: 0.991
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10.8
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 l'esempio dell'issue usando scripts/mypy_callable.py e il comando mypy indicato senza flags. Inizia tracciando il modo in cui mypy inferisce i tipi di default_functions e dell'elenco inline; il lavoro è completato quando la discrepanza viene risolta con un comportamento corrispondente oppure le aspettative vengono documentate chiaramente.
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