Skip to content

Commit

Permalink
Fix coerce in Hint (#251)
Browse files Browse the repository at this point in the history
* Fix nullable type null value being converted

* Unpin setuptools version

* Update test

* make format
  • Loading branch information
ChihweiLHBird authored Dec 31, 2024
1 parent 7ab93d9 commit fd85ada
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools<60.0", "wheel"]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.ruff]
Expand Down
6 changes: 2 additions & 4 deletions sanic_ext/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ class ValidationError(SanicException):
status_code = 400


class InitError(SanicException):
...
class InitError(SanicException): ...


class ExtensionNotFound(SanicException):
...
class ExtensionNotFound(SanicException): ...
3 changes: 1 addition & 2 deletions sanic_ext/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def _startup(self, bootstrap):
self._started = True

@abstractmethod
def startup(self, bootstrap) -> None:
...
def startup(self, bootstrap) -> None: ...

def label(self):
return ""
Expand Down
3 changes: 1 addition & 2 deletions sanic_ext/extensions/health/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from sanic import Sanic


class Stale(ValueError):
...
class Stale(ValueError): ...


@dataclass
Expand Down
6 changes: 3 additions & 3 deletions sanic_ext/extensions/openapi/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ def build_spec(app, loop):
):
operation.autodoc(docstring)

operation._default[
"operationId"
] = f"{method.lower()}~{route_name}"
operation._default["operationId"] = (
f"{method.lower()}~{route_name}"
)
operation._default["summary"] = clean_route_name(route_name)

if host:
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extensions/openapi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
I.e., the objects described https://swagger.io/docs/specification
"""

from __future__ import annotations

from inspect import isclass
Expand Down
16 changes: 6 additions & 10 deletions sanic_ext/extensions/openapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
documentation to OperationStore() and components created in the blueprints.
"""

from functools import wraps
from inspect import isawaitable, isclass
from typing import (
Expand Down Expand Up @@ -94,13 +95,11 @@ def _content_or_component(content):


@overload
def exclude(flag: bool = True, *, bp: Blueprint) -> None:
...
def exclude(flag: bool = True, *, bp: Blueprint) -> None: ...


@overload
def exclude(flag: bool = True) -> Callable:
...
def exclude(flag: bool = True) -> Callable: ...


def exclude(flag: bool = True, *, bp: Optional[Blueprint] = None):
Expand Down Expand Up @@ -247,8 +246,7 @@ def parameter(
*,
parameter: definitions.Parameter,
**kwargs,
) -> Callable[[T], T]:
...
) -> Callable[[T], T]: ...


@overload
Expand All @@ -258,8 +256,7 @@ def parameter(
location: None,
parameter: definitions.Parameter,
**kwargs,
) -> Callable[[T], T]:
...
) -> Callable[[T], T]: ...


@overload
Expand All @@ -269,8 +266,7 @@ def parameter(
location: Optional[str] = None,
parameter: None = None,
**kwargs,
) -> Callable[[T], T]:
...
) -> Callable[[T], T]: ...


def parameter(
Expand Down
3 changes: 1 addition & 2 deletions sanic_ext/extensions/templating/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from jinja2 import Environment


class TemplateResponse(HTTPResponse):
...
class TemplateResponse(HTTPResponse): ...


class LazyResponse(TemplateResponse):
Expand Down
2 changes: 2 additions & 0 deletions sanic_ext/extras/validation/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def coerce(self, value):
try:
if isinstance(value, list):
value = [coerce_type(item) for item in value]
elif value is None and self.nullable:
value = None
else:
value = coerce_type(value)
except (ValueError, TypeError):
Expand Down
5 changes: 5 additions & 0 deletions tests/extra/test_validation_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def test_validate_form(app):
class Pet:
name: str
alter_ego: List[str]
description: Optional[str] = None

@app.post("/function")
@validate(form=Pet)
Expand All @@ -348,6 +349,7 @@ async def handler(_, body: Pet):
{
"is_pet": isinstance(body, Pet),
"pet": {"name": body.name, "alter_ego": body.alter_ego},
"description": body.description if body.description else "",
}
)

Expand All @@ -359,18 +361,21 @@ async def post(self, _, body: Pet):
{
"is_pet": isinstance(body, Pet),
"pet": {"name": body.name, "alter_ego": body.alter_ego},
"description": body.description if body.description else "",
}
)

_, response = app.test_client.post("/function", data=SNOOPY_DATA)
assert response.status == 200
assert response.json["is_pet"]
assert response.json["pet"] == SNOOPY_DATA
assert response.json["description"] == ""

_, response = app.test_client.post("/method", data=SNOOPY_DATA)
assert response.status == 200
assert response.json["is_pet"]
assert response.json["pet"] == SNOOPY_DATA
assert response.json["description"] == ""


def test_validate_query(app):
Expand Down

0 comments on commit fd85ada

Please sign in to comment.