pass context through dispatch methods #5818
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current
AppContextobject is passed through the various request dispatch methods, rather than each method accessing the proxies. closes #5815@pgjones first proposed this in #5229 as a way to speed up dispatch especially for Quart and async views. This PR applies to more methods, and also implements compatibility during a deprecation period.
Dispatch methods now take
ctx: AppContextas the first parameter. The followingFlaskmethods were changed:update_template_contexthandle_http_exceptionhandle_user_exceptionhandle_exceptionlog_exceptiondispatch_requestfull_dispatch_requestfinalize_requestmake_default_options_responsepreprocess_requestprocess_responsedo_teardown_requestdo_teardown_appcontexturl_forandmake_responsewere not changed, as it's much more likely that these are called from user code that only has access to the proxy.An
__init_subclass__class method is added to detect old signatures on subclasses ofFlask. The second parameter is inspected (first is self). If it is not annotated, it must be namedctx. If it is annotated, it must either be the string or classAppContext. If an old signature is detected, the method is wrapped to remove the argument when otherFlaskmethods call it during dispatch. The base method is also wrapped to inject the argument so thatsuper().base_methodfrom the overridden method will continue to work.I did not apply the compat wrapper to every base
Flaskmethod, only the ones that a subclass overrides. Therefore, if user code is directly calling these internal dispatch methods, they will get aTypeError. This is only likely (and unlikely at that) to happen during testing. I did this over concern that the wrapper would be unnecessary and a performance hit for most applications. If we get bug reports we can consider adding the wrapper.