From 59626280eba8d3841adb01212e7c37f0ca05377c Mon Sep 17 00:00:00 2001 From: Dark04072006 Date: Sun, 7 Jul 2024 00:54:55 +0300 Subject: [PATCH 1/2] fix type hints --- dataclass_rest/rest.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/dataclass_rest/rest.py b/dataclass_rest/rest.py index 9394811..9481b87 100644 --- a/dataclass_rest/rest.py +++ b/dataclass_rest/rest.py @@ -1,10 +1,12 @@ -from functools import partial -from typing import Any, Dict, Optional, Callable +from typing import Any, Dict, Optional, Callable, ParamSpec, TypeVar from .boundmethod import BoundMethod from .method import Method from .parse_func import parse_func, DEFAULT_BODY_PARAM +_P = ParamSpec("_P") +_RT = TypeVar("_RT") + def rest( url_template: str, @@ -32,8 +34,15 @@ def dec(func: Callable) -> Method: return dec -get = partial(rest, method="GET") -post = partial(rest, method="POST") -put = partial(rest, method="PUT") -patch = partial(rest, method="PATCH") -delete = partial(rest, method="DELETE") +def _rest_method(func: Callable[_P, _RT], method: str) -> Callable[_P, _RT]: + def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _RT: + return func(*args, **kwargs, method=method) + + return wrapper + + +get = _rest_method(rest, method="GET") +post = _rest_method(rest, method="POST") +put = _rest_method(rest, method="PUT") +patch = _rest_method(rest, method="PATCH") +delete = _rest_method(rest, method="DELETE") From e571d3ee6366c09001e7129642e3aab7af2686d5 Mon Sep 17 00:00:00 2001 From: Dark04072006 Date: Fri, 26 Jul 2024 00:38:18 +0300 Subject: [PATCH 2/2] 3.6+ version support --- dataclass_rest/rest.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/dataclass_rest/rest.py b/dataclass_rest/rest.py index 9481b87..21e03b0 100644 --- a/dataclass_rest/rest.py +++ b/dataclass_rest/rest.py @@ -1,21 +1,20 @@ -from typing import Any, Dict, Optional, Callable, ParamSpec, TypeVar +from typing import Any, Callable, Dict, Optional, TypeVar, cast from .boundmethod import BoundMethod from .method import Method -from .parse_func import parse_func, DEFAULT_BODY_PARAM +from .parse_func import DEFAULT_BODY_PARAM, parse_func -_P = ParamSpec("_P") -_RT = TypeVar("_RT") +_Func = TypeVar("_Func", bound=Callable[..., Any]) def rest( - url_template: str, - *, - method: str, - body_name: str = DEFAULT_BODY_PARAM, - additional_params: Optional[Dict[str, Any]] = None, - method_class: Optional[Callable[..., BoundMethod]] = None, - send_json: bool = True, + url_template: str, + *, + method: str, + body_name: str = DEFAULT_BODY_PARAM, + additional_params: Optional[Dict[str, Any]] = None, + method_class: Optional[Callable[..., BoundMethod]] = None, + send_json: bool = True, ) -> Callable[[Callable], Method]: if additional_params is None: additional_params = {} @@ -34,12 +33,12 @@ def dec(func: Callable) -> Method: return dec -def _rest_method(func: Callable[_P, _RT], method: str) -> Callable[_P, _RT]: - def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _RT: +def _rest_method(func: _Func, method: str) -> _Func: + def wrapper(*args, **kwargs): return func(*args, **kwargs, method=method) - return wrapper - + return cast(_Func, wrapper) + get = _rest_method(rest, method="GET") post = _rest_method(rest, method="POST")