python / python/mypy

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

Aperta
#14,602 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo l’esempio Python fornito con mypy e confronta il tipo inferito di foo_func1 con il tipo previsto Foo[Callable[[int], str]]. Segui la diagnostica di compatibilità dell’argomento callable e conferma che il lavoro è completato quando la chiamata finale a ap non segnala più un errore arg-type per tipi Callable equivalenti.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.