forked from orbit-oss/flask
allow TypedDict as a response value
This commit is contained in:
parent
76a6b4f2b5
commit
4bf7415a96
3 changed files with 15 additions and 1 deletions
|
|
@ -91,6 +91,8 @@ Unreleased
|
||||||
|
|
||||||
- Allow returning a list from a view function, to convert it to a
|
- Allow returning a list from a view function, to convert it to a
|
||||||
JSON response like a dict is. :issue:`4672`
|
JSON response like a dict is. :issue:`4672`
|
||||||
|
- When type checking, allow ``TypedDict`` to be returned from view
|
||||||
|
functions. :pr:`4695`
|
||||||
|
|
||||||
|
|
||||||
Version 2.1.3
|
Version 2.1.3
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ ResponseValue = t.Union[
|
||||||
str,
|
str,
|
||||||
bytes,
|
bytes,
|
||||||
t.List[t.Any],
|
t.List[t.Any],
|
||||||
t.Dict[str, t.Any],
|
# Only dict is actually accepted, but Mapping allows for TypedDict.
|
||||||
|
t.Mapping[str, t.Any],
|
||||||
t.Iterator[str],
|
t.Iterator[str],
|
||||||
t.Iterator[bytes],
|
t.Iterator[bytes],
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ from __future__ import annotations
|
||||||
import typing as t
|
import typing as t
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
|
import typing_extensions as te
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from flask import stream_template
|
from flask import stream_template
|
||||||
|
|
@ -38,6 +40,15 @@ def hello_json_list() -> t.List[t.Any]:
|
||||||
return [{"message": "Hello"}, {"message": "World"}]
|
return [{"message": "Hello"}, {"message": "World"}]
|
||||||
|
|
||||||
|
|
||||||
|
class StatusJSON(te.TypedDict):
|
||||||
|
status: str
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/typed-dict")
|
||||||
|
def typed_dict() -> StatusJSON:
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/generator")
|
@app.route("/generator")
|
||||||
def hello_generator() -> t.Generator[str, None, None]:
|
def hello_generator() -> t.Generator[str, None, None]:
|
||||||
def show() -> t.Generator[str, None, None]:
|
def show() -> t.Generator[str, None, None]:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue