python / python/mypy

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

Open
#20,593 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-context
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by placing the reported reproduction in wrappers.py and running mypy 1.19.1 with the shown Python 3.12 code. Compare the diagnostics for decorate1, decorate2, and decorate3, then trace generic callable inference for the dataclass constructor and lambda. Done means all three decorator forms type-check without errors while preserving the reported signatures.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.