python / python/mypy

Get arg-type incompatible type error for arguments of a Callable type

Offen
#14,602 0 Kommentare 1 Reaktion 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

For the sample code below, mypy is able to infer all the type information. mypy shows that the type of foo_func1 is Foo[(n: int) -> str]. However, on the last line of the sample code, mypy gives the following type error:

error: Argument 1 to "ap" has incompatible type "Foo[Callable[[Arg(int, 'n')], str]]"; expected "Foo[Callable[[int], str]]" [arg-type]

As the error message shows, the caller type (i.e. Foo[Callable[[Arg(int, 'n')], str]] ) and callee type (i.e. Foo[Callable[[int], str]] ) are in essence equal. Is there a way to make the type checking pass?

To Reproduce

from dataclasses import dataclass
from typing import Callable, Generic, TypeVar

A = TypeVar('A')
C = TypeVar('C')
E = TypeVar('E')

@dataclass
class Foo(Generic[A]):
    value: A

def ap(f: Foo[Callable[[C], E]], other: Foo[C]) -> Foo[E]:
        return Foo(f.value(other.value))

def func1(n: int) -> str:
    return 'h' * n

foo_func1 = Foo(func1)  
foo_5 = Foo(5)
foo_result = ap(foo_func1, foo_5)

Expected Behavior

Something like foo_func1's type should match the type Foo[Callable[[int], str]].

Actual Behavior

mypy gives the following type error:

error: Argument 1 to "ap" has incompatible type "Foo[Callable[[Arg(int, 'n')], str]]"; expected "Foo[Callable[[int], str]]" [arg-type]

for the last line of the sample code above.

Your Environment

  • Mypy version used: latest source from this git repo
  • Mypy configuration options from mypy.ini (and other config files):

allow_redefinition = true
check_untyped_defs = true
ignore_errors = false
ignore_missing_imports = true
implicit_reexport = false
local_partial_types = true
no_implicit_optional = true
strict_equality = false
strict_optional = true
warn_no_return = true
warn_redundant_casts = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

  • Python version used: 3.11.1

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

Beginne damit, das bereitgestellte Python-Beispiel mit mypy auszuführen und den inferierten Typ von foo_func1 mit dem erwarteten Typ Foo[Callable[[int], str]] zu vergleichen. Verfolge die Diagnose zur Kompatibilität des Callable-Arguments und bestätige, dass die Arbeit abgeschlossen ist, wenn der abschließende Aufruf von ap keinen arg-type-Fehler mehr für äquivalente Callable-Typen meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
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.