python / python/mypy

improper inference of lambdas and dataclass constructors used as generic decorator arguments

Aperta
#20,593 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

I'm attempting to make some registry-like decorators to be generic in my code and bumped into mypy complaining about my constructs.

The point is: concrete decorators should be defined with [HandlerT: Handler] type variable. It's to correctly decorate functions with non-exact (but matching) call signatures. Signatures should be preserved.
The rest registry functionality to be handled with broadly generic _decorate function.

I supposed decorate1, decorate2 and decorate3 should behave the same. Among them, lambda version preferred due to its conciseness and flexibility. Nevertheless, only decorate3 passes mypy checks. FTR, pyright passes them all.

To Reproduce

from collections.abc import Callable
from dataclasses import dataclass

type Handler = Callable[[int], str]

@dataclass
class Wrapper:
    handler: Handler

registry: dict[str, Wrapper] = {}

def _decorate[WrappedT](
        key: str, wrapper: Callable[[WrappedT], Wrapper]) -> Callable[[WrappedT], WrappedT]:
    def _wrap(handler: WrappedT) -> WrappedT:
        registry[key] = wrapper(handler)
        return handler
    return _wrap

def decorate1[HandlerT: Handler](key: str) -> Callable[[HandlerT], HandlerT]:
    return _decorate(key, Wrapper)   # line 27

def decorate2[HandlerT: Handler](key: str) -> Callable[[HandlerT], HandlerT]:
    return _decorate(key, lambda handler: Wrapper(handler))   # line 31

def decorate3[HandlerT: Handler](key: str) -> Callable[[HandlerT], HandlerT]:
    def _wrapper(handler: HandlerT) -> Wrapper:
        return Wrapper(handler)
    return _decorate(key, _wrapper)

@decorate1("add1")
@decorate2("add2")
@decorate3("add3")
def add_handler(x: int, y: int = 0) -> str:
    return str(x + y)

@decorate1("inc1")
@decorate2("inc2")
@decorate3("inc3")
def inc_handler(x: int) -> str:
    return add_handler(x, 1)

Expected Behavior

all of decorate1, decorate2 and decorate3 forms are correct, no errors shown

Actual Behavior

wrappers.py:27: error: Incompatible return value type (got "Callable[[Callable[[int], str]], Callable[[int], str]]", expected "Callable[[HandlerT], HandlerT]")  [return-value]
wrappers.py:31: error: Argument 1 to "Wrapper" has incompatible type "WrappedT"; expected "Callable[[int], str]"  [arg-type]

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.12

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 inserendo la riproduzione riportata in wrappers.py ed eseguendo mypy 1.19.1 con il codice Python 3.12 mostrato. Confronta le diagnostiche per decorate1, decorate2 e decorate3, quindi traccia l'inferenza dei callable generici per il costruttore di dataclass e la lambda. Il lavoro è completato quando tutte e tre le forme di decoratore superano il controllo dei tipi senza errori, preservando le firme riportate.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.