Skip to content

Commit 6ba5dc7

Browse files
committed
chore: support JSONRepsonse dumps callable return type bytes
the `dumps` parameter is used to serialize the `JSONResponse` to text. this is often done with python's built in `json.dumps` but other times via `orjson.dumps` however, to use `orjson.dumps`, you have to ignore the suggested type (`str`) because `orjson.dumps` returns a `bytes` to correct this, the typing for the dumps callable has been switched to be an `AnyStr` so either `bytes` or `str` returning callable can be passed
1 parent da1c646 commit 6ba5dc7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

sanic/response/convenience.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def json(
3838
status: int = 200,
3939
headers: Optional[Dict[str, str]] = None,
4040
content_type: str = "application/json",
41-
dumps: Optional[Callable[..., str]] = None,
41+
dumps: Optional[Callable[..., AnyStr]] = None,
4242
**kwargs: Any,
4343
) -> JSONResponse:
4444
"""Returns response object with body in json format.
@@ -48,7 +48,7 @@ def json(
4848
status (int, optional): HTTP response code. Defaults to `200`.
4949
headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.
5050
content_type (str, optional): The content type (string) of the response. Defaults to `"application/json"`.
51-
dumps (Callable[..., str], optional): A custom json dumps function. Defaults to `None`.
51+
dumps (Callable[..., AnyStr], optional): A custom json dumps function. Defaults to `None`.
5252
**kwargs (Any): Remaining arguments that are passed to the json encoder.
5353
5454
Returns:

sanic/response/types.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class JSONResponse(HTTPResponse):
264264
status (int, optional): HTTP response number. Defaults to `200`.
265265
headers (Optional[Union[Header, Dict[str, str]]], optional): Headers to be returned. Defaults to `None`.
266266
content_type (str, optional): Content type to be returned (as a header). Defaults to `"application/json"`.
267-
dumps (Optional[Callable[..., str]], optional): The function to use for json encoding. Defaults to `None`.
267+
dumps (Optional[Callable[..., AnyStr]], optional): The function to use for json encoding. Defaults to `None`.
268268
**kwargs (Any, optional): The kwargs to pass to the json encoding function. Defaults to `{}`.
269269
""" # noqa: E501
270270

@@ -283,13 +283,15 @@ def __init__(
283283
status: int = 200,
284284
headers: Optional[Union[Header, Dict[str, str]]] = None,
285285
content_type: str = "application/json",
286-
dumps: Optional[Callable[..., str]] = None,
286+
dumps: Optional[Callable[..., AnyStr]] = None,
287287
**kwargs: Any,
288288
):
289289
self._initialized = False
290290
self._body_manually_set = False
291291

292-
self._use_dumps = dumps or BaseHTTPResponse._dumps
292+
self._use_dumps: Callable[..., str | bytes] = (
293+
dumps or BaseHTTPResponse._dumps
294+
)
293295
self._use_dumps_kwargs = kwargs
294296

295297
self._raw_body = body

0 commit comments

Comments
 (0)