Skip to content

Commit

Permalink
Fix json decode error for users using simplejson instead of built-in …
Browse files Browse the repository at this point in the history
…json (#1923)
  • Loading branch information
haakonvt authored Sep 12, 2024
1 parent 44f724b commit 26dc07b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.59.3] - 2024-09-12
### Fixed
- JSONDecodeError can no longer be raised in environments where simplejson is used instead of built-in json.

## [7.59.2] - 2024-09-12
### Fixed
- A bug in `client.sequences.data.retrieve_dataframe(...)` where passing a column to `column_external_ids` caused a TypeError.
Expand Down
5 changes: 3 additions & 2 deletions cognite/client/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import re
import warnings
from collections import UserList
from json.decoder import JSONDecodeError
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -27,6 +26,7 @@

import requests.utils
from requests import Response
from requests.exceptions import JSONDecodeError as RequestsJSONDecodeError
from requests.structures import CaseInsensitiveDict

from cognite.client._http_client import HTTPClient, HTTPClientConfig, get_global_requests_session
Expand Down Expand Up @@ -63,6 +63,7 @@
IdentifierSequenceCore,
SingletonIdentifierSequence,
)
from cognite.client.utils._json import JSONDecodeError
from cognite.client.utils._text import convert_all_keys_to_camel_case, shorten, to_camel_case, to_snake_case
from cognite.client.utils._validation import assert_type, verify_limit
from cognite.client.utils.useful_types import SequenceNotStr
Expand Down Expand Up @@ -1338,7 +1339,7 @@ def _log_request(self, res: Response, **kwargs: Any) -> None:
def _get_response_content_safe(res: Response) -> str:
try:
return _json.dumps(res.json())
except JSONDecodeError:
except (JSONDecodeError, RequestsJSONDecodeError):
pass

try:
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.59.2"
__version__ = "7.59.3"
__api_subversion__ = "20230101"
13 changes: 11 additions & 2 deletions cognite/client/utils/_json.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from __future__ import annotations

import json
import math
import numbers
from decimal import Decimal
from types import MappingProxyType
from typing import Any

__all__ = ["dumps", "loads", "convert_to_float", "convert_nonfinite_float_to_str"]
# Users are seeing JSONDecodeError coming from a block that intercepts JSONDecodeError
# (i.e. shouldn't be possible). It seems Python runtimes like e.g. Databricks patches the
# built-in json library with simplejson and thus simplejson.JSONDecodeError != json.JSONDecodeError
try:
import simplejson as json
from simplejson import JSONDecodeError
except ImportError:
import json # type: ignore [no-redef]
from json import JSONDecodeError # type: ignore [assignment]

__all__ = ["dumps", "loads", "JSONDecodeError", "convert_to_float", "convert_nonfinite_float_to_str"]


def _default_json_encoder(obj: Any) -> Any:
Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[tool.poetry]
name = "cognite-sdk"


version = "7.59.2"
version = "7.59.3"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down Expand Up @@ -88,6 +87,7 @@ packaging = ">=23"
IPython = ">=7.0.0" # Used in docstring examples
PyYAML = "^6.0"
pytest-icdiff = "*" # Used for better diffs in pytest
types-simplejson = "^3.19.0.20240801"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 26dc07b

Please sign in to comment.