fix: several disabled pylint rules in models/helpers.py (#10909)

* Removed conflicting lint and isort check in model helpers seems it's not appearing anymore

* Removed disabled linting for accessing private method. `parent_foreign_key_mappings` becomes public because it is accessed by other instance than `self`.

* Updated model's helper - removed unecessary exception and replaced with check while accessing global context to reset ownerships.

* Updated model's helper - renamed unused attribute to private in user link method.

* Updated model's helper - added specific exception for adding extra json column. Removed disabled pylint rule.

* Applied changes after review to `models/helpers.py`:
- removed unecesary function's param rename
- added extra JSON content in exception

* Removed self.extra_json content from exception message.
This commit is contained in:
Kasia Kucharczyk 2020-09-18 17:32:02 +02:00 committed by GitHub
parent e375ed4678
commit 2003442b32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,10 +19,9 @@ import json
import logging import logging
import re import re
from datetime import datetime, timedelta from datetime import datetime, timedelta
from json.decoder import JSONDecodeError
from typing import Any, Dict, List, Optional, Set, Union from typing import Any, Dict, List, Optional, Set, Union
# isort and pylint disagree, isort should win
# pylint: disable=ungrouped-imports
import humanize import humanize
import pandas as pd import pandas as pd
import pytz import pytz
@ -45,9 +44,7 @@ logger = logging.getLogger(__name__)
def json_to_dict(json_str: str) -> Dict[Any, Any]: def json_to_dict(json_str: str) -> Dict[Any, Any]:
if json_str: if json_str:
val = re.sub(",[ \t\r\n]+}", "}", json_str) val = re.sub(",[ \t\r\n]+}", "}", json_str)
val = re.sub( val = re.sub(",[ \t\r\n]+\\]", "]", val)
",[ \t\r\n]+\]", "]", val # pylint: disable=anomalous-backslash-in-string
)
return json.loads(val) return json.loads(val)
return {} return {}
@ -89,6 +86,14 @@ class ImportMixin:
) )
return unique return unique
@classmethod
def parent_foreign_key_mappings(cls) -> Dict[str, str]:
"""Get a mapping of foreign name to the local name of foreign keys"""
parent_rel = cls.__mapper__.relationships.get(cls.export_parent)
if parent_rel:
return {l.name: r.name for (l, r) in parent_rel.local_remote_pairs}
return {}
@classmethod @classmethod
def export_schema( def export_schema(
cls, recursive: bool = True, include_parent_ref: bool = False cls, recursive: bool = True, include_parent_ref: bool = False
@ -135,7 +140,7 @@ class ImportMixin:
"""Import obj from a dictionary""" """Import obj from a dictionary"""
if sync is None: if sync is None:
sync = [] sync = []
parent_refs = cls._parent_foreign_key_mappings() parent_refs = cls.parent_foreign_key_mappings()
export_fields = set(cls.export_fields) | set(parent_refs.keys()) export_fields = set(cls.export_fields) | set(parent_refs.keys())
new_children = {c: dict_rep[c] for c in cls.export_children if c in dict_rep} new_children = {c: dict_rep[c] for c in cls.export_children if c in dict_rep}
unique_constrains = cls._unique_constrains() unique_constrains = cls._unique_constrains()
@ -217,9 +222,7 @@ class ImportMixin:
# If children should get synced, delete the ones that did not # If children should get synced, delete the ones that did not
# get updated. # get updated.
if child in sync and not is_new_obj: if child in sync and not is_new_obj:
back_refs = ( back_refs = child_class.parent_foreign_key_mappings()
child_class._parent_foreign_key_mappings() # pylint: disable=protected-access
)
delete_filters = [ delete_filters = [
getattr(child_class, k) == getattr(obj, back_refs.get(k)) getattr(child_class, k) == getattr(obj, back_refs.get(k))
for k in back_refs.keys() for k in back_refs.keys()
@ -306,11 +309,9 @@ class ImportMixin:
self.created_by = None self.created_by = None
self.changed_by = None self.changed_by = None
# flask global context might not exist (in cli or tests for example) # flask global context might not exist (in cli or tests for example)
try: self.owners = []
if g.user: if g and hasattr(g, "user"):
self.owners = [g.user] self.owners = [g.user]
except Exception: # pylint: disable=broad-except
self.owners = []
@property @property
def params_dict(self) -> Dict[Any, Any]: def params_dict(self) -> Dict[Any, Any]:
@ -321,7 +322,7 @@ class ImportMixin:
return json_to_dict(self.template_params) # type: ignore return json_to_dict(self.template_params) # type: ignore
def _user_link(user: User) -> Union[Markup, str]: # pylint: disable=no-self-use def _user_link(user: User) -> Union[Markup, str]:
if not user: if not user:
return "" return ""
url = "/superset/profile/{}/".format(user.username) url = "/superset/profile/{}/".format(user.username)
@ -424,7 +425,10 @@ class ExtraJSONMixin:
def extra(self) -> Dict[str, Any]: def extra(self) -> Dict[str, Any]:
try: try:
return json.loads(self.extra_json) return json.loads(self.extra_json)
except Exception: # pylint: disable=broad-except except (TypeError, JSONDecodeError) as exc:
logger.error(
"Unable to load an extra json: %r. Leaving empty.", exc, exc_info=True
)
return {} return {}
def set_extra_json(self, extras: Dict[str, Any]) -> None: def set_extra_json(self, extras: Dict[str, Any]) -> None: