Skip to content
Myr

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

4 files+40 added−4 removed

modified:src/flask/blueprints.py+4 added-2 removed
559 unmodified lines
560 lambda s: s.app.template_context_processors.setdefault(None, []).append(f)561 )562 return f563564 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 """570569 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 f574575 return decorator
16 unmodified lines
modified:src/flask/scaffold.py+2 added-2 removed
652 unmodified lines
653654 @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.659660 A decorator that is used to register a function given an661 error code. Example::
21 unmodified lines
683 :param code_or_exception: the code as integer for the handler, or684 an arbitrary exception685 """686687 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 f690691 return decorator
183 unmodified lines
modified:src/flask/typing.py+1 added
50 unmodified lines
51# https://github.com/pallets/flask/issues/409552# https://github.com/pallets/flask/issues/429553# https://github.com/pallets/flask/issues/429754ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]55ErrorHandlerDecorator = t.TypeVar("ErrorHandlerDecorator", bound=ErrorHandlerCallable)5657ViewCallable = t.Callable[..., ResponseReturnValue]58RouteDecorator = t.TypeVar("RouteDecorator", bound=ViewCallable)
added:tests/typing/typing_error_handler.py+33 added
1from __future__ import annotations23from http import HTTPStatus45from werkzeug.exceptions import BadRequest6from werkzeug.exceptions import NotFound78from flask import Flask910app = Flask(__name__)111213@app.errorhandler(400)14@app.errorhandler(HTTPStatus.BAD_REQUEST)15@app.errorhandler(BadRequest)16def handle_400(e: BadRequest) -> str:17 return ""181920@app.errorhandler(ValueError)21def handle_custom(e: ValueError) -> str:22 return ""232425@app.errorhandler(ValueError)26def handle_accept_base(e: Exception) -> str:27 return ""282930@app.errorhandler(BadRequest)31@app.errorhandler(404)32def handle_multiple(e: BadRequest | NotFound) -> str:33 return ""