Annotation of `Callable` and `function`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Beispiel aus dem Issue mit scripts/mypy_callable.py und dem angegebenen mypy-Befehl ohne Flags. Beginne damit nachzuverfolgen, wie mypy die Typen von default_functions und der Inline-Liste ableitet; die Aufgabe ist erledigt, wenn die Abweichung durch übereinstimmendes Verhalten behoben oder die erwarteten Bedingungen klar dokumentiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100