forked from orbit-oss/flask
Merge pull request #4579 from lecovi/docs/#4574-test-typing
This commit is contained in:
commit
fb89745408
7 changed files with 138 additions and 31 deletions
|
|
@ -87,7 +87,7 @@ per-file-ignores =
|
|||
src/flask/__init__.py: F401
|
||||
|
||||
[mypy]
|
||||
files = src/flask
|
||||
files = src/flask, tests/typing
|
||||
python_version = 3.7
|
||||
show_error_codes = True
|
||||
allow_redefinition = True
|
||||
|
|
|
|||
|
|
@ -1033,7 +1033,7 @@ class Flask(Scaffold):
|
|||
self,
|
||||
rule: str,
|
||||
endpoint: t.Optional[str] = None,
|
||||
view_func: t.Optional[t.Callable] = None,
|
||||
view_func: t.Optional[ft.ViewCallable] = None,
|
||||
provide_automatic_options: t.Optional[bool] = None,
|
||||
**options: t.Any,
|
||||
) -> None:
|
||||
|
|
@ -1681,7 +1681,7 @@ class Flask(Scaffold):
|
|||
if isinstance(rv[1], (Headers, dict, tuple, list)):
|
||||
rv, headers = rv
|
||||
else:
|
||||
rv, status = rv # type: ignore[misc]
|
||||
rv, status = rv # type: ignore[assignment,misc]
|
||||
# other sized tuples are not allowed
|
||||
else:
|
||||
raise TypeError(
|
||||
|
|
|
|||
|
|
@ -384,7 +384,7 @@ class Blueprint(Scaffold):
|
|||
self,
|
||||
rule: str,
|
||||
endpoint: t.Optional[str] = None,
|
||||
view_func: t.Optional[t.Callable] = None,
|
||||
view_func: t.Optional[ft.ViewCallable] = None,
|
||||
provide_automatic_options: t.Optional[bool] = None,
|
||||
**options: t.Any,
|
||||
) -> None:
|
||||
|
|
@ -561,12 +561,14 @@ class Blueprint(Scaffold):
|
|||
)
|
||||
return f
|
||||
|
||||
def app_errorhandler(self, code: t.Union[t.Type[Exception], int]) -> t.Callable:
|
||||
def app_errorhandler(
|
||||
self, code: t.Union[t.Type[Exception], int]
|
||||
) -> t.Callable[[ft.ErrorHandlerDecorator], ft.ErrorHandlerDecorator]:
|
||||
"""Like :meth:`Flask.errorhandler` but for a blueprint. This
|
||||
handler is used for all requests, even if outside of the blueprint.
|
||||
"""
|
||||
|
||||
def decorator(f: ft.ErrorHandlerCallable) -> ft.ErrorHandlerCallable:
|
||||
def decorator(f: ft.ErrorHandlerDecorator) -> ft.ErrorHandlerDecorator:
|
||||
self.record_once(lambda s: s.app.errorhandler(code)(f))
|
||||
return f
|
||||
|
||||
|
|
|
|||
|
|
@ -363,48 +363,60 @@ class Scaffold:
|
|||
method: str,
|
||||
rule: str,
|
||||
options: dict,
|
||||
) -> t.Callable[[F], F]:
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
if "methods" in options:
|
||||
raise TypeError("Use the 'route' decorator to use the 'methods' argument.")
|
||||
|
||||
return self.route(rule, methods=[method], **options)
|
||||
|
||||
def get(self, rule: str, **options: t.Any) -> t.Callable[[F], F]:
|
||||
def get(
|
||||
self, rule: str, **options: t.Any
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
"""Shortcut for :meth:`route` with ``methods=["GET"]``.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._method_route("GET", rule, options)
|
||||
|
||||
def post(self, rule: str, **options: t.Any) -> t.Callable[[F], F]:
|
||||
def post(
|
||||
self, rule: str, **options: t.Any
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
"""Shortcut for :meth:`route` with ``methods=["POST"]``.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._method_route("POST", rule, options)
|
||||
|
||||
def put(self, rule: str, **options: t.Any) -> t.Callable[[F], F]:
|
||||
def put(
|
||||
self, rule: str, **options: t.Any
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
"""Shortcut for :meth:`route` with ``methods=["PUT"]``.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._method_route("PUT", rule, options)
|
||||
|
||||
def delete(self, rule: str, **options: t.Any) -> t.Callable[[F], F]:
|
||||
def delete(
|
||||
self, rule: str, **options: t.Any
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
"""Shortcut for :meth:`route` with ``methods=["DELETE"]``.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._method_route("DELETE", rule, options)
|
||||
|
||||
def patch(self, rule: str, **options: t.Any) -> t.Callable[[F], F]:
|
||||
def patch(
|
||||
self, rule: str, **options: t.Any
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
"""Shortcut for :meth:`route` with ``methods=["PATCH"]``.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._method_route("PATCH", rule, options)
|
||||
|
||||
def route(self, rule: str, **options: t.Any) -> t.Callable[[F], F]:
|
||||
def route(
|
||||
self, rule: str, **options: t.Any
|
||||
) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]:
|
||||
"""Decorate a view function to register it with the given URL
|
||||
rule and options. Calls :meth:`add_url_rule`, which has more
|
||||
details about the implementation.
|
||||
|
|
@ -428,7 +440,7 @@ class Scaffold:
|
|||
:class:`~werkzeug.routing.Rule` object.
|
||||
"""
|
||||
|
||||
def decorator(f: F) -> F:
|
||||
def decorator(f: ft.RouteDecorator) -> ft.RouteDecorator:
|
||||
endpoint = options.pop("endpoint", None)
|
||||
self.add_url_rule(rule, endpoint, f, **options)
|
||||
return f
|
||||
|
|
@ -440,7 +452,7 @@ class Scaffold:
|
|||
self,
|
||||
rule: str,
|
||||
endpoint: t.Optional[str] = None,
|
||||
view_func: t.Optional[t.Callable] = None,
|
||||
view_func: t.Optional[ft.ViewCallable] = None,
|
||||
provide_automatic_options: t.Optional[bool] = None,
|
||||
**options: t.Any,
|
||||
) -> None:
|
||||
|
|
@ -642,7 +654,7 @@ class Scaffold:
|
|||
@setupmethod
|
||||
def errorhandler(
|
||||
self, code_or_exception: t.Union[t.Type[Exception], int]
|
||||
) -> t.Callable[[ft.ErrorHandlerCallable], ft.ErrorHandlerCallable]:
|
||||
) -> t.Callable[[ft.ErrorHandlerDecorator], ft.ErrorHandlerDecorator]:
|
||||
"""Register a function to handle errors by code or exception class.
|
||||
|
||||
A decorator that is used to register a function given an
|
||||
|
|
@ -672,7 +684,7 @@ class Scaffold:
|
|||
an arbitrary exception
|
||||
"""
|
||||
|
||||
def decorator(f: ft.ErrorHandlerCallable) -> ft.ErrorHandlerCallable:
|
||||
def decorator(f: ft.ErrorHandlerDecorator) -> ft.ErrorHandlerDecorator:
|
||||
self.register_error_handler(code_or_exception, f)
|
||||
return f
|
||||
|
||||
|
|
|
|||
|
|
@ -1,37 +1,30 @@
|
|||
import typing as t
|
||||
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from _typeshed.wsgi import WSGIApplication # noqa: F401
|
||||
from werkzeug.datastructures import Headers # noqa: F401
|
||||
from werkzeug.wrappers import Response # noqa: F401
|
||||
|
||||
# The possible types that are directly convertible or are a Response object.
|
||||
ResponseValue = t.Union[
|
||||
"Response",
|
||||
str,
|
||||
bytes,
|
||||
t.Dict[str, t.Any], # any jsonify-able dict
|
||||
t.Iterator[str],
|
||||
t.Iterator[bytes],
|
||||
]
|
||||
StatusCode = int
|
||||
ResponseValue = t.Union["Response", str, bytes, t.Dict[str, t.Any]]
|
||||
|
||||
# the possible types for an individual HTTP header
|
||||
HeaderName = str
|
||||
# This should be a Union, but mypy doesn't pass unless it's a TypeVar.
|
||||
HeaderValue = t.Union[str, t.List[str], t.Tuple[str, ...]]
|
||||
|
||||
# the possible types for HTTP headers
|
||||
HeadersValue = t.Union[
|
||||
"Headers", t.Dict[HeaderName, HeaderValue], t.List[t.Tuple[HeaderName, HeaderValue]]
|
||||
"Headers",
|
||||
t.Mapping[str, HeaderValue],
|
||||
t.Sequence[t.Tuple[str, HeaderValue]],
|
||||
]
|
||||
|
||||
# The possible types returned by a route function.
|
||||
ResponseReturnValue = t.Union[
|
||||
ResponseValue,
|
||||
t.Tuple[ResponseValue, HeadersValue],
|
||||
t.Tuple[ResponseValue, StatusCode],
|
||||
t.Tuple[ResponseValue, StatusCode, HeadersValue],
|
||||
t.Tuple[ResponseValue, int],
|
||||
t.Tuple[ResponseValue, int, HeadersValue],
|
||||
"WSGIApplication",
|
||||
]
|
||||
|
||||
|
|
@ -51,6 +44,7 @@ TemplateGlobalCallable = t.Callable[..., t.Any]
|
|||
TemplateTestCallable = t.Callable[..., bool]
|
||||
URLDefaultCallable = t.Callable[[str, dict], None]
|
||||
URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]
|
||||
|
||||
# This should take Exception, but that either breaks typing the argument
|
||||
# with a specific exception, or decorating multiple times with different
|
||||
# exceptions (and using a union type on the argument).
|
||||
|
|
@ -58,3 +52,7 @@ URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], N
|
|||
# https://github.com/pallets/flask/issues/4295
|
||||
# https://github.com/pallets/flask/issues/4297
|
||||
ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]
|
||||
ErrorHandlerDecorator = t.TypeVar("ErrorHandlerDecorator", bound=ErrorHandlerCallable)
|
||||
|
||||
ViewCallable = t.Callable[..., ResponseReturnValue]
|
||||
RouteDecorator = t.TypeVar("RouteDecorator", bound=ViewCallable)
|
||||
|
|
|
|||
33
tests/typing/typing_error_handler.py
Normal file
33
tests/typing/typing_error_handler.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from http import HTTPStatus
|
||||
|
||||
from werkzeug.exceptions import BadRequest
|
||||
from werkzeug.exceptions import NotFound
|
||||
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.errorhandler(400)
|
||||
@app.errorhandler(HTTPStatus.BAD_REQUEST)
|
||||
@app.errorhandler(BadRequest)
|
||||
def handle_400(e: BadRequest) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
@app.errorhandler(ValueError)
|
||||
def handle_custom(e: ValueError) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
@app.errorhandler(ValueError)
|
||||
def handle_accept_base(e: Exception) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
@app.errorhandler(BadRequest)
|
||||
@app.errorhandler(404)
|
||||
def handle_multiple(e: BadRequest | NotFound) -> str:
|
||||
return ""
|
||||
62
tests/typing/typing_route.py
Normal file
62
tests/typing/typing_route.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from http import HTTPStatus
|
||||
|
||||
from flask import Flask
|
||||
from flask import jsonify
|
||||
from flask.templating import render_template
|
||||
from flask.views import View
|
||||
from flask.wrappers import Response
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/str")
|
||||
def hello_str() -> str:
|
||||
return "<p>Hello, World!</p>"
|
||||
|
||||
|
||||
@app.route("/bytes")
|
||||
def hello_bytes() -> bytes:
|
||||
return b"<p>Hello, World!</p>"
|
||||
|
||||
|
||||
@app.route("/json")
|
||||
def hello_json() -> Response:
|
||||
return jsonify({"response": "Hello, World!"})
|
||||
|
||||
|
||||
@app.route("/status")
|
||||
@app.route("/status/<int:code>")
|
||||
def tuple_status(code: int = 200) -> tuple[str, int]:
|
||||
return "hello", code
|
||||
|
||||
|
||||
@app.route("/status-enum")
|
||||
def tuple_status_enum() -> tuple[str, int]:
|
||||
return "hello", HTTPStatus.OK
|
||||
|
||||
|
||||
@app.route("/headers")
|
||||
def tuple_headers() -> tuple[str, dict[str, str]]:
|
||||
return "Hello, World!", {"Content-Type": "text/plain"}
|
||||
|
||||
|
||||
@app.route("/template")
|
||||
@app.route("/template/<name>")
|
||||
def return_template(name: str | None = None) -> str:
|
||||
return render_template("index.html", name=name)
|
||||
|
||||
|
||||
class RenderTemplateView(View):
|
||||
def __init__(self: RenderTemplateView, template_name: str) -> None:
|
||||
self.template_name = template_name
|
||||
|
||||
def dispatch_request(self: RenderTemplateView) -> str:
|
||||
return render_template(self.template_name)
|
||||
|
||||
|
||||
app.add_url_rule(
|
||||
"/about",
|
||||
view_func=RenderTemplateView.as_view("about_page", template_name="about.html"),
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue