Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Unify all json.(loads|dumps) usage to utils.json #28702

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ persistent=yes

# List of plugins (as comma separated values of python modules names) to load,
# usually to register additional checkers.
load-plugins=
load-plugins=superset.extensions.pylint

# Use multiple processes to speed up Pylint.
jobs=2
Expand Down
7 changes: 3 additions & 4 deletions superset/annotation_layers/annotations/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
from marshmallow import fields, Schema, ValidationError
from marshmallow.validate import Length

from superset.exceptions import SupersetException
from superset.utils import json as json_utils
from superset.utils import json

openapi_spec_methods_override = {
"get": {"get": {"summary": "Get an annotation layer"}},
Expand Down Expand Up @@ -51,8 +50,8 @@

def validate_json(value: Union[bytes, bytearray, str]) -> None:
try:
json_utils.validate_json(value)
except SupersetException as ex:
json.validate_json(value)
except json.JSONDecodeError as ex:
raise ValidationError("JSON not valid") from ex


Expand Down
2 changes: 1 addition & 1 deletion superset/async_events/async_query_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import logging
import uuid
from typing import Any, Literal, Optional
Expand All @@ -23,6 +22,7 @@
import redis
from flask import Flask, Request, request, Response, session

from superset.utils import json
from superset.utils.core import get_user_id

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.
# pylint: disable=too-many-lines
import json
import logging
from datetime import datetime
from io import BytesIO
Expand Down Expand Up @@ -81,6 +80,7 @@
from superset.models.slice import Slice
from superset.tasks.thumbnails import cache_chart_thumbnail
from superset.tasks.utils import get_current_user
from superset.utils import json
from superset.utils.screenshots import ChartScreenshot, DEFAULT_CHART_WINDOW_SIZE
from superset.utils.urls import get_url_path
from superset.views.base_api import (
Expand Down
11 changes: 5 additions & 6 deletions superset/charts/data/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from __future__ import annotations

import contextlib
import json
import logging
from typing import Any, TYPE_CHECKING

Expand Down Expand Up @@ -46,7 +45,7 @@
from superset.exceptions import QueryObjectValidationError
from superset.extensions import event_logger
from superset.models.sql_lab import Query
from superset.utils import json as json_utils
from superset.utils import json
from superset.utils.core import (
create_zip,
DatasourceType,
Expand Down Expand Up @@ -129,7 +128,7 @@

try:
json_body = json.loads(chart.query_context)
except (TypeError, json.decoder.JSONDecodeError):
except (TypeError, json.JSONDecodeError):
json_body = None

if json_body is None:
Expand Down Expand Up @@ -171,7 +170,7 @@

try:
form_data = json.loads(chart.params)
except (TypeError, json.decoder.JSONDecodeError):
except (TypeError, json.JSONDecodeError):

Check warning on line 173 in superset/charts/data/api.py

View check run for this annotation

Codecov / codecov/patch

superset/charts/data/api.py#L173

Added line #L173 was not covered by tests
form_data = {}

return self._get_data_response(
Expand Down Expand Up @@ -395,9 +394,9 @@
)

if result_format == ChartDataResultFormat.JSON:
response_data = json_utils.dumps(
response_data = json.dumps(
{"result": result["queries"]},
default=json_utils.json_int_dttm_ser,
default=json.json_int_dttm_ser,
ignore_nan=True,
)
resp = make_response(response_data, 200)
Expand Down
4 changes: 2 additions & 2 deletions superset/commands/chart/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
# isort:skip_file

import json
import logging
from collections.abc import Iterator
from typing import Callable
Expand All @@ -30,6 +29,7 @@
from superset.models.slice import Slice
from superset.utils.dict_import_export import EXPORT_VERSION
from superset.utils.file import get_filename
from superset.utils import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -64,7 +64,7 @@
if payload.get("params"):
try:
payload["params"] = json.loads(payload["params"])
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:

Check warning on line 67 in superset/commands/chart/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/chart/export.py#L67

Added line #L67 was not covered by tests
logger.info("Unable to decode `params` field: %s", payload["params"])

payload["version"] = EXPORT_VERSION
Expand Down
4 changes: 2 additions & 2 deletions superset/commands/chart/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.

import copy
import json
from inspect import isclass
from typing import Any

Expand All @@ -25,6 +24,7 @@
from superset.migrations.shared.migrate_viz import processors
from superset.migrations.shared.migrate_viz.base import MigrateViz
from superset.models.slice import Slice
from superset.utils import json
from superset.utils.core import AnnotationType, get_user


Expand Down Expand Up @@ -117,7 +117,7 @@
# also update `query_context`
try:
query_context = json.loads(output.get("query_context") or "{}")
except (json.decoder.JSONDecodeError, TypeError):
except (json.JSONDecodeError, TypeError):

Check warning on line 120 in superset/commands/chart/importers/v1/utils.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/chart/importers/v1/utils.py#L120

Added line #L120 was not covered by tests
query_context = {}
if "form_data" in query_context:
query_context["form_data"] = output["params"]
Expand Down
2 changes: 1 addition & 1 deletion superset/commands/chart/warm_up_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from typing import Any, Optional, Union

import simplejson as json
from flask import g

from superset.commands.base import BaseCommand
Expand All @@ -29,6 +28,7 @@
)
from superset.extensions import db
from superset.models.slice import Slice
from superset.utils import json
from superset.utils.core import error_msg_from_exception
from superset.views.utils import get_dashboard_extra_filters, get_form_data, get_viz
from superset.viz import viz_types
Expand Down
6 changes: 3 additions & 3 deletions superset/commands/dashboard/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
# isort:skip_file

import json
import logging
import random
import string
Expand All @@ -36,6 +35,7 @@
from superset.models.slice import Slice
from superset.utils.dict_import_export import EXPORT_VERSION
from superset.utils.file import get_filename
from superset.utils import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -126,7 +126,7 @@
if value:
try:
payload[new_name] = json.loads(value)
except (TypeError, json.decoder.JSONDecodeError):
except (TypeError, json.JSONDecodeError):

Check warning on line 129 in superset/commands/dashboard/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dashboard/export.py#L129

Added line #L129 was not covered by tests
logger.info("Unable to decode `%s` field: %s", key, value)
payload[new_name] = {}

Expand Down Expand Up @@ -176,7 +176,7 @@
if value:
try:
payload[new_name] = json.loads(value)
except (TypeError, json.decoder.JSONDecodeError):
except (TypeError, json.JSONDecodeError):

Check warning on line 179 in superset/commands/dashboard/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dashboard/export.py#L179

Added line #L179 was not covered by tests
logger.info("Unable to decode `%s` field: %s", key, value)
payload[new_name] = {}

Expand Down
2 changes: 1 addition & 1 deletion superset/commands/dashboard/importers/v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import logging
import time
from copy import copy
Expand All @@ -32,6 +31,7 @@
from superset.migrations.shared.native_filters import migrate_dashboard
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.utils import json
from superset.utils.dashboard_filter_scopes_converter import (
convert_filter_scopes,
copy_filter_scopes,
Expand Down
2 changes: 1 addition & 1 deletion superset/commands/dashboard/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
# specific language governing permissions and limitations
# under the License.

import json
import logging
from typing import Any

from superset import db, security_manager
from superset.commands.exceptions import ImportFailedError
from superset.models.dashboard import Dashboard
from superset.utils import json
from superset.utils.core import get_user

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion superset/commands/dashboard/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import logging
from typing import Any, Optional

Expand All @@ -37,6 +36,7 @@
from superset.extensions import db
from superset.models.dashboard import Dashboard
from superset.tags.models import ObjectType
from superset.utils import json

logger = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions superset/commands/database/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
# isort:skip_file
import functools
import json
import logging
from typing import Any, Callable
from collections.abc import Iterator
Expand All @@ -30,14 +29,15 @@
from superset.utils.dict_import_export import EXPORT_VERSION
from superset.utils.file import get_filename
from superset.utils.ssh_tunnel import mask_password_info
from superset.utils import json

logger = logging.getLogger(__name__)


def parse_extra(extra_payload: str) -> dict[str, Any]:
try:
extra = json.loads(extra_payload)
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:

Check warning on line 40 in superset/commands/database/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/database/export.py#L40

Added line #L40 was not covered by tests
logger.info("Unable to decode `extra` field: %s", extra_payload)
return {}

Expand Down
2 changes: 1 addition & 1 deletion superset/commands/database/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.

import json
from typing import Any

from superset import app, db, security_manager
Expand All @@ -25,6 +24,7 @@
from superset.exceptions import SupersetSecurityException
from superset.models.core import Database
from superset.security.analytics_db_safety import check_sqlalchemy_uri
from superset.utils import json


def import_database(
Expand Down
4 changes: 2 additions & 2 deletions superset/commands/database/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
from contextlib import closing
from typing import Any, Optional

Expand All @@ -33,6 +32,7 @@
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.extensions import event_logger
from superset.models.core import Database
from superset.utils import json

BYPASS_VALIDATION_ENGINES = {"bigquery"}

Expand Down Expand Up @@ -82,7 +82,7 @@
)
try:
encrypted_extra = json.loads(serialized_encrypted_extra)
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:

Check warning on line 85 in superset/commands/database/validate.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/database/validate.py#L85

Added line #L85 was not covered by tests
encrypted_extra = {}

# try to connect
Expand Down
8 changes: 4 additions & 4 deletions superset/commands/dataset/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
# isort:skip_file

import json
import logging
from collections.abc import Iterator
from typing import Callable
Expand All @@ -31,6 +30,7 @@
from superset.utils.dict_import_export import EXPORT_VERSION
from superset.utils.file import get_filename
from superset.utils.ssh_tunnel import mask_password_info
from superset.utils import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -63,14 +63,14 @@
if payload.get(key):
try:
payload[key] = json.loads(payload[key])
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:

Check warning on line 66 in superset/commands/dataset/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dataset/export.py#L66

Added line #L66 was not covered by tests
logger.info("Unable to decode `%s` field: %s", key, payload[key])
for key in ("metrics", "columns"):
for attributes in payload.get(key, []):
if attributes.get("extra"):
try:
attributes["extra"] = json.loads(attributes["extra"])
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:

Check warning on line 73 in superset/commands/dataset/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dataset/export.py#L73

Added line #L73 was not covered by tests
logger.info(
"Unable to decode `extra` field: %s", attributes["extra"]
)
Expand Down Expand Up @@ -108,7 +108,7 @@
if payload.get("extra"):
try:
payload["extra"] = json.loads(payload["extra"])
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:

Check warning on line 111 in superset/commands/dataset/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dataset/export.py#L111

Added line #L111 was not covered by tests
logger.info("Unable to decode `extra` field: %s", payload["extra"])

if ssh_tunnel := DatabaseDAO.get_ssh_tunnel(model.database.id):
Expand Down
2 changes: 1 addition & 1 deletion superset/commands/dataset/importers/v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import logging
from typing import Any, Callable, Optional

Expand All @@ -34,6 +33,7 @@
TableColumn,
)
from superset.models.core import Database
from superset.utils import json
from superset.utils.dict_import_export import DATABASES_KEY

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion superset/commands/dataset/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.
import gzip
import json
import logging
import re
from typing import Any
Expand All @@ -33,6 +32,7 @@
from superset.connectors.sqla.models import SqlaTable
from superset.models.core import Database
from superset.sql_parse import Table
from superset.utils import json
from superset.utils.core import get_user

logger = logging.getLogger(__name__)
Expand Down
Loading
Loading