mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Add location details to deprecation warning (#47155)
This commit is contained in:
parent
f3c74948c3
commit
f86e7535e0
@ -746,12 +746,21 @@ def deprecated(
|
|||||||
def validator(config: Dict) -> Dict:
|
def validator(config: Dict) -> Dict:
|
||||||
"""Check if key is in config and log warning."""
|
"""Check if key is in config and log warning."""
|
||||||
if key in config:
|
if key in config:
|
||||||
|
try:
|
||||||
|
KeywordStyleAdapter(logging.getLogger(module_name)).warning(
|
||||||
|
warning.replace(
|
||||||
|
"'{key}' option",
|
||||||
|
f"'{key}' option near {config.__config_file__}:{config.__line__}", # type: ignore
|
||||||
|
),
|
||||||
|
key=key,
|
||||||
|
replacement_key=replacement_key,
|
||||||
|
)
|
||||||
|
except AttributeError:
|
||||||
KeywordStyleAdapter(logging.getLogger(module_name)).warning(
|
KeywordStyleAdapter(logging.getLogger(module_name)).warning(
|
||||||
warning,
|
warning,
|
||||||
key=key,
|
key=key,
|
||||||
replacement_key=replacement_key,
|
replacement_key=replacement_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
value = config[key]
|
value = config[key]
|
||||||
if replacement_key:
|
if replacement_key:
|
||||||
config.pop(key)
|
config.pop(key)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""Test config validators."""
|
"""Test config validators."""
|
||||||
|
from collections import OrderedDict
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
import enum
|
import enum
|
||||||
import os
|
import os
|
||||||
@ -799,6 +800,77 @@ def test_deprecated_cant_find_module():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_deprecated_logger_with_config_attributes(caplog):
|
||||||
|
"""Test if the logger outputs the correct message if the line and file attribute is available in config."""
|
||||||
|
file: str = "configuration.yaml"
|
||||||
|
line: int = 54
|
||||||
|
replacement = f"'mars' option near {file}:{line} is deprecated"
|
||||||
|
config = OrderedDict([("mars", "blah")])
|
||||||
|
setattr(config, "__config_file__", file)
|
||||||
|
setattr(config, "__line__", line)
|
||||||
|
|
||||||
|
cv.deprecated("mars", replacement_key="jupiter", default=False)(config)
|
||||||
|
|
||||||
|
assert len(caplog.records) == 1
|
||||||
|
assert replacement in caplog.text
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
|
assert len(caplog.records) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_deprecated_logger_with_one_config_attribute(caplog):
|
||||||
|
"""Test if the logger outputs the correct message if only one of line and file attribute is available in config."""
|
||||||
|
file: str = "configuration.yaml"
|
||||||
|
line: int = 54
|
||||||
|
replacement = f"'mars' option near {file}:{line} is deprecated"
|
||||||
|
config = OrderedDict([("mars", "blah")])
|
||||||
|
setattr(config, "__config_file__", file)
|
||||||
|
|
||||||
|
cv.deprecated("mars", replacement_key="jupiter", default=False)(config)
|
||||||
|
|
||||||
|
assert len(caplog.records) == 1
|
||||||
|
assert replacement not in caplog.text
|
||||||
|
assert (
|
||||||
|
"The 'mars' option is deprecated, please replace it with 'jupiter'"
|
||||||
|
) in caplog.text
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
|
assert len(caplog.records) == 0
|
||||||
|
|
||||||
|
config = OrderedDict([("mars", "blah")])
|
||||||
|
setattr(config, "__line__", line)
|
||||||
|
|
||||||
|
cv.deprecated("mars", replacement_key="jupiter", default=False)(config)
|
||||||
|
|
||||||
|
assert len(caplog.records) == 1
|
||||||
|
assert replacement not in caplog.text
|
||||||
|
assert (
|
||||||
|
"The 'mars' option is deprecated, please replace it with 'jupiter'"
|
||||||
|
) in caplog.text
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
|
assert len(caplog.records) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_deprecated_logger_without_config_attributes(caplog):
|
||||||
|
"""Test if the logger outputs the correct message if the line and file attribute is not available in config."""
|
||||||
|
file: str = "configuration.yaml"
|
||||||
|
line: int = 54
|
||||||
|
replacement = f"'mars' option near {file}:{line} is deprecated"
|
||||||
|
config = OrderedDict([("mars", "blah")])
|
||||||
|
|
||||||
|
cv.deprecated("mars", replacement_key="jupiter", default=False)(config)
|
||||||
|
|
||||||
|
assert len(caplog.records) == 1
|
||||||
|
assert replacement not in caplog.text
|
||||||
|
assert (
|
||||||
|
"The 'mars' option is deprecated, please replace it with 'jupiter'"
|
||||||
|
) in caplog.text
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
|
assert len(caplog.records) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_key_dependency():
|
def test_key_dependency():
|
||||||
"""Test key_dependency validator."""
|
"""Test key_dependency validator."""
|
||||||
schema = vol.Schema(cv.key_dependency("beer", "soda"))
|
schema = vol.Schema(cv.key_dependency("beer", "soda"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user