From d01f934a1fdf09b833e5f20cadf247f3f2860bd7 Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Wed, 13 Dec 2023 22:28:49 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20fix=20formatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pre-commit-config.yaml | 2 +- fastapi_class/exception/handler.py | 4 +--- fastapi_class/routers.py | 6 +----- fastapi_class/views.py | 18 ++++++------------ tests/test_openapi.py | 16 ++++------------ tests/test_views.py | 9 ++------- 6 files changed, 15 insertions(+), 40 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1bb8ae2..6201f61 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,4 +15,4 @@ repos: - id: ruff args: - --fix - - id: ruff-format \ No newline at end of file + - id: ruff-format diff --git a/fastapi_class/exception/handler.py b/fastapi_class/exception/handler.py index 2f0500c..08de3cc 100644 --- a/fastapi_class/exception/handler.py +++ b/fastapi_class/exception/handler.py @@ -13,9 +13,7 @@ class ExceptionAbstract(ABC): _DEFAULT_DETAIL_SPECIAL_NAME = "__detail__" def __init__(self, *, exceptions: Iterable[tuple[int, str]] | None = None) -> None: - self._exceptions = exceptions or [ - (status.HTTP_500_INTERNAL_SERVER_ERROR, UNKOWN_SERVER_ERROR_DETAIL) - ] + self._exceptions = exceptions or [(status.HTTP_500_INTERNAL_SERVER_ERROR, UNKOWN_SERVER_ERROR_DETAIL)] @classmethod @abstractmethod diff --git a/fastapi_class/routers.py b/fastapi_class/routers.py index d47f776..c7fcdfc 100644 --- a/fastapi_class/routers.py +++ b/fastapi_class/routers.py @@ -90,11 +90,7 @@ async def _wrapper(*args, **kwargs): return await function(*args, **kwargs) parsed_method = set() - _methods = ( - (methods,) - if isinstance(methods, str) - else methods or ((name,) if name else (function.__name__,)) - ) + _methods = (methods,) if isinstance(methods, str) else methods or ((name,) if name else (function.__name__,)) for method in _methods: if isinstance(method, Method): parsed_method.add(method) diff --git a/fastapi_class/views.py b/fastapi_class/views.py index 15ee6ed..ab1edd9 100644 --- a/fastapi_class/views.py +++ b/fastapi_class/views.py @@ -57,22 +57,18 @@ def _decorator(cls) -> None: obj = cls() cls_based_response_model = getattr(obj, RESPONSE_MODEL_ATTRIBUTE_NAME, {}) cls_based_response_class = getattr(obj, RESPONSE_CLASS_ATTRIBUTE_NAME, {}) - common_exceptions = getattr(obj, EXCEPTIONS_ATTRIBUTE_NAME, {}).get( - COMMON_KEYWORD, () - ) + common_exceptions = getattr(obj, EXCEPTIONS_ATTRIBUTE_NAME, {}).get(COMMON_KEYWORD, ()) for _callable_name in dir(obj): _callable = getattr(obj, _callable_name) - if _callable_name in set(Method) or hasattr( - _callable, ENDPOINT_METADATA_ATTRIBUTE_NAME - ): + if _callable_name in set(Method) or hasattr(_callable, ENDPOINT_METADATA_ATTRIBUTE_NAME): metadata: Metadata = getattr( _callable, ENDPOINT_METADATA_ATTRIBUTE_NAME, Metadata([_callable_name]), ) - exceptions: Iterable[HTTPException] = getattr( - obj, ENDPOINT_METADATA_ATTRIBUTE_NAME, {} - ).get(_callable_name, []) + exceptions: Iterable[HTTPException] = getattr(obj, ENDPOINT_METADATA_ATTRIBUTE_NAME, {}).get( + _callable_name, [] + ) exceptions += common_exceptions _path = path if metadata and metadata.path: @@ -84,9 +80,7 @@ def _decorator(cls) -> None: response_class=metadata.response_class_or_default( cls_based_response_class.get(_callable_name, JSONResponse) ), - response_model=metadata.response_model_or_default( - cls_based_response_model.get(_callable_name) - ), + response_model=metadata.response_model_or_default(cls_based_response_model.get(_callable_name)), responses=_exceptions_to_responses(exceptions), name=metadata.name_or_default(name_parser(cls, _callable_name)), status_code=metadata.status_code_or_default(default_status_code), diff --git a/tests/test_openapi.py b/tests/test_openapi.py index cf7758b..519dbb6 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -17,30 +17,22 @@ def test_exceptions_to_responses__function_exception_factory(): def test_exceptions_to_responses__mixed(): - assert _exceptions_to_responses( - {lambda: HTTPException(404, "test"), HTTPException(400, "test1")} - ) == { + assert _exceptions_to_responses({lambda: HTTPException(404, "test"), HTTPException(400, "test1")}) == { 400: {"description": "test1", "model": ExceptionModel}, 404: {"description": "test", "model": ExceptionModel}, } def test_exceptions_to_responses__collision_on_status_code(): - assert _exceptions_to_responses( - [lambda: HTTPException(400, "lambda"), HTTPException(400, "exc")] - ) == { + assert _exceptions_to_responses([lambda: HTTPException(400, "lambda"), HTTPException(400, "exc")]) == { 400: {"description": "lambda or exc", "model": ExceptionModel}, } def test_exceptions_to_response__broad_exception(): - assert _exceptions_to_responses({Exception()}) == { - 400: {"description": "", "model": ExceptionModel} - } + assert _exceptions_to_responses({Exception()}) == {400: {"description": "", "model": ExceptionModel}} @pytest.mark.parametrize("data", ("test", 5, 3.14, (1, 2, 3))) def test_exception_to_response__random_data(data): - assert _exceptions_to_responses({data}) == { - 400: {"description": str(data), "model": ExceptionModel} - } + assert _exceptions_to_responses({data}) == {400: {"description": str(data), "model": ExceptionModel}} diff --git a/tests/test_views.py b/tests/test_views.py index f19b1af..e806aeb 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -55,9 +55,7 @@ def dummy(self): def dummy(self): ... - data[_endpoint.get("alternative_name") or dummy.__name__] = _endpoint[ - "decorator" - ](dummy) + data[_endpoint.get("alternative_name") or dummy.__name__] = _endpoint["decorator"](dummy) class_base_view = type(name, (object,), data) return class_base_view @@ -77,9 +75,6 @@ def test_view__use_name_of_functions_as_methods( for method in Method: assert "/" in schema["paths"] assert method.value in schema["paths"]["/"] - assert ( - schema["paths"]["/"][method.value]["summary"] - == f"{method.value.capitalize()} Test Class Based" - ) + assert schema["paths"]["/"][method.value]["summary"] == f"{method.value.capitalize()} Test Class Based" responses = schema["paths"]["/"][method.value]["responses"] assert "200" in responses