Repository navigation
Replies: 1 comment
|
There are two fundamental reasons why plain functions and static methods fail with this pattern:
Working ImplementationHere is the complete pattern supporting:
from typing import Any, Callable, Concatenate, Generic, ParamSpec, TypeVar, overload
from typing_extensions import Self
_T = TypeVar("_T")
_P = ParamSpec("_P")
_R = TypeVar("_R")
class UniversalDecorator(Generic[_T, _P, _R]):
wrapped: Callable[..., _R] | None
# 1. Initialization overloads:
# 1a. Factory mode with arguments: @UniversalDecorator(arg=1)
@overload
def __init__(self, wrapped: None = None, /, **kwargs: Any) -> None: ...
# 1b. Method mode without arguments: @UniversalDecorator on def method(self, ...)
@overload
def __init__(self, wrapped: Callable[Concatenate[_T, _P], _R], /) -> None: ...
# 1c. Plain function or staticmethod: @UniversalDecorator on def func(...)
@overload
def __init__(self, wrapped: Callable[_P, _R], /) -> None: ...
def __init__(self, wrapped: Callable[..., _R] | None = None, /, **kwargs: Any) -> None:
self.wrapped = wrapped
# 2. Call overloads:
# 2a. Factory execution: decorator(wrapped_func)
@overload
def __call__(self, wrapped: Callable[Concatenate[_T, _P], _R], /) -> "UniversalDecorator[_T, _P, _R]": ...
@overload
def __call__(self, wrapped: Callable[_P, _R], /) -> "UniversalDecorator[Any, _P, _R]": ...
# 2b. Direct execution of plain function: decorated_func(*args, **kwargs)
@overload
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any:
if self.wrapped is None and len(args) == 1 and callable(args[0]):
self.wrapped = args[0]
return self
assert self.wrapped is not None
return self.wrapped(*args, **kwargs)
# 3. Descriptor protocol (__get__) overloads:
# 3a. Accessed via Class or as a Static Method -> returns unbound Callable[_P, _R]
@overload
def __get__(self, instance: None, owner: type[Any] | None = None, /) -> Callable[..., _R]: ...
# 3b. Accessed via Instance -> binds `self`, returning Callable[_P, _R]
@overload
def __get__(self, instance: _T, owner: type[_T] | None = None, /) -> Callable[_P, _R]: ...
def __get__(self, instance: Any, owner: type[Any] | None = None, /) -> Callable[..., _R]:
if instance is None:
# Class access or static method
return self.wrapped # type: ignore[return-value]
def bound_method(*args: _P.args, **kwargs: _P.kwargs) -> _R:
assert self.wrapped is not None
return self.wrapped(instance, *args, **kwargs)
return bound_methodKey Differences:
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I am trying to implement universal class-based decorator (or methods decorator at least). I got typing right for methods and classmethods. I am having troubles with staticmethods and plain functions. I would be grateful if you could help me to get it working for all the cases. The code is available at pyright playground.
All reactions