python / python/mypy

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

Offen
#20,593 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-type-context
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

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, die gemeldete Reproduktion in wrappers.py einzufügen und mypy 1.19.1 mit dem gezeigten Python-3.12-Code auszuführen. Vergleiche die Diagnosen für decorate1, decorate2 und decorate3 und verfolge anschließend die Inferenz generischer Callables für den Dataclass-Konstruktor und den Lambda-Ausdruck. Abgeschlossen ist die Aufgabe, wenn alle drei Decorator-Formen ohne Fehler typgeprüft werden und dabei die gemeldeten Signaturen erhalten bleiben.

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

Neue Issues direkt in Ihr Postfach

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