-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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