Remove deprecated json helper constants and function (#150111)

This commit is contained in:
G Johansson
2025-08-13 12:42:00 +02:00
committed by GitHub
parent 7fba0ca2c0
commit f39305f64e
3 changed files with 5 additions and 89 deletions

View File

@@ -12,39 +12,7 @@ from typing import TYPE_CHECKING, Any, Final
import orjson import orjson
from homeassistant.util.file import write_utf8_file, write_utf8_file_atomic from homeassistant.util.file import write_utf8_file, write_utf8_file_atomic
from homeassistant.util.json import ( from homeassistant.util.json import SerializationError, format_unserializable_data
JSON_DECODE_EXCEPTIONS as _JSON_DECODE_EXCEPTIONS,
JSON_ENCODE_EXCEPTIONS as _JSON_ENCODE_EXCEPTIONS,
SerializationError,
format_unserializable_data,
json_loads as _json_loads,
)
from .deprecation import (
DeprecatedConstant,
all_with_deprecated_constants,
check_if_deprecated_constant,
deprecated_function,
dir_with_deprecated_constants,
)
_DEPRECATED_JSON_DECODE_EXCEPTIONS = DeprecatedConstant(
_JSON_DECODE_EXCEPTIONS, "homeassistant.util.json.JSON_DECODE_EXCEPTIONS", "2025.8"
)
_DEPRECATED_JSON_ENCODE_EXCEPTIONS = DeprecatedConstant(
_JSON_ENCODE_EXCEPTIONS, "homeassistant.util.json.JSON_ENCODE_EXCEPTIONS", "2025.8"
)
json_loads = deprecated_function(
"homeassistant.util.json.json_loads", breaks_in_ha_version="2025.8"
)(_json_loads)
# These can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(
dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
)
__all__ = all_with_deprecated_constants(globals())
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@@ -10,6 +10,7 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import CoreState, HomeAssistant from homeassistant.core import CoreState, HomeAssistant
from homeassistant.helpers import discovery_flow, json as json_helper from homeassistant.helpers import discovery_flow, json as json_helper
from homeassistant.helpers.discovery_flow import DiscoveryKey from homeassistant.helpers.discovery_flow import DiscoveryKey
from homeassistant.util import json as json_util
@pytest.fixture @pytest.fixture
@@ -151,6 +152,6 @@ def test_discovery_key_serialize_deserialize(key: str | tuple[str]) -> None:
) )
serialized = json_helper.json_dumps(discovery_key_1) serialized = json_helper.json_dumps(discovery_key_1)
assert ( assert (
discovery_flow.DiscoveryKey.from_json_dict(json_helper.json_loads(serialized)) discovery_flow.DiscoveryKey.from_json_dict(json_util.json_loads(serialized))
== discovery_key_1 == discovery_key_1
) )

View File

@@ -13,7 +13,6 @@ from unittest.mock import Mock, patch
import pytest import pytest
from homeassistant.core import Event, HomeAssistant, State from homeassistant.core import Event, HomeAssistant, State
from homeassistant.helpers import json as json_helper
from homeassistant.helpers.json import ( from homeassistant.helpers.json import (
ExtendedJSONEncoder, ExtendedJSONEncoder,
JSONEncoder as DefaultHASSJSONEncoder, JSONEncoder as DefaultHASSJSONEncoder,
@@ -27,14 +26,9 @@ from homeassistant.helpers.json import (
) )
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from homeassistant.util.color import RGBColor from homeassistant.util.color import RGBColor
from homeassistant.util.json import ( from homeassistant.util.json import SerializationError, load_json
JSON_DECODE_EXCEPTIONS,
JSON_ENCODE_EXCEPTIONS,
SerializationError,
load_json,
)
from tests.common import import_and_test_deprecated_constant, json_round_trip from tests.common import json_round_trip
# Test data that can be saved as JSON # Test data that can be saved as JSON
TEST_JSON_A = {"a": 1, "B": "two"} TEST_JSON_A = {"a": 1, "B": "two"}
@@ -350,50 +344,3 @@ def test_find_unserializable_data() -> None:
BadData(), BadData(),
dump=partial(json.dumps, cls=MockJSONEncoder), dump=partial(json.dumps, cls=MockJSONEncoder),
) == {"$(BadData).bla": bad_data} ) == {"$(BadData).bla": bad_data}
def test_deprecated_json_loads(caplog: pytest.LogCaptureFixture) -> None:
"""Test deprecated json_loads function.
It was moved from helpers to util in #88099
"""
json_helper.json_loads("{}")
assert (
"The deprecated function json_loads was called. It will be removed "
"in HA Core 2025.8. Use homeassistant.util.json.json_loads instead"
) in caplog.text
@pytest.mark.parametrize(
("constant_name", "replacement_name", "replacement"),
[
(
"JSON_DECODE_EXCEPTIONS",
"homeassistant.util.json.JSON_DECODE_EXCEPTIONS",
JSON_DECODE_EXCEPTIONS,
),
(
"JSON_ENCODE_EXCEPTIONS",
"homeassistant.util.json.JSON_ENCODE_EXCEPTIONS",
JSON_ENCODE_EXCEPTIONS,
),
],
)
def test_deprecated_aliases(
caplog: pytest.LogCaptureFixture,
constant_name: str,
replacement_name: str,
replacement: Any,
) -> None:
"""Test deprecated JSON_DECODE_EXCEPTIONS and JSON_ENCODE_EXCEPTIONS constants.
They were moved from helpers to util in #88099
"""
import_and_test_deprecated_constant(
caplog,
json_helper,
constant_name,
replacement_name,
replacement,
"2025.8",
)