Skip to content
Myr

relax errorhandler function arg type

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 0ef1e65 · Browse files · 5 files+19 added−27 removed
This commit

State

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

Diff stats

5 files+19 added−27 removed

  • other:+19 added−27 removed

5 files+19 added−27 removed

modified:src/flask/app.py+1 added-3 removed
1263 unmodified lines
1264 """1265 self.shell_context_processors.append(f)1266 return f12671268 def _find_error_handler(1269 self, e: Exception1270 ) -> t.Optional["ErrorHandlerCallable[Exception]"]:1268 def _find_error_handler(self, e: Exception) -> t.Optional["ErrorHandlerCallable"]:1269 """Return a registered error handler for an exception in this order:1270 blueprint handler for a specific code, app handler for a specific code,1271 blueprint handler for an exception class, app handler for an exception1272 class, or ``None`` if a suitable handler is not found.
810 unmodified lines
modified:src/flask/blueprints.py+1 added-3 removed
572 unmodified lines
573 """Like :meth:`Flask.errorhandler` but for a blueprint. This574 handler is used for all requests, even if outside of the blueprint.575 """576577 def decorator(578 f: "ErrorHandlerCallable[Exception]",579 ) -> "ErrorHandlerCallable[Exception]":577 def decorator(f: "ErrorHandlerCallable") -> "ErrorHandlerCallable":578 self.record_once(lambda s: s.app.errorhandler(code)(f))579 return f580581 return decorator
16 unmodified lines
modified:src/flask/scaffold.py+7 added-18 removed
19 unmodified lines
20from .templating import _default_template_ctx_processor21from .typing import AfterRequestCallable22from .typing import AppOrBlueprintKey23from .typing import BeforeRequestCallable24from .typing import GenericException24from .typing import TeardownCallable25from .typing import TemplateContextProcessorCallable26from .typing import URLDefaultCallable27from .typing import URLValuePreprocessorCallable
115 unmodified lines
143 #: This data structure is internal. It should not be modified144 #: directly and its format may change at any time.145 self.error_handler_spec: t.Dict[146 AppOrBlueprintKey,148 t.Dict[149 t.Optional[int],150 t.Dict[t.Type[Exception], "ErrorHandlerCallable[Exception]"],151 ],147 t.Dict[t.Optional[int], t.Dict[t.Type[Exception], "ErrorHandlerCallable"]],148 ] = defaultdict(lambda: defaultdict(dict))149150 #: A data structure of functions to call at the beginning of151 #: each request, in the format ``{scope: [functions]}``. The
495 unmodified lines
647 return f648649 @setupmethod650 def errorhandler(655 self, code_or_exception: t.Union[t.Type[GenericException], int]656 ) -> t.Callable[657 ["ErrorHandlerCallable[GenericException]"],658 "ErrorHandlerCallable[GenericException]",659 ]:651 self, code_or_exception: t.Union[t.Type[Exception], int]652 ) -> t.Callable[["ErrorHandlerCallable"], "ErrorHandlerCallable"]:653 """Register a function to handle errors by code or exception class.654655 A decorator that is used to register a function given an656 error code. Example::
21 unmodified lines
678 :param code_or_exception: the code as integer for the handler, or679 an arbitrary exception680 """681689 def decorator(690 f: "ErrorHandlerCallable[GenericException]",691 ) -> "ErrorHandlerCallable[GenericException]":682 def decorator(f: "ErrorHandlerCallable") -> "ErrorHandlerCallable":683 self.register_error_handler(code_or_exception, f)684 return f685686 return decorator687688 @setupmethod689 def register_error_handler(690 self,700 code_or_exception: t.Union[t.Type[GenericException], int],701 f: "ErrorHandlerCallable[GenericException]",691 code_or_exception: t.Union[t.Type[Exception], int],692 f: "ErrorHandlerCallable",693 ) -> None:694 """Alternative error attach function to the :meth:`errorhandler`695 decorator that is more straightforward to use for non decorator696 usage.
15 unmodified lines
712 " code. Use a subclass of HTTPException with that code"713 " instead."714 ) from None715725 self.error_handler_spec[None][code][exc_class] = t.cast(726 "ErrorHandlerCallable[Exception]", f727 )716 self.error_handler_spec[None][code][exc_class] = f717718 @staticmethod719 def _get_exc_class_and_code(720 exc_class_or_code: t.Union[t.Type[Exception], int]
149 unmodified lines
modified:src/flask/typing.py+7 added-3 removed
31 unmodified lines
32 t.Tuple[ResponseValue, StatusCode, HeadersValue],33 "WSGIApplication",34]3536GenericException = t.TypeVar("GenericException", bound=Exception, contravariant=True)3736AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named37AfterRequestCallable = t.Callable[["Response"], "Response"]38BeforeFirstRequestCallable = t.Callable[[], None]39BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
3 unmodified lines
43TemplateGlobalCallable = t.Callable[..., t.Any]44TemplateTestCallable = t.Callable[..., bool]45URLDefaultCallable = t.Callable[[str, dict], None]46URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]47# This should take Exception, but that either breaks typing the argument48# with a specific exception, or decorating multiple times with different49# exceptions (and using a union type on the argument).50# https://github.com/pallets/flask/issues/409551# https://github.com/pallets/flask/issues/429552# https://github.com/pallets/flask/issues/429749ErrorHandlerCallable = t.Callable[[GenericException], ResponseReturnValue]53ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]
modified:CHANGES.rst+3 added
45 unmodified lines
46 context from the ``flask run`` command remains available in the47 loader thread. :issue:`4460`48- Deleting the session cookie uses the ``httponly`` flag.49 :issue:`4485`50- Relax typing for ``errorhandler`` to allow the user to use more51 precise types and decorate the same function multiple times.52 :issue:`4095, 4295, 4297`535455Version 2.0.356-------------
1199 unmodified lines