python / python/mypy

TypeVar in higher order decorator with type alias not resolved properly

Aperta
#18,503 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-type-alias topic-type-variables
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

from collections.abc import Callable

type A1[**P, R] = Callable[[Callable[P, R]], Callable[P, R]]
type A2[**P, R] = Callable[P, R]


def decorator[**P, R]() -> A1[P, R]:
    def inner(func: Callable[P, R]) -> Callable[P, R]:
        return func
    return inner

def decorator_working[**P, R]() -> Callable[[A2[P, R]], A2[P, R]]:
    def inner(func: Callable[P, R]) -> Callable[P, R]:
        return func
    return inner


@decorator()  # error
def f1(x: str) -> int:
    return 1

@decorator_working()
def f2(x: str) -> int:
    return 1


reveal_type(f1)
reveal_type(f2)
Condensed version for debugging
# mypy: allow-empty-bodies
from collections.abc import Callable

type A1[**P, R] = Callable[[Callable[P, R]], Callable[P, R]]
type A2[**P, R] = Callable[P, R]

def decorator[**P, R]() -> A1[P, R]: ...
def decorator_working[**P, R]() -> Callable[[A2[P, R]], A2[P, R]]: ...

@decorator()
def f1(x: str) -> int: ...

@decorator_working()
def f2(x: str) -> int: ...

reveal_type(f1)
reveal_type(f2)

Expected Behavior
Mypy should be able to resolve the TypeVars for decorator just like it does for decorator_working.

Actual Behavior

error: Argument 1 has incompatible type "Callable[[str], int]"; expected "Callable[[VarArg(Never), KwArg(Never)], Never]"  [arg-type]
note: Revealed type is "def (*Never, **Never) -> Never"
note: Revealed type is "def (x: builtins.str) -> builtins.int"

Your Environment

  • Mypy version used: mypy 1.15.0+dev.68cffa7afe03d2b663aced9a70254e58704857db (compiled: no)
  • Python version: 3.13

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 riproducendo l’esempio condensato nell’issue con mypy su Python 3.13, quindi confronta le diagnosi e i tipi rivelati per decorator e decorator_working. Segui il percorso di inferenza del decoratore di ordine superiore e dell’alias di tipo; il lavoro è completato quando decorator risolve i suoi TypeVars come decorator_working, accetta f1 e rivela il tipo callable previsto senza errori.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.