Skip to content

Commit

Permalink
✨ fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
yezz123 committed Dec 13, 2023
1 parent e4fa5ad commit d01f934
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ repos:
- id: ruff
args:
- --fix
- id: ruff-format
- id: ruff-format
4 changes: 1 addition & 3 deletions fastapi_class/exception/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions fastapi_class/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 6 additions & 12 deletions fastapi_class/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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),
Expand Down
16 changes: 4 additions & 12 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
9 changes: 2 additions & 7 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit d01f934

Please sign in to comment.