mirror of
https://github.com/home-assistant/core.git
synced 2025-04-19 14:57:52 +00:00
Cleanup deprecated json utils (#121069)
* Cleanup deprectated json utils * Adjust pylint
This commit is contained in:
parent
8709c668cc
commit
1332e39f9e
@ -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)
|
||||
|
@ -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$"),
|
||||
),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
|
@ -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."""
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user