python / python/mypy

Annotation of `Callable` and `function`

Offen
#14,319 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
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 func argument with List[function]? Where do I get function from? As far as I can see, it is not part of standard Python nor in the typing library.

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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Ö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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.