mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Warn about deprecated constants only once per caller (#195805)
This commit is contained in:
@@ -362,13 +362,14 @@ class DeferredDeprecatedAlias[T]:
|
||||
|
||||
|
||||
_PREFIX_DEPRECATED = "_DEPRECATED_"
|
||||
_REPORTED_DEPRECATED_CONSTANTS: set[tuple[str | None, str, str, int]] = set()
|
||||
|
||||
|
||||
def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> Any:
|
||||
"""Check if the not found name is a deprecated constant.
|
||||
|
||||
If it is, print a deprecation warning and return the value of the constant.
|
||||
Otherwise raise AttributeError.
|
||||
If it is, print a deprecation warning once per caller and return the value
|
||||
of the constant. Otherwise raise AttributeError.
|
||||
"""
|
||||
module_name = module_globals.get("__name__")
|
||||
value = replacement = None
|
||||
@@ -406,6 +407,12 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A
|
||||
# https://peps.python.org/pep-0562/#specification
|
||||
raise AttributeError(msg)
|
||||
|
||||
caller = sys._getframe(1) # noqa: SLF001
|
||||
key = (module_name, name, caller.f_code.co_filename, caller.f_lineno)
|
||||
if key in _REPORTED_DEPRECATED_CONSTANTS:
|
||||
return value
|
||||
_REPORTED_DEPRECATED_CONSTANTS.add(key)
|
||||
|
||||
_print_deprecation_warning_internal(
|
||||
name,
|
||||
module_name or __name__,
|
||||
|
||||
@@ -538,6 +538,7 @@ def reset_globals() -> Generator[None]:
|
||||
# Reset the frame helper globals
|
||||
frame.async_setup(None)
|
||||
frame._REPORTED_INTEGRATIONS.clear()
|
||||
deprecation._REPORTED_DEPRECATED_CONSTANTS.clear()
|
||||
deprecation._REPORTED_DEPRECATED_ENTITY_USAGE.clear()
|
||||
|
||||
# Reset patch_json
|
||||
|
||||
@@ -566,6 +566,45 @@ def test_check_if_deprecated_constant_integration_not_found(
|
||||
) not in caplog.record_tuples
|
||||
|
||||
|
||||
def test_check_if_deprecated_constant_warns_once_per_caller(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test check_if_deprecated_constant warns once per caller."""
|
||||
module_name = "config.custom_components.hue.light"
|
||||
module_globals = {
|
||||
"__name__": module_name,
|
||||
"_DEPRECATED_TEST_CONSTANT": DeprecatedConstant("value", "NEW_CONSTANT", None),
|
||||
}
|
||||
filename = "/home/paulus/config/custom_components/hue/light.py"
|
||||
|
||||
with (
|
||||
patch.dict(sys.modules, {module_name: Mock(__file__=filename)}),
|
||||
patch(
|
||||
"homeassistant.helpers.frame.get_current_frame",
|
||||
return_value=extract_stack_to_frame(
|
||||
[Mock(filename=filename, lineno="23", line="self.name")]
|
||||
),
|
||||
),
|
||||
):
|
||||
for _ in range(2):
|
||||
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
|
||||
assert value == "value"
|
||||
assert len(caplog.record_tuples) == 1
|
||||
|
||||
# A different caller is warned about separately
|
||||
check_if_deprecated_constant("TEST_CONSTANT", module_globals)
|
||||
assert len(caplog.record_tuples) == 2
|
||||
|
||||
assert set(caplog.record_tuples) == {
|
||||
(
|
||||
module_name,
|
||||
logging.WARNING,
|
||||
"The deprecated constant TEST_CONSTANT was used from hue. Use NEW_CONSTANT"
|
||||
" instead, please report it to the author of the 'hue' custom integration",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
def test_test_check_if_deprecated_constant_invalid(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user