Skip to content

Commit fd85ada

Browse files
Fix coerce in Hint (#251)
* Fix nullable type null value being converted * Unpin setuptools version * Update test * make format
1 parent 7ab93d9 commit fd85ada

File tree

10 files changed

+23
-24
lines changed

10 files changed

+23
-24
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools<60.0", "wheel"]
2+
requires = ["setuptools", "wheel"]
33
build-backend = "setuptools.build_meta"
44

55
[tool.ruff]

sanic_ext/exceptions.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ class ValidationError(SanicException):
55
status_code = 400
66

77

8-
class InitError(SanicException):
9-
...
8+
class InitError(SanicException): ...
109

1110

12-
class ExtensionNotFound(SanicException):
13-
...
11+
class ExtensionNotFound(SanicException): ...

sanic_ext/extensions/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ def _startup(self, bootstrap):
4646
self._started = True
4747

4848
@abstractmethod
49-
def startup(self, bootstrap) -> None:
50-
...
49+
def startup(self, bootstrap) -> None: ...
5150

5251
def label(self):
5352
return ""

sanic_ext/extensions/health/monitor.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from sanic import Sanic
1818

1919

20-
class Stale(ValueError):
21-
...
20+
class Stale(ValueError): ...
2221

2322

2423
@dataclass

sanic_ext/extensions/openapi/blueprint.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ def build_spec(app, loop):
173173
):
174174
operation.autodoc(docstring)
175175

176-
operation._default[
177-
"operationId"
178-
] = f"{method.lower()}~{route_name}"
176+
operation._default["operationId"] = (
177+
f"{method.lower()}~{route_name}"
178+
)
179179
operation._default["summary"] = clean_route_name(route_name)
180180

181181
if host:

sanic_ext/extensions/openapi/definitions.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
I.e., the objects described https://swagger.io/docs/specification
55
66
"""
7+
78
from __future__ import annotations
89

910
from inspect import isclass

sanic_ext/extensions/openapi/openapi.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
documentation to OperationStore() and components created in the blueprints.
44
55
"""
6+
67
from functools import wraps
78
from inspect import isawaitable, isclass
89
from typing import (
@@ -94,13 +95,11 @@ def _content_or_component(content):
9495

9596

9697
@overload
97-
def exclude(flag: bool = True, *, bp: Blueprint) -> None:
98-
...
98+
def exclude(flag: bool = True, *, bp: Blueprint) -> None: ...
9999

100100

101101
@overload
102-
def exclude(flag: bool = True) -> Callable:
103-
...
102+
def exclude(flag: bool = True) -> Callable: ...
104103

105104

106105
def exclude(flag: bool = True, *, bp: Optional[Blueprint] = None):
@@ -247,8 +246,7 @@ def parameter(
247246
*,
248247
parameter: definitions.Parameter,
249248
**kwargs,
250-
) -> Callable[[T], T]:
251-
...
249+
) -> Callable[[T], T]: ...
252250

253251

254252
@overload
@@ -258,8 +256,7 @@ def parameter(
258256
location: None,
259257
parameter: definitions.Parameter,
260258
**kwargs,
261-
) -> Callable[[T], T]:
262-
...
259+
) -> Callable[[T], T]: ...
263260

264261

265262
@overload
@@ -269,8 +266,7 @@ def parameter(
269266
location: Optional[str] = None,
270267
parameter: None = None,
271268
**kwargs,
272-
) -> Callable[[T], T]:
273-
...
269+
) -> Callable[[T], T]: ...
274270

275271

276272
def parameter(

sanic_ext/extensions/templating/render.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from jinja2 import Environment
1717

1818

19-
class TemplateResponse(HTTPResponse):
20-
...
19+
class TemplateResponse(HTTPResponse): ...
2120

2221

2322
class LazyResponse(TemplateResponse):

sanic_ext/extras/validation/check.py

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def coerce(self, value):
141141
try:
142142
if isinstance(value, list):
143143
value = [coerce_type(item) for item in value]
144+
elif value is None and self.nullable:
145+
value = None
144146
else:
145147
value = coerce_type(value)
146148
except (ValueError, TypeError):

tests/extra/test_validation_dataclass.py

+5
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def test_validate_form(app):
340340
class Pet:
341341
name: str
342342
alter_ego: List[str]
343+
description: Optional[str] = None
343344

344345
@app.post("/function")
345346
@validate(form=Pet)
@@ -348,6 +349,7 @@ async def handler(_, body: Pet):
348349
{
349350
"is_pet": isinstance(body, Pet),
350351
"pet": {"name": body.name, "alter_ego": body.alter_ego},
352+
"description": body.description if body.description else "",
351353
}
352354
)
353355

@@ -359,18 +361,21 @@ async def post(self, _, body: Pet):
359361
{
360362
"is_pet": isinstance(body, Pet),
361363
"pet": {"name": body.name, "alter_ego": body.alter_ego},
364+
"description": body.description if body.description else "",
362365
}
363366
)
364367

365368
_, response = app.test_client.post("/function", data=SNOOPY_DATA)
366369
assert response.status == 200
367370
assert response.json["is_pet"]
368371
assert response.json["pet"] == SNOOPY_DATA
372+
assert response.json["description"] == ""
369373

370374
_, response = app.test_client.post("/method", data=SNOOPY_DATA)
371375
assert response.status == 200
372376
assert response.json["is_pet"]
373377
assert response.json["pet"] == SNOOPY_DATA
378+
assert response.json["description"] == ""
374379

375380

376381
def test_validate_query(app):

0 commit comments

Comments
 (0)