Merge pull request #3179 from Lee-W/MethodView-inheritance

Fix MethodView inheritance Error (#3138)
This commit is contained in:
David Lord 2019-05-17 14:18:40 -07:00 committed by GitHub
commit aade460f15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 3 deletions

View file

@ -21,12 +21,15 @@ Unreleased
- :func:`send_file` supports :class:`~io.BytesIO` partial content. - :func:`send_file` supports :class:`~io.BytesIO` partial content.
(`#2957`_) (`#2957`_)
- :func:`open_resource` accepts the "rt" file mode. This still does - :func:`open_resource` accepts the "rt" file mode. This still does
the same thing as "r". (:issue:`3163`) the same thing as "r". :issue:`3163`
- The :attr:`MethodView.methods` attribute set in a base class is used
by subclasses. :issue:`3138`
.. _#2935: https://github.com/pallets/flask/issues/2935 .. _#2935: https://github.com/pallets/flask/issues/2935
.. _#2957: https://github.com/pallets/flask/issues/2957 .. _#2957: https://github.com/pallets/flask/issues/2957
.. _#2994: https://github.com/pallets/flask/pull/2994 .. _#2994: https://github.com/pallets/flask/pull/2994
.. _#3059: https://github.com/pallets/flask/pull/3059 .. _#3059: https://github.com/pallets/flask/pull/3059
.. _#3179: https://github.com/pallets/flask/pull/3179
Version 1.0.3 Version 1.0.3

View file

@ -120,6 +120,10 @@ class MethodViewType(type):
if "methods" not in d: if "methods" not in d:
methods = set() methods = set()
for base in bases:
if getattr(base, "methods", None):
methods.update(base.methods)
for key in http_method_funcs: for key in http_method_funcs:
if hasattr(cls, key): if hasattr(cls, key):
methods.add(key.upper()) methods.add(key.upper())

View file

@ -10,12 +10,11 @@
""" """
import pytest import pytest
from werkzeug.http import parse_set_header
import flask import flask
import flask.views import flask.views
from werkzeug.http import parse_set_header
def common_test(app): def common_test(app):
c = app.test_client() c = app.test_client()
@ -199,6 +198,24 @@ def test_endpoint_override(app):
common_test(app) common_test(app)
def test_methods_var_inheritance(app, client):
class BaseView(flask.views.MethodView):
methods = ["GET", "PROPFIND"]
class ChildView(BaseView):
def get(self):
return "GET"
def propfind(self):
return "PROPFIND"
app.add_url_rule("/", view_func=ChildView.as_view("index"))
assert client.get("/").data == b"GET"
assert client.open("/", method="PROPFIND").data == b"PROPFIND"
assert ChildView.methods == {"PROPFIND", "GET"}
def test_multiple_inheritance(app, client): def test_multiple_inheritance(app, client):
class GetView(flask.views.MethodView): class GetView(flask.views.MethodView):
def get(self): def get(self):