Skip to content
Myr

Merge pull request #4695 from zr40/allow-typeddict-as-responsevalue

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Allow TypedDict as a ResponseValue

authored by David Lord · committed by GitHub · parents 76a6b4f, 4bf7415 · Browse files · 3 files+15 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+15 added−1 removed

  • tests:+11 added−0 removed
  • other:+4 added−1 removed

3 files+15 added−1 removed

modified:src/flask/typing.py+2 added-1 removed
9 unmodified lines
10 "Response",11 str,12 bytes,13 t.List[t.Any],14 # Only dict is actually accepted, but Mapping allows for TypedDict.14 t.Dict[str, t.Any],15 t.Mapping[str, t.Any],16 t.Iterator[str],17 t.Iterator[bytes],18]19
61 unmodified lines
modified:tests/typing/typing_route.py+11 added
1 unmodified line
23import typing as t4from http import HTTPStatus56import typing_extensions as te78from flask import Flask9from flask import jsonify10from flask import stream_template11from flask.templating import render_template
27 unmodified lines
39def hello_json_list() -> t.List[t.Any]:40 return [{"message": "Hello"}, {"message": "World"}]414243class StatusJSON(te.TypedDict):44 status: str454647@app.route("/typed-dict")48def typed_dict() -> StatusJSON:49 return {"status": "ok"}505152@app.route("/generator")53def hello_generator() -> t.Generator[str, None, None]:54 def show() -> t.Generator[str, None, None]:55 for x in range(100):
59 unmodified lines
modified:CHANGES.rst+2 added
89 unmodified lines
90 ``response.get_data()`` or ``response.close()`` is called.9192- Allow returning a list from a view function, to convert it to a93 JSON response like a dict is. :issue:`4672`94- When type checking, allow ``TypedDict`` to be returned from view95 functions. :pr:`4695`969798Version 2.1.399-------------
1294 unmodified lines