Add warnings for deprecated json helpers (#121161)
parent
04a6285e62
commit
fe0bafd067
|
@ -13,13 +13,39 @@ import orjson
|
|||
|
||||
from homeassistant.util.file import write_utf8_file, write_utf8_file_atomic
|
||||
from homeassistant.util.json import ( # noqa: F401
|
||||
JSON_DECODE_EXCEPTIONS,
|
||||
JSON_ENCODE_EXCEPTIONS,
|
||||
JSON_DECODE_EXCEPTIONS as _JSON_DECODE_EXCEPTIONS,
|
||||
JSON_ENCODE_EXCEPTIONS as _JSON_ENCODE_EXCEPTIONS,
|
||||
SerializationError,
|
||||
format_unserializable_data,
|
||||
json_loads,
|
||||
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__)
|
||||
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ from unittest.mock import Mock, patch
|
|||
import pytest
|
||||
|
||||
from homeassistant.core import Event, HomeAssistant, State
|
||||
from homeassistant.helpers import json as json_helper
|
||||
from homeassistant.helpers.json import (
|
||||
ExtendedJSONEncoder,
|
||||
JSONEncoder as DefaultHASSJSONEncoder,
|
||||
|
@ -25,9 +26,14 @@ from homeassistant.helpers.json import (
|
|||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.color import RGBColor
|
||||
from homeassistant.util.json import SerializationError, load_json
|
||||
from homeassistant.util.json import (
|
||||
JSON_DECODE_EXCEPTIONS,
|
||||
JSON_ENCODE_EXCEPTIONS,
|
||||
SerializationError,
|
||||
load_json,
|
||||
)
|
||||
|
||||
from tests.common import json_round_trip
|
||||
from tests.common import import_and_test_deprecated_constant, json_round_trip
|
||||
|
||||
# Test data that can be saved as JSON
|
||||
TEST_JSON_A = {"a": 1, "B": "two"}
|
||||
|
@ -335,3 +341,50 @@ def test_find_unserializable_data() -> None:
|
|||
BadData(),
|
||||
dump=partial(json.dumps, cls=MockJSONEncoder),
|
||||
) == {"$(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 (
|
||||
"json_loads is a deprecated function which 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",
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue