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

fix: mypy fails related to simplejson.dumps #29861

Merged
merged 1 commit into from
Aug 5, 2024
Merged
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
37 changes: 15 additions & 22 deletions superset/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import logging
import uuid
from datetime import date, datetime, time, timedelta
from typing import Any, Callable, Optional, Union
from typing import Any, Callable, Dict, Optional, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -183,6 +183,7 @@ def dumps( # pylint: disable=too-many-arguments
indent: Union[str, int, None] = None,
separators: Union[tuple[str, str], None] = None,
cls: Union[type[simplejson.JSONEncoder], None] = None,
encoding: Optional[str] = "utf-8",
) -> str:
"""
Dumps object to compatible JSON format
Expand All @@ -199,29 +200,21 @@ def dumps( # pylint: disable=too-many-arguments
"""

results_string = ""
dumps_kwargs: Dict[str, Any] = {
"default": default,
"allow_nan": allow_nan,
"ignore_nan": ignore_nan,
"sort_keys": sort_keys,
"indent": indent,
"separators": separators,
"cls": cls,
"encoding": encoding,
}
try:
results_string = simplejson.dumps(
obj,
default=default,
allow_nan=allow_nan,
ignore_nan=ignore_nan,
sort_keys=sort_keys,
indent=indent,
separators=separators,
cls=cls,
)
results_string = simplejson.dumps(obj, **dumps_kwargs)
except UnicodeDecodeError:
results_string = simplejson.dumps(
obj,
default=default,
allow_nan=allow_nan,
ignore_nan=ignore_nan,
sort_keys=sort_keys,
indent=indent,
separators=separators,
cls=cls,
encoding=None,
)
dumps_kwargs["encoding"] = None
results_string = simplejson.dumps(obj, **dumps_kwargs)
return results_string


Expand Down
Loading