Skip to content
Myr

Inherit "methods" in MethodView

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

* Current behavior: If a base class inherits MethodView and child class inherits without overwriting "methods". The "methods" defined in base class would be ignored * Fix: Inherit all the "methods" defined in base classes if "methods" variable is not overwritten

authored by LeeW · committed by David Lord · parent 8ef56c8 · Browse files · 3 files+27 added−3 removed
This commit

State

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

Diff stats

3 files+27 added−3 removed

  • tests:+19 added−2 removed
  • other:+8 added−1 removed

3 files+27 added−3 removed

modified:flask/views.py+4 added
118 unmodified lines
119120 if "methods" not in d:121 methods = set()122123 for base in bases:124 if getattr(base, "methods", None):125 methods.update(base.methods)126127 for key in http_method_funcs:128 if hasattr(cls, key):129 methods.add(key.upper())130
34 unmodified lines
modified:tests/test_views.py+19 added-2 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""1112import pytest13from werkzeug.http import parse_set_header1415import flask16import flask.views1717from werkzeug.http import parse_set_header181919def common_test(app):20 c = app.test_client()2122 assert c.get("/").data == b"GET"
174 unmodified lines
197 # But these tests should still pass. We just log a warning.198 common_test(app)199200201def test_methods_var_inheritance(app, client):202 class BaseView(flask.views.MethodView):203 methods = ["GET", "PROPFIND"]204205 class ChildView(BaseView):206 def get(self):207 return "GET"208209 def propfind(self):210 return "PROPFIND"211212 app.add_url_rule("/", view_func=ChildView.as_view("index"))213214 assert client.get("/").data == b"GET"215 assert client.open("/", method="PROPFIND").data == b"PROPFIND"216 assert ChildView.methods == {"PROPFIND", "GET"}217218219def test_multiple_inheritance(app, client):220 class GetView(flask.views.MethodView):221 def get(self):222 return "GET"
32 unmodified lines
modified:CHANGES.rst+4 added-1 removed
19 unmodified lines
20 (`#3059`_)21- :func:`send_file` supports :class:`~io.BytesIO` partial content.22 (`#2957`_)23- :func:`open_resource` accepts the "rt" file mode. This still does24 the same thing as "r". (:issue:`3163`)24 the same thing as "r". :issue:`3163`25- The :attr:`MethodView.methods` attribute set in a base class is used26 by subclasses. :issue:`3138`2728.. _#2935: https://github.com/pallets/flask/issues/293529.. _#2957: https://github.com/pallets/flask/issues/295730.. _#2994: https://github.com/pallets/flask/pull/299431.. _#3059: https://github.com/pallets/flask/pull/305932.. _#3179: https://github.com/pallets/flask/pull/3179333435Version 1.0.336-------------
941 unmodified lines