From 8058f14d4ad1cf4d0369cca426933b956b352818 Mon Sep 17 00:00:00 2001 From: tarun Date: Thu, 30 Jul 2026 19:58:53 +0530 Subject: [PATCH] Fix issue #5200: pass positional arguments to class-based views (Fixes #5199) The `dispatch_request` method in the `View` and `MethodView` classes has been modified to accept positional arguments. The `as_view` method in the `View` class has also been updated to pass these arguments to the view function. --- src/flask/views.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/flask/views.py b/src/flask/views.py index 53fe976dc2..b53814f94e 100644 --- a/src/flask/views.py +++ b/src/flask/views.py @@ -12,7 +12,6 @@ ["get", "post", "head", "options", "delete", "put", "trace", "patch"] ) - class View: """Subclass this class and override :meth:`dispatch_request` to create a generic class-based view. Call :meth:`as_view` to create a @@ -75,7 +74,7 @@ def dispatch_request(self, name): #: .. versionadded:: 2.2 init_every_request: t.ClassVar[bool] = True - def dispatch_request(self) -> ft.ResponseReturnValue: + def dispatch_request(self, *args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue: """The actual view function behavior. Subclasses must override this and return a valid response. Any variables from the URL rule are passed as keyword arguments. @@ -103,17 +102,17 @@ def as_view( """ if cls.init_every_request: - def view(**kwargs: t.Any) -> ft.ResponseReturnValue: + def view(*args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue: self = view.view_class( # type: ignore[attr-defined] *class_args, **class_kwargs ) - return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return] + return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs) # type: ignore[no-any-return] else: self = cls(*class_args, **class_kwargs) # pyright: ignore - def view(**kwargs: t.Any) -> ft.ResponseReturnValue: - return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return] + def view(*args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue: + return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs) # type: ignore[no-any-return] if cls.decorators: view.__name__ = name @@ -134,7 +133,6 @@ def view(**kwargs: t.Any) -> ft.ResponseReturnValue: view.provide_automatic_options = cls.provide_automatic_options # type: ignore return view - class MethodView(View): """Dispatches request methods to the corresponding instance methods. For example, if you implement a ``get`` method, it will be used to @@ -179,7 +177,7 @@ def __init_subclass__(cls, **kwargs: t.Any) -> None: if methods: cls.methods = methods - def dispatch_request(self, **kwargs: t.Any) -> ft.ResponseReturnValue: + def dispatch_request(self, *args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue: meth = getattr(self, request.method.lower(), None) # If the request method is HEAD and we don't have a handler for it @@ -188,4 +186,4 @@ def dispatch_request(self, **kwargs: t.Any) -> ft.ResponseReturnValue: meth = getattr(self, "get", None) assert meth is not None, f"Unimplemented method {request.method!r}" - return current_app.ensure_sync(meth)(**kwargs) # type: ignore[no-any-return] + return current_app.ensure_sync(meth)(*args, **kwargs) # type: ignore[no-any-return]