mirror of
https://github.com/apache/superset.git
synced 2026-04-20 16:44:46 +00:00
Re-enable pylint for some model files (#8770)
* Allow id as a valid name for pylint * Re-enable pylint for superset/models/core.py * re-enable pylint for superset/models/sql_lab.py * re-enable pylint for superset/models/schedules.py * re-enable pylint for superset/models/helpers.py * re-enable pylint for superset/models/annotations.py * re-enable pylint on superset/models/tags.py * a couple more fixes after black formatting... * Add another inline pylint disable * Fix black * Move to inline disables for 'id' attribute on models * Fix lint disables after black reformatted them
This commit is contained in:
committed by
Maxime Beauchemin
parent
6c130b32ad
commit
adf2cc2039
@@ -14,7 +14,6 @@
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
# pylint: disable=C,R,W
|
||||
"""a collection of model-related helper classes and functions"""
|
||||
import json
|
||||
import logging
|
||||
@@ -22,6 +21,8 @@ import re
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
# isort and pylint disagree, isort should win
|
||||
# pylint: disable=ungrouped-imports
|
||||
import humanize
|
||||
import sqlalchemy as sa
|
||||
import yaml
|
||||
@@ -38,10 +39,12 @@ from superset.utils.core import QueryStatus
|
||||
def json_to_dict(json_str):
|
||||
if json_str:
|
||||
val = re.sub(",[ \t\r\n]+}", "}", json_str)
|
||||
val = re.sub(",[ \t\r\n]+\]", "]", val)
|
||||
val = re.sub(
|
||||
",[ \t\r\n]+\]", "]", val # pylint: disable=anomalous-backslash-in-string
|
||||
)
|
||||
return json.loads(val)
|
||||
else:
|
||||
return {}
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
class ImportMixin(object):
|
||||
@@ -83,24 +86,24 @@ class ImportMixin(object):
|
||||
if not include_parent_ref:
|
||||
parent_ref = cls.__mapper__.relationships.get(cls.export_parent)
|
||||
if parent_ref:
|
||||
parent_excludes = {c.name for c in parent_ref.local_columns}
|
||||
parent_excludes = {column.name for column in parent_ref.local_columns}
|
||||
|
||||
def formatter(c):
|
||||
def formatter(column):
|
||||
return (
|
||||
"{0} Default ({1})".format(str(c.type), c.default.arg)
|
||||
if c.default
|
||||
else str(c.type)
|
||||
"{0} Default ({1})".format(str(column.type), column.default.arg)
|
||||
if column.default
|
||||
else str(column.type)
|
||||
)
|
||||
|
||||
schema = {
|
||||
c.name: formatter(c)
|
||||
for c in cls.__table__.columns
|
||||
if (c.name in cls.export_fields and c.name not in parent_excludes)
|
||||
column.name: formatter(column)
|
||||
for column in cls.__table__.columns
|
||||
if (column.name in cls.export_fields and column.name not in parent_excludes)
|
||||
}
|
||||
if recursive:
|
||||
for c in cls.export_children:
|
||||
child_class = cls.__mapper__.relationships[c].argument.class_
|
||||
schema[c] = [
|
||||
for column in cls.export_children:
|
||||
child_class = cls.__mapper__.relationships[column].argument.class_
|
||||
schema[column] = [
|
||||
child_class.export_schema(
|
||||
recursive=recursive, include_parent_ref=include_parent_ref
|
||||
)
|
||||
@@ -108,8 +111,12 @@ class ImportMixin(object):
|
||||
return schema
|
||||
|
||||
@classmethod
|
||||
def import_from_dict(cls, session, dict_rep, parent=None, recursive=True, sync=[]):
|
||||
def import_from_dict(
|
||||
cls, session, dict_rep, parent=None, recursive=True, sync=None
|
||||
): # pylint: disable=too-many-arguments,too-many-locals,too-many-branches
|
||||
"""Import obj from a dictionary"""
|
||||
if sync is None:
|
||||
sync = []
|
||||
parent_refs = cls._parent_foreign_key_mappings()
|
||||
export_fields = set(cls.export_fields) | set(parent_refs.keys())
|
||||
new_children = {
|
||||
@@ -126,10 +133,10 @@ class ImportMixin(object):
|
||||
|
||||
if not parent:
|
||||
if cls.export_parent:
|
||||
for p in parent_refs.keys():
|
||||
if p not in dict_rep:
|
||||
for prnt in parent_refs.keys():
|
||||
if prnt not in dict_rep:
|
||||
raise RuntimeError(
|
||||
"{0}: Missing field {1}".format(cls.__name__, p)
|
||||
"{0}: Missing field {1}".format(cls.__name__, prnt)
|
||||
)
|
||||
else:
|
||||
# Set foreign keys to parent obj
|
||||
@@ -182,10 +189,10 @@ class ImportMixin(object):
|
||||
|
||||
# Recursively create children
|
||||
if recursive:
|
||||
for c in cls.export_children:
|
||||
child_class = cls.__mapper__.relationships[c].argument.class_
|
||||
for child in cls.export_children:
|
||||
child_class = cls.__mapper__.relationships[child].argument.class_
|
||||
added = []
|
||||
for c_obj in new_children.get(c, []):
|
||||
for c_obj in new_children.get(child, []):
|
||||
added.append(
|
||||
child_class.import_from_dict(
|
||||
session=session, dict_rep=c_obj, parent=obj, sync=sync
|
||||
@@ -193,8 +200,10 @@ class ImportMixin(object):
|
||||
)
|
||||
# If children should get synced, delete the ones that did not
|
||||
# get updated.
|
||||
if c in sync and not is_new_obj:
|
||||
back_refs = child_class._parent_foreign_key_mappings()
|
||||
if child in sync and not is_new_obj:
|
||||
back_refs = (
|
||||
child_class._parent_foreign_key_mappings() # pylint: disable=protected-access
|
||||
)
|
||||
delete_filters = [
|
||||
getattr(child_class, k) == getattr(obj, back_refs.get(k))
|
||||
for k in back_refs.keys()
|
||||
@@ -203,7 +212,7 @@ class ImportMixin(object):
|
||||
session.query(child_class).filter(and_(*delete_filters))
|
||||
).difference(set(added))
|
||||
for o in to_delete:
|
||||
logging.info("Deleting %s %s", c, str(obj))
|
||||
logging.info("Deleting %s %s", child, str(obj))
|
||||
session.delete(o)
|
||||
|
||||
return obj
|
||||
@@ -234,16 +243,16 @@ class ImportMixin(object):
|
||||
)
|
||||
}
|
||||
if recursive:
|
||||
for c in self.export_children:
|
||||
for cld in self.export_children:
|
||||
# sorting to make lists of children stable
|
||||
dict_rep[c] = sorted(
|
||||
dict_rep[cld] = sorted(
|
||||
[
|
||||
child.export_to_dict(
|
||||
recursive=recursive,
|
||||
include_parent_ref=include_parent_ref,
|
||||
include_defaults=include_defaults,
|
||||
)
|
||||
for child in getattr(self, c)
|
||||
for child in getattr(self, cld)
|
||||
],
|
||||
key=lambda k: sorted(str(k.items())),
|
||||
)
|
||||
@@ -276,7 +285,7 @@ class ImportMixin(object):
|
||||
try:
|
||||
if g.user:
|
||||
self.owners = [g.user]
|
||||
except Exception:
|
||||
except Exception: # pylint: disable=broad-except
|
||||
self.owners = []
|
||||
|
||||
@property
|
||||
@@ -288,6 +297,13 @@ class ImportMixin(object):
|
||||
return json_to_dict(self.template_params)
|
||||
|
||||
|
||||
def _user_link(user): # pylint: disable=no-self-use
|
||||
if not user:
|
||||
return ""
|
||||
url = "/superset/profile/{}/".format(user.username)
|
||||
return Markup('<a href="{}">{}</a>'.format(url, escape(user) or ""))
|
||||
|
||||
|
||||
class AuditMixinNullable(AuditMixin):
|
||||
|
||||
"""Altering the AuditMixin to use nullable fields
|
||||
@@ -319,12 +335,6 @@ class AuditMixinNullable(AuditMixin):
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
def _user_link(self, user):
|
||||
if not user:
|
||||
return ""
|
||||
url = "/superset/profile/{}/".format(user.username)
|
||||
return Markup('<a href="{}">{}</a>'.format(url, escape(user) or ""))
|
||||
|
||||
def changed_by_name(self):
|
||||
if self.created_by:
|
||||
return escape("{}".format(self.created_by))
|
||||
@@ -332,11 +342,11 @@ class AuditMixinNullable(AuditMixin):
|
||||
|
||||
@renders("created_by")
|
||||
def creator(self):
|
||||
return self._user_link(self.created_by)
|
||||
return _user_link(self.created_by)
|
||||
|
||||
@property
|
||||
def changed_by_(self):
|
||||
return self._user_link(self.changed_by)
|
||||
return _user_link(self.changed_by)
|
||||
|
||||
@renders("changed_on")
|
||||
def changed_on_(self):
|
||||
@@ -351,14 +361,14 @@ class AuditMixinNullable(AuditMixin):
|
||||
return Markup(f'<span class="no-wrap">{self.changed_on_humanized}</span>')
|
||||
|
||||
|
||||
class QueryResult(object):
|
||||
class QueryResult(object): # pylint: disable=too-few-public-methods
|
||||
|
||||
"""Object returned by the query interface"""
|
||||
|
||||
def __init__(
|
||||
def __init__( # pylint: disable=too-many-arguments
|
||||
self, df, query, duration, status=QueryStatus.SUCCESS, error_message=None
|
||||
):
|
||||
self.df = df
|
||||
self.df = df # pylint: disable=invalid-name
|
||||
self.query = query
|
||||
self.duration = duration
|
||||
self.status = status
|
||||
@@ -374,7 +384,7 @@ class ExtraJSONMixin:
|
||||
def extra(self):
|
||||
try:
|
||||
return json.loads(self.extra_json)
|
||||
except Exception:
|
||||
except Exception: # pylint: disable=broad-except
|
||||
return {}
|
||||
|
||||
def set_extra_json(self, d):
|
||||
|
||||
Reference in New Issue
Block a user