From eac40e73a3cc3f604a610667e447b955a54131e6 Mon Sep 17 00:00:00 2001 From: darwintree <17946284+darwintree@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:37:43 +0800 Subject: [PATCH] Improve combomethod type hints --- eth_utils/decorators.py | 19 +++++++++++++------ newsfragments/236.feature.rst | 1 + setup.py | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 newsfragments/236.feature.rst diff --git a/eth_utils/decorators.py b/eth_utils/decorators.py index b2cc8625..3b979237 100644 --- a/eth_utils/decorators.py +++ b/eth_utils/decorators.py @@ -4,9 +4,15 @@ Any, Callable, Dict, - Optional, + Generic, Type, TypeVar, + Union, +) + +from typing_extensions import ( + Concatenate, + ParamSpec, ) from .types import ( @@ -14,17 +20,18 @@ ) T = TypeVar("T") +P = ParamSpec("P") -class combomethod: - def __init__(self, method: Callable[..., Any]) -> None: +class combomethod(Generic[P, T]): + def __init__(self, method: Callable[Concatenate[Any, P], T]) -> None: self.method = method def __get__( - self, obj: Optional[T] = None, objtype: Optional[Type[T]] = None - ) -> Callable[..., Any]: + self, obj: object = None, objtype: Union[type, None] = None + ) -> Callable[P, T]: @functools.wraps(self.method) - def _wrapper(*args: Any, **kwargs: Any) -> Any: + def _wrapper(*args: P.args, **kwargs: P.kwargs) -> T: if obj is not None: return self.method(obj, *args, **kwargs) else: diff --git a/newsfragments/236.feature.rst b/newsfragments/236.feature.rst new file mode 100644 index 00000000..58ff3c81 --- /dev/null +++ b/newsfragments/236.feature.rst @@ -0,0 +1 @@ +Improve type hints for decorator combomethod. diff --git a/setup.py b/setup.py index 66fff890..cba6f562 100644 --- a/setup.py +++ b/setup.py @@ -52,6 +52,7 @@ include_package_data=True, install_requires=[ "cached-property>=1.5.2,<2;python_version<'3.8'", + "typing_extensions", "eth-hash>=0.3.1", "eth-typing>=3.0.0", "toolz>0.8.2;implementation_name=='pypy'",