Cleanup deprecated json utils (#121069)

* Cleanup deprectated json utils

* Adjust pylint
This commit is contained in:
epenet 2024-07-03 15:57:32 +02:00 committed by GitHub
parent 8709c668cc
commit 1332e39f9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 0 additions and 92 deletions

View File

@ -2,8 +2,6 @@
from __future__ import annotations
from collections.abc import Callable
import json
import logging
from os import PathLike
from typing import Any
@ -12,8 +10,6 @@ import orjson
from homeassistant.exceptions import HomeAssistantError
from .file import WriteError # noqa: F401
_SENTINEL = object()
_LOGGER = logging.getLogger(__name__)
@ -129,63 +125,9 @@ def load_json_object(
raise HomeAssistantError(f"Expected JSON to be parsed as a dict got {type(value)}")
def save_json(
filename: str,
data: list | dict,
private: bool = False,
*,
encoder: type[json.JSONEncoder] | None = None,
atomic_writes: bool = False,
) -> None:
"""Save JSON data to a file."""
# pylint: disable-next=import-outside-toplevel
from homeassistant.helpers.frame import report
report(
(
"uses save_json from homeassistant.util.json module."
" This is deprecated and will stop working in Home Assistant 2022.4, it"
" should be updated to use homeassistant.helpers.json module instead"
),
error_if_core=False,
)
# pylint: disable-next=import-outside-toplevel
import homeassistant.helpers.json as json_helper
json_helper.save_json(
filename, data, private, encoder=encoder, atomic_writes=atomic_writes
)
def format_unserializable_data(data: dict[str, Any]) -> str:
"""Format output of find_paths in a friendly way.
Format is comma separated: <path>=<value>(<type>)
"""
return ", ".join(f"{path}={value}({type(value)}" for path, value in data.items())
def find_paths_unserializable_data(
bad_data: Any, *, dump: Callable[[Any], str] = json.dumps
) -> dict[str, Any]:
"""Find the paths to unserializable data.
This method is slow! Only use for error handling.
"""
# pylint: disable-next=import-outside-toplevel
from homeassistant.helpers.frame import report
report(
(
"uses find_paths_unserializable_data from homeassistant.util.json module."
" This is deprecated and will stop working in Home Assistant 2022.4, it"
" should be updated to use homeassistant.helpers.json module instead"
),
error_if_core=False,
)
# pylint: disable-next=import-outside-toplevel
import homeassistant.helpers.json as json_helper
return json_helper.find_paths_unserializable_data(bad_data, dump=dump)

View File

@ -392,12 +392,6 @@ _OBSOLETE_IMPORT: dict[str, list[ObsoleteImportMatch]] = {
constant=re.compile(r"^IMPERIAL_SYSTEM$"),
),
],
"homeassistant.util.json": [
ObsoleteImportMatch(
reason="moved to homeassistant.helpers.json",
constant=re.compile(r"^save_json|find_paths_unserializable_data$"),
),
],
}

View File

@ -131,34 +131,6 @@ def test_json_loads_object() -> None:
json_loads_object("null")
async def test_deprecated_test_find_unserializable_data(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test deprecated test_find_unserializable_data logs a warning."""
# pylint: disable-next=hass-deprecated-import,import-outside-toplevel
from homeassistant.util.json import find_paths_unserializable_data
find_paths_unserializable_data(1)
assert (
"uses find_paths_unserializable_data from homeassistant.util.json"
in caplog.text
)
assert "should be updated to use homeassistant.helpers.json module" in caplog.text
async def test_deprecated_save_json(
caplog: pytest.LogCaptureFixture, tmp_path: Path
) -> None:
"""Test deprecated save_json logs a warning."""
# pylint: disable-next=hass-deprecated-import,import-outside-toplevel
from homeassistant.util.json import save_json
fname = tmp_path / "test1.json"
save_json(fname, TEST_JSON_A)
assert "uses save_json from homeassistant.util.json" in caplog.text
assert "should be updated to use homeassistant.helpers.json module" in caplog.text
async def test_loading_derived_class() -> None:
"""Test loading data from classes derived from str."""