Skip to content

Commit 8058f14

Browse files
taruntarun
authored andcommitted
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.
1 parent 36e4a82 commit 8058f14

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/flask/views.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
["get", "post", "head", "options", "delete", "put", "trace", "patch"]
1313
)
1414

15-
1615
class View:
1716
"""Subclass this class and override :meth:`dispatch_request` to
1817
create a generic class-based view. Call :meth:`as_view` to create a
@@ -75,7 +74,7 @@ def dispatch_request(self, name):
7574
#: .. versionadded:: 2.2
7675
init_every_request: t.ClassVar[bool] = True
7776

78-
def dispatch_request(self) -> ft.ResponseReturnValue:
77+
def dispatch_request(self, *args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue:
7978
"""The actual view function behavior. Subclasses must override
8079
this and return a valid response. Any variables from the URL
8180
rule are passed as keyword arguments.
@@ -103,17 +102,17 @@ def as_view(
103102
"""
104103
if cls.init_every_request:
105104

106-
def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
105+
def view(*args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue:
107106
self = view.view_class( # type: ignore[attr-defined]
108107
*class_args, **class_kwargs
109108
)
110-
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
109+
return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs) # type: ignore[no-any-return]
111110

112111
else:
113112
self = cls(*class_args, **class_kwargs) # pyright: ignore
114113

115-
def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
116-
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
114+
def view(*args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue:
115+
return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs) # type: ignore[no-any-return]
117116

118117
if cls.decorators:
119118
view.__name__ = name
@@ -134,7 +133,6 @@ def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
134133
view.provide_automatic_options = cls.provide_automatic_options # type: ignore
135134
return view
136135

137-
138136
class MethodView(View):
139137
"""Dispatches request methods to the corresponding instance methods.
140138
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:
179177
if methods:
180178
cls.methods = methods
181179

182-
def dispatch_request(self, **kwargs: t.Any) -> ft.ResponseReturnValue:
180+
def dispatch_request(self, *args: t.Any, **kwargs: t.Any) -> ft.ResponseReturnValue:
183181
meth = getattr(self, request.method.lower(), None)
184182

185183
# 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:
188186
meth = getattr(self, "get", None)
189187

190188
assert meth is not None, f"Unimplemented method {request.method!r}"
191-
return current_app.ensure_sync(meth)(**kwargs) # type: ignore[no-any-return]
189+
return current_app.ensure_sync(meth)(*args, **kwargs) # type: ignore[no-any-return]

0 commit comments

Comments
 (0)