You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pass context internally instead of using contextvars #5815
Currently, there are a bunch of different methods on the Flask class that run to dispatch each request. Many of these access request and other context proxies. We should update them to pass the AppContext everywhere instead. This is more convenient after #5812 with only one context object instead of two. As @pgjones pointed out in #5229, not having to access the contextvar is a significant speedup to ASGI Quart, although it doesn't appear to affect WSGI Flask as much. Perhaps if we were serving with greenlets and so contexts were switching more, it would be more noticeable in Flask too.
The obvious problem is that it is a breaking change to the signatures of all these methods. I'm unsure how many methods are affected, but I'm also unsure how many projects are even subclassing Flask to override any of the methods. The fact that the methods are public seems unintentional, compared to the much more common ways of configuring and customizing an app.
We could continue to support both signatures, showing a deprecation warning for the old one, by adding some detection to Flask.__init__. This would add some amount of time during app setup, but wouldn't affect runtime performance of the app.
Another idea I had for the future was to have @route and other decorators inspect the signature of the decorated function, to allow injecting request, app, etc. if users would rather use that pattern than import the proxies. Having the request object directly available in all the dispatch methods would make this more straightforward. User code would be able to choose what pattern they want, the current proxy pattern would never be deprecated for user code.
Not a fan of deprecating it, for 99% of all apps the performance difference is unnecessary so the convenience wins IMHO.
I think nobody wants to update their application to a change like this, also because it would imply having to propagate the vars deeper into application code, util functions, etc. that currently use the profies.
Reacted by Roshan Damor | Python | AI | Full Stack
This would only be for methods on Flask (Scaffold, etc), it would not require passing them throughout your own code. #5229 gives an idea of the scope of that.
I've been playing around with the injector a bit, it could be used to re-add the arguments from the proxies further down the chain if some code wanted to use that pattern but other didn't.
The deprecation can be unversioned for a bit, just to see if anyone brings it up. If we do this internally, I don't want to support signature inspection and wrappers forever.
Reacted by Roshan Damor | Python | AI | Full Stack
It may be very difficult to do this in a way that existing overridden code will not fail and will show a deprecation warning.
We want to upgrade an overridden method that does not take the new ctx: AppContext parameter. We can detect this in __init_subclass__ with inspect.signature, and wrap the function to take the correct signature, so that other parts of the dispatch code will call it correctly. But that's the "simple" part.
What if other user code is still calling the method, overridden or not, without the new argument? The most obvious way this happens is with super().method within the overridden method. The wrapper could perhaps be clever enough to check the type of the first argument, and decide to make a new or old signature call. The wrapper would have to be applied to every base method to handle the super call case.
I haven't tried to write this more complex wrapper yet. I have a feeling it will be difficult to write. I know it will add overhead to every request, since multiple methods will now have to do detection on the arguments passed to them. This overhead will mostly be waste, as the vast majority of code will not be overridden.
Perhaps we can just say this is a breaking change in 4.0? I still think the result is worth it. We could still show a better error message by doing some detection in __init_subclass__, but showing an error instead of applying a wrapper and showing a warning.
The following method signatures would change to add ctx: AppContext as the first parameter:
update_template_context
handle_http_exception
handle_user_exception
handle_exception
log_exception
dispatch_request
full_dispatch_request
finalize_request
make_default_options_response
preprocess_request
process_response
do_teardown_request
do_teardown_appcontext
I'd be somewhat surprised to find any of these overridden. They're either really internal, or they're the machinery that calls registered callback functions.
I've managed to write some code that can detect overridden methods with the old signature, and wrap them and the corresponding base method so they'll continue to work during the deprecation period.
Activity
ThiefMaster commentedon Sep 13, 2025
Not a fan of deprecating it, for 99% of all apps the performance difference is unnecessary so the convenience wins IMHO.
I think nobody wants to update their application to a change like this, also because it would imply having to propagate the vars deeper into application code, util functions, etc. that currently use the profies.
davidism commentedon Sep 13, 2025
This would only be for methods on
Flask(Scaffold, etc), it would not require passing them throughout your own code. #5229 gives an idea of the scope of that.I've been playing around with the injector a bit, it could be used to re-add the arguments from the proxies further down the chain if some code wanted to use that pattern but other didn't.
The deprecation can be unversioned for a bit, just to see if anyone brings it up. If we do this internally, I don't want to support signature inspection and wrappers forever.
ThiefMaster commentedon Sep 13, 2025
ah, internals only. not such a big deal then, as it would just affect people subclassing...
davidism commentedon Sep 18, 2025
It may be very difficult to do this in a way that existing overridden code will not fail and will show a deprecation warning.
We want to upgrade an overridden method that does not take the new
ctx: AppContextparameter. We can detect this in__init_subclass__withinspect.signature, and wrap the function to take the correct signature, so that other parts of the dispatch code will call it correctly. But that's the "simple" part.What if other user code is still calling the method, overridden or not, without the new argument? The most obvious way this happens is with
super().methodwithin the overridden method. The wrapper could perhaps be clever enough to check the type of the first argument, and decide to make a new or old signature call. The wrapper would have to be applied to every base method to handle thesupercall case.I haven't tried to write this more complex wrapper yet. I have a feeling it will be difficult to write. I know it will add overhead to every request, since multiple methods will now have to do detection on the arguments passed to them. This overhead will mostly be waste, as the vast majority of code will not be overridden.
Perhaps we can just say this is a breaking change in 4.0? I still think the result is worth it. We could still show a better error message by doing some detection in
__init_subclass__, but showing an error instead of applying a wrapper and showing a warning.davidism commentedon Sep 18, 2025
The following method signatures would change to add
ctx: AppContextas the first parameter:update_template_contexthandle_http_exceptionhandle_user_exceptionhandle_exceptionlog_exceptiondispatch_requestfull_dispatch_requestfinalize_requestmake_default_options_responsepreprocess_requestprocess_responsedo_teardown_requestdo_teardown_appcontextI'd be somewhat surprised to find any of these overridden. They're either really internal, or they're the machinery that calls registered callback functions.
davidism commentedon Sep 19, 2025
I've managed to write some code that can detect overridden methods with the old signature, and wrap them and the corresponding base method so they'll continue to work during the deprecation period.