From 492977cff72ede52acbca7603ce8d8b7e71554a9 Mon Sep 17 00:00:00 2001 From: sh1ma Date: Thu, 17 Sep 2020 09:12:35 +0900 Subject: [PATCH] pydantic --- pyproject.toml | 2 +- src/apywrapper/_request.py | 6 +++--- src/apywrapper/_types.py | 11 +---------- src/apywrapper/_utils.py | 22 ++++++++++------------ tests/test_utils.py | 6 +++--- 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1d377cd..0df3699 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "apywrapper" -version = "0.0.0" +version = "0.0.0.post123.dev0+96fa6fa" description = "make wrapper for RESTful API" license = "GPL-3.0-or-later" authors = ["sh1ma "] diff --git a/src/apywrapper/_request.py b/src/apywrapper/_request.py index 7590b2f..b45c540 100644 --- a/src/apywrapper/_request.py +++ b/src/apywrapper/_request.py @@ -45,12 +45,12 @@ def wrapper( params = func(*args, **kwargs) path = Path(path_str, params) response = request_func(path, params) - if ( + if hook_func: + return hook_func(entity, response) + elif ( entity is None or response.status_code == 204 ): # entity is None or response body is None return None - if hook_func: - return hook_func(entity, response) elif serialize_func: response.raise_for_status() return serialize_func(entity, response.json()) diff --git a/src/apywrapper/_types.py b/src/apywrapper/_types.py index c357908..4b9a838 100644 --- a/src/apywrapper/_types.py +++ b/src/apywrapper/_types.py @@ -8,15 +8,6 @@ from httpx import Response -class DataclassEntityType(Protocol): - # pylint: disable=too-few-public-methods - """ - Entity Object Type on dataclass - """ - - __dataclass_fields__: Dict - - class PydanticEntityType(Protocol): """ Entity Object Type on Pydantic @@ -25,7 +16,7 @@ class PydanticEntityType(Protocol): __fields__: Dict -EntityType = Union[DataclassEntityType, PydanticEntityType] +EntityType = PydanticEntityType Entity = Optional[Union[List[EntityType], EntityType]] diff --git a/src/apywrapper/_utils.py b/src/apywrapper/_utils.py index 850bc5c..0e85757 100644 --- a/src/apywrapper/_utils.py +++ b/src/apywrapper/_utils.py @@ -3,21 +3,19 @@ """ from inspect import signature -from typing import Any, Optional, Type, cast, get_args, get_origin +from typing import Any -from ._types import ApiFunc, EntityType +from ._types import ApiFunc -def get_returntype_from_annotation(func: ApiFunc) -> Optional[Type[EntityType]]: +def get_returntype_from_annotation(func: ApiFunc) -> Any: return_annotation = signature(func).return_annotation - if return_annotation is None: - return None - return resolve_returntype(return_annotation) + return return_annotation -def resolve_returntype(tp: Any) -> Type[EntityType]: - if (tp_origin := get_origin(tp)) is not None: - if tp_origin is not list: - return cast(Type[EntityType], get_origin(tp)) - return resolve_returntype(get_args(tp)[0]) - return cast(Type[EntityType], tp) +# def resolve_returntype(tp: Any) -> Type[EntityType]: +# if (tp_origin := get_origin(tp)) is not None: +# if tp_origin is not list: +# return cast(Type[EntityType], get_origin(tp)) +# return resolve_returntype(get_args(tp)[0]) +# return cast(Type[EntityType], tp) diff --git a/tests/test_utils.py b/tests/test_utils.py index c5caaf5..a5fe903 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,8 +21,8 @@ def test4() -> List[Union[str, int]]: def test5() -> str: pass - assert get_returntype_from_annotation(test1) is str - assert get_returntype_from_annotation(test2) is dict + assert get_returntype_from_annotation(test1) is List[str] + assert get_returntype_from_annotation(test2) is Dict[str, str] assert get_returntype_from_annotation(test3) is None - assert get_returntype_from_annotation(test4) is Union + assert get_returntype_from_annotation(test4) is List[Union[str, int]] assert get_returntype_from_annotation(test5) is str