Minor tweak of cv.deprecated + cv.removed (#59095)

* Minor tweak of cv.deprecated + cv.removed

* Satisfy pylint
This commit is contained in:
Erik Montnemery 2021-11-04 19:12:21 +01:00 committed by GitHub
parent 491e62792b
commit a62bc6b3b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 32 deletions

View file

@ -710,10 +710,10 @@ class multi_select:
def _deprecated_or_removed( def _deprecated_or_removed(
key: str, key: str,
replacement_key: str | None = None, replacement_key: str | None,
default: Any | None = None, default: Any | None,
raise_if_present: bool | None = False, raise_if_present: bool,
option_status: str | None = "is deprecated", option_removed: bool,
) -> Callable[[dict], dict]: ) -> Callable[[dict], dict]:
""" """
Log key as deprecated and provide a replacement (if exists) or fail. Log key as deprecated and provide a replacement (if exists) or fail.
@ -735,36 +735,34 @@ def _deprecated_or_removed(
# will be missing information, so let's guard. # will be missing information, so let's guard.
# https://github.com/home-assistant/core/issues/24982 # https://github.com/home-assistant/core/issues/24982
module_name = __name__ module_name = __name__
if replacement_key: if option_removed:
warning = ( logger_func = logging.getLogger(module_name).error
"The '{key}' option {option_status}," option_status = "has been removed"
" please replace it with '{replacement_key}'"
)
else: else:
warning = ( logger_func = logging.getLogger(module_name).warning
"The '{key}' option {option_status}," option_status = "is deprecated"
" please remove it from your configuration"
)
def validator(config: dict) -> dict: def validator(config: dict) -> dict:
"""Check if key is in config and log warning or error.""" """Check if key is in config and log warning or error."""
if key in config: if key in config:
try: try:
warning_local = warning.replace( near = f"near {config.__config_file__}:{config.__line__} " # type: ignore
"'{key}' option",
f"'{key}' option near {config.__config_file__}:{config.__line__}", # type: ignore
)
except AttributeError: except AttributeError:
warning_local = warning near = ""
warning_local = warning_local.format( arguments: tuple[str, ...]
key=key, if replacement_key:
replacement_key=replacement_key, warning = "The '%s' option %s%s, please replace it with '%s'"
option_status=option_status, arguments = (key, near, option_status, replacement_key)
) else:
if raise_if_present: warning = (
raise vol.Invalid(warning_local) "The '%s' option %s%s, please remove it from your configuration"
)
arguments = (key, near, option_status)
logging.getLogger(module_name).warning(warning_local) if raise_if_present:
raise vol.Invalid(warning % arguments)
logger_func(warning, *arguments)
value = config[key] value = config[key]
if replacement_key: if replacement_key:
config.pop(key) config.pop(key)
@ -805,8 +803,8 @@ def deprecated(
key, key,
replacement_key=replacement_key, replacement_key=replacement_key,
default=default, default=default,
raise_if_present=raise_if_present, raise_if_present=raise_if_present or False,
option_status="is deprecated", option_removed=False,
) )
@ -825,8 +823,8 @@ def removed(
key, key,
replacement_key=None, replacement_key=None,
default=default, default=default,
raise_if_present=raise_if_present, raise_if_present=raise_if_present or False,
option_status="was removed", option_removed=True,
) )

View file

@ -741,7 +741,7 @@ def test_deprecated_or_removed_param_and_raise(caplog, schema):
with pytest.raises(vol.Invalid) as excinfo: with pytest.raises(vol.Invalid) as excinfo:
deprecated_schema(test_data) deprecated_schema(test_data)
assert ( assert (
"The 'mars' option was removed, please remove it from your configuration" "The 'mars' option has been removed, please remove it from your configuration"
in str(excinfo.value) in str(excinfo.value)
) )
assert len(caplog.records) == 0 assert len(caplog.records) == 0
@ -916,7 +916,7 @@ def test_deprecated_or_removed_logger_with_config_attributes(caplog):
assert len(caplog.records) == 0 assert len(caplog.records) == 0
# test as removed option # test as removed option
option_status = "was removed" option_status = "has been removed"
replacement = f"'mars' option near {file}:{line} {option_status}, please remove it from your configuration" replacement = f"'mars' option near {file}:{line} {option_status}, please remove it from your configuration"
config = OrderedDict([("mars", "blah")]) config = OrderedDict([("mars", "blah")])
setattr(config, "__config_file__", file) setattr(config, "__config_file__", file)