add errorhandler type check tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 81be290 · Browse files · 4 files·+40 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+40 added−4 removed
- tests:+33 added−0 removed
- other:+7 added−4 removed
Provenance
- Commit
3351a86- Tree
6f6a074- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
81be290
4 files changed+40 added−4 removed
modified:src/flask/blueprints.py+4 added-2 removed
559 unmodified lines560 lambda s: s.app.template_context_processors.setdefault(None, []).append(f)561 )562 return f563 564- def app_errorhandler(self, code: t.Union[t.Type[Exception], int]) -> t.Callable:564+ def app_errorhandler(565+ self, code: t.Union[t.Type[Exception], int]566+ ) -> t.Callable[[ft.ErrorHandlerDecorator], ft.ErrorHandlerDecorator]:567 """Like :meth:`Flask.errorhandler` but for a blueprint. This568 handler is used for all requests, even if outside of the blueprint.569 """570 569- def decorator(f: ft.ErrorHandlerCallable) -> ft.ErrorHandlerCallable:571+ def decorator(f: ft.ErrorHandlerDecorator) -> ft.ErrorHandlerDecorator:572 self.record_once(lambda s: s.app.errorhandler(code)(f))573 return f574 575 return decorator16 unmodified linesmodified:src/flask/scaffold.py+2 added-2 removed
652 unmodified lines653 654 @setupmethod655 def errorhandler(656 self, code_or_exception: t.Union[t.Type[Exception], int]657- ) -> t.Callable[[ft.ErrorHandlerCallable], ft.ErrorHandlerCallable]:657+ ) -> t.Callable[[ft.ErrorHandlerDecorator], ft.ErrorHandlerDecorator]:658 """Register a function to handle errors by code or exception class.659 660 A decorator that is used to register a function given an661 error code. Example::21 unmodified lines683 :param code_or_exception: the code as integer for the handler, or684 an arbitrary exception685 """686 687- def decorator(f: ft.ErrorHandlerCallable) -> ft.ErrorHandlerCallable:687+ def decorator(f: ft.ErrorHandlerDecorator) -> ft.ErrorHandlerDecorator:688 self.register_error_handler(code_or_exception, f)689 return f690 691 return decorator183 unmodified linesmodified:src/flask/typing.py+1 added
50 unmodified lines51 # https://github.com/pallets/flask/issues/409552 # https://github.com/pallets/flask/issues/429553 # https://github.com/pallets/flask/issues/429754 ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]55+ErrorHandlerDecorator = t.TypeVar("ErrorHandlerDecorator", bound=ErrorHandlerCallable)56 57 ViewCallable = t.Callable[..., ResponseReturnValue]58 RouteDecorator = t.TypeVar("RouteDecorator", bound=ViewCallable)added:tests/typing/typing_error_handler.py+33 added
1+from __future__ import annotations2+3+from http import HTTPStatus4+5+from werkzeug.exceptions import BadRequest6+from werkzeug.exceptions import NotFound7+8+from flask import Flask9+10+app = Flask(__name__)11+12+13+@app.errorhandler(400)14+@app.errorhandler(HTTPStatus.BAD_REQUEST)15+@app.errorhandler(BadRequest)16+def handle_400(e: BadRequest) -> str:17+ return ""18+19+20+@app.errorhandler(ValueError)21+def handle_custom(e: ValueError) -> str:22+ return ""23+24+25+@app.errorhandler(ValueError)26+def handle_accept_base(e: Exception) -> str:27+ return ""28+29+30+@app.errorhandler(BadRequest)31+@app.errorhandler(404)32+def handle_multiple(e: BadRequest | NotFound) -> str:33+ return ""