Use deprecated_class decorator in deprecated YAML loader classes (#104835)
parent
99f28c7163
commit
00e57ab9a4
|
@ -23,7 +23,7 @@ except ImportError:
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.frame import report
|
from homeassistant.helpers.deprecation import deprecated_class
|
||||||
|
|
||||||
from .const import SECRET_YAML
|
from .const import SECRET_YAML
|
||||||
from .objects import Input, NodeDictClass, NodeListClass, NodeStrClass
|
from .objects import Input, NodeDictClass, NodeListClass, NodeStrClass
|
||||||
|
@ -137,17 +137,10 @@ class FastSafeLoader(FastestAvailableSafeLoader, _LoaderMixin):
|
||||||
self.secrets = secrets
|
self.secrets = secrets
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated_class("FastSafeLoader")
|
||||||
class SafeLoader(FastSafeLoader):
|
class SafeLoader(FastSafeLoader):
|
||||||
"""Provided for backwards compatibility. Logs when instantiated."""
|
"""Provided for backwards compatibility. Logs when instantiated."""
|
||||||
|
|
||||||
def __init__(*args: Any, **kwargs: Any) -> None:
|
|
||||||
"""Log a warning and call super."""
|
|
||||||
report(
|
|
||||||
"uses deprecated 'SafeLoader' instead of 'FastSafeLoader', "
|
|
||||||
"which will stop working in HA Core 2024.6,"
|
|
||||||
)
|
|
||||||
FastSafeLoader.__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class PythonSafeLoader(yaml.SafeLoader, _LoaderMixin):
|
class PythonSafeLoader(yaml.SafeLoader, _LoaderMixin):
|
||||||
"""Python safe loader."""
|
"""Python safe loader."""
|
||||||
|
@ -158,17 +151,10 @@ class PythonSafeLoader(yaml.SafeLoader, _LoaderMixin):
|
||||||
self.secrets = secrets
|
self.secrets = secrets
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated_class("PythonSafeLoader")
|
||||||
class SafeLineLoader(PythonSafeLoader):
|
class SafeLineLoader(PythonSafeLoader):
|
||||||
"""Provided for backwards compatibility. Logs when instantiated."""
|
"""Provided for backwards compatibility. Logs when instantiated."""
|
||||||
|
|
||||||
def __init__(*args: Any, **kwargs: Any) -> None:
|
|
||||||
"""Log a warning and call super."""
|
|
||||||
report(
|
|
||||||
"uses deprecated 'SafeLineLoader' instead of 'PythonSafeLoader', "
|
|
||||||
"which will stop working in HA Core 2024.6,"
|
|
||||||
)
|
|
||||||
PythonSafeLoader.__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
LoaderType = FastSafeLoader | PythonSafeLoader
|
LoaderType = FastSafeLoader | PythonSafeLoader
|
||||||
|
|
||||||
|
|
|
@ -590,7 +590,7 @@ async def test_loading_actual_file_with_syntax_error(
|
||||||
def mock_integration_frame() -> Generator[Mock, None, None]:
|
def mock_integration_frame() -> Generator[Mock, None, None]:
|
||||||
"""Mock as if we're calling code from inside an integration."""
|
"""Mock as if we're calling code from inside an integration."""
|
||||||
correct_frame = Mock(
|
correct_frame = Mock(
|
||||||
filename="/home/paulus/homeassistant/components/hue/light.py",
|
filename="/home/paulus/.homeassistant/custom_components/hue/light.py",
|
||||||
lineno="23",
|
lineno="23",
|
||||||
line="self.light.is_on",
|
line="self.light.is_on",
|
||||||
)
|
)
|
||||||
|
@ -614,12 +614,12 @@ def mock_integration_frame() -> Generator[Mock, None, None]:
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("loader_class", "message"),
|
("loader_class", "new_class"),
|
||||||
[
|
[
|
||||||
(yaml.loader.SafeLoader, "'SafeLoader' instead of 'FastSafeLoader'"),
|
(yaml.loader.SafeLoader, "FastSafeLoader"),
|
||||||
(
|
(
|
||||||
yaml.loader.SafeLineLoader,
|
yaml.loader.SafeLineLoader,
|
||||||
"'SafeLineLoader' instead of 'PythonSafeLoader'",
|
"PythonSafeLoader",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -628,14 +628,17 @@ async def test_deprecated_loaders(
|
||||||
mock_integration_frame: Mock,
|
mock_integration_frame: Mock,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
loader_class,
|
loader_class,
|
||||||
message: str,
|
new_class: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test instantiating the deprecated yaml loaders logs a warning."""
|
"""Test instantiating the deprecated yaml loaders logs a warning."""
|
||||||
with pytest.raises(TypeError), patch(
|
with pytest.raises(TypeError), patch(
|
||||||
"homeassistant.helpers.frame._REPORTED_INTEGRATIONS", set()
|
"homeassistant.helpers.frame._REPORTED_INTEGRATIONS", set()
|
||||||
):
|
):
|
||||||
loader_class()
|
loader_class()
|
||||||
assert (f"Detected that integration 'hue' uses deprecated {message}") in caplog.text
|
assert (
|
||||||
|
f"{loader_class.__name__} was called from hue, this is a deprecated class. "
|
||||||
|
f"Use {new_class} instead"
|
||||||
|
) in caplog.text
|
||||||
|
|
||||||
|
|
||||||
def test_string_annotated(try_both_loaders) -> None:
|
def test_string_annotated(try_both_loaders) -> None:
|
||||||
|
|
Loading…
Reference in New Issue