Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type hints #37

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions dataclass_rest/rest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from functools import partial
from typing import Any, Dict, Optional, Callable
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

_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 = {}
Expand All @@ -32,8 +33,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: _Func, method: str) -> _Func:
def wrapper(*args, **kwargs):
return func(*args, **kwargs, method=method)

return cast(_Func, 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")