style(mypy): Enforcing typing for superset.views (#9939)

Co-authored-by: John Bodley <john.bodley@airbnb.com>
This commit is contained in:
John Bodley
2020-06-05 08:44:11 -07:00
committed by GitHub
parent 5c4d4f16b3
commit 63e0188f45
23 changed files with 440 additions and 340 deletions

View File

@@ -27,6 +27,7 @@ from flask_sqlalchemy import BaseQuery
from superset import db, get_feature_flags, security_manager
from superset.constants import RouteMethod
from superset.models.sql_lab import Query, SavedQuery, TableSchema, TabState
from superset.typing import FlaskResponse
from superset.utils import core as utils
from .base import (
@@ -120,15 +121,15 @@ class SavedQueryView(
show_template = "superset/models/savedquery/show.html"
def pre_add(self, item):
def pre_add(self, item: "SavedQueryView") -> None:
item.user = g.user
def pre_update(self, item):
def pre_update(self, item: "SavedQueryView") -> None:
self.pre_add(item)
@has_access
@expose("show/<pk>")
def show(self, pk):
def show(self, pk: int) -> FlaskResponse:
pk = self._deserialize_pk_if_composite(pk)
widgets = self._show(pk)
query = self.datamodel.get(pk).to_json()
@@ -168,18 +169,18 @@ class SavedQueryViewApi(SavedQueryView): # pylint: disable=too-many-ancestors
@has_access_api
@expose("show/<pk>")
def show(self, pk):
def show(self, pk: int) -> FlaskResponse:
return super().show(pk)
def _get_owner_id(tab_state_id):
def _get_owner_id(tab_state_id: int) -> int:
return db.session.query(TabState.user_id).filter_by(id=tab_state_id).scalar()
class TabStateView(BaseSupersetView):
@has_access_api
@expose("/", methods=["POST"])
def post(self): # pylint: disable=no-self-use
def post(self) -> FlaskResponse: # pylint: disable=no-self-use
query_editor = json.loads(request.form["queryEditor"])
tab_state = TabState(
user_id=g.user.get_id(),
@@ -201,7 +202,7 @@ class TabStateView(BaseSupersetView):
@has_access_api
@expose("/<int:tab_state_id>", methods=["DELETE"])
def delete(self, tab_state_id): # pylint: disable=no-self-use
def delete(self, tab_state_id: int) -> FlaskResponse: # pylint: disable=no-self-use
if _get_owner_id(tab_state_id) != int(g.user.get_id()):
return Response(status=403)
@@ -216,7 +217,7 @@ class TabStateView(BaseSupersetView):
@has_access_api
@expose("/<int:tab_state_id>", methods=["GET"])
def get(self, tab_state_id): # pylint: disable=no-self-use
def get(self, tab_state_id: int) -> FlaskResponse: # pylint: disable=no-self-use
if _get_owner_id(tab_state_id) != int(g.user.get_id()):
return Response(status=403)
@@ -229,7 +230,9 @@ class TabStateView(BaseSupersetView):
@has_access_api
@expose("<int:tab_state_id>/activate", methods=["POST"])
def activate(self, tab_state_id): # pylint: disable=no-self-use
def activate( # pylint: disable=no-self-use
self, tab_state_id: int
) -> FlaskResponse:
owner_id = _get_owner_id(tab_state_id)
if owner_id is None:
return Response(status=404)
@@ -246,7 +249,7 @@ class TabStateView(BaseSupersetView):
@has_access_api
@expose("<int:tab_state_id>", methods=["PUT"])
def put(self, tab_state_id): # pylint: disable=no-self-use
def put(self, tab_state_id: int) -> FlaskResponse: # pylint: disable=no-self-use
if _get_owner_id(tab_state_id) != int(g.user.get_id()):
return Response(status=403)
@@ -257,7 +260,9 @@ class TabStateView(BaseSupersetView):
@has_access_api
@expose("<int:tab_state_id>/migrate_query", methods=["POST"])
def migrate_query(self, tab_state_id): # pylint: disable=no-self-use
def migrate_query( # pylint: disable=no-self-use
self, tab_state_id: int
) -> FlaskResponse:
if _get_owner_id(tab_state_id) != int(g.user.get_id()):
return Response(status=403)
@@ -270,7 +275,9 @@ class TabStateView(BaseSupersetView):
@has_access_api
@expose("<int:tab_state_id>/query/<client_id>", methods=["DELETE"])
def delete_query(self, tab_state_id, client_id): # pylint: disable=no-self-use
def delete_query( # pylint: disable=no-self-use
self, tab_state_id: str, client_id: str
) -> FlaskResponse:
db.session.query(Query).filter_by(
client_id=client_id, user_id=g.user.get_id(), sql_editor_id=tab_state_id
).delete(synchronize_session=False)
@@ -281,7 +288,7 @@ class TabStateView(BaseSupersetView):
class TableSchemaView(BaseSupersetView):
@has_access_api
@expose("/", methods=["POST"])
def post(self): # pylint: disable=no-self-use
def post(self) -> FlaskResponse: # pylint: disable=no-self-use
table = json.loads(request.form["table"])
# delete any existing table schema
@@ -306,7 +313,9 @@ class TableSchemaView(BaseSupersetView):
@has_access_api
@expose("/<int:table_schema_id>", methods=["DELETE"])
def delete(self, table_schema_id): # pylint: disable=no-self-use
def delete( # pylint: disable=no-self-use
self, table_schema_id: int
) -> FlaskResponse:
db.session.query(TableSchema).filter(TableSchema.id == table_schema_id).delete(
synchronize_session=False
)
@@ -315,7 +324,9 @@ class TableSchemaView(BaseSupersetView):
@has_access_api
@expose("/<int:table_schema_id>/expanded", methods=["POST"])
def expanded(self, table_schema_id): # pylint: disable=no-self-use
def expanded( # pylint: disable=no-self-use
self, table_schema_id: int
) -> FlaskResponse:
payload = json.loads(request.form["expanded"])
(
db.session.query(TableSchema)
@@ -332,6 +343,6 @@ class SqlLab(BaseSupersetView):
@expose("/my_queries/")
@has_access
def my_queries(self): # pylint: disable=no-self-use
def my_queries(self) -> FlaskResponse: # pylint: disable=no-self-use
"""Assigns a list of found users to the given role."""
return redirect("/savedqueryview/list/?_flt_0_user={}".format(g.user.id))