mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 03:35:09 +02:00
Avoid constant alias for integration DOMAIN (#145788)
* Avoid constant alias for integration DOMAIN * Tweak * Improve * Three more --------- Co-authored-by: Shay Levy <levyshay1@gmail.com>
This commit is contained in:
@@ -233,6 +233,11 @@ class HassImportsFormatChecker(BaseChecker):
|
||||
"hass-import-constant-alias",
|
||||
"Used when a constant should be imported as an alias",
|
||||
),
|
||||
"W7427": (
|
||||
"`%s` alias is unnecessary for `%s`",
|
||||
"hass-import-constant-unnecessary-alias",
|
||||
"Used when a constant alias is unnecessary",
|
||||
),
|
||||
}
|
||||
options = ()
|
||||
|
||||
@@ -274,16 +279,24 @@ class HassImportsFormatChecker(BaseChecker):
|
||||
self, current_package: str, node: nodes.ImportFrom
|
||||
) -> None:
|
||||
"""Check for improper 'from ._ import _' invocations."""
|
||||
if node.level <= 1 or (
|
||||
not current_package.startswith("homeassistant.components.")
|
||||
and not current_package.startswith("tests.components.")
|
||||
if not current_package.startswith(
|
||||
("homeassistant.components.", "tests.components.")
|
||||
):
|
||||
return
|
||||
|
||||
split_package = current_package.split(".")
|
||||
current_component = split_package[2]
|
||||
|
||||
self._check_for_constant_alias(node, current_component, current_component)
|
||||
|
||||
if node.level <= 1:
|
||||
# No need to check relative import
|
||||
return
|
||||
|
||||
if not node.modname and len(split_package) == node.level + 1:
|
||||
for name in node.names:
|
||||
# Allow relative import to component root
|
||||
if name[0] != split_package[2]:
|
||||
if name[0] != current_component:
|
||||
self.add_message("hass-absolute-import", node=node)
|
||||
return
|
||||
return
|
||||
@@ -298,6 +311,15 @@ class HassImportsFormatChecker(BaseChecker):
|
||||
) -> bool:
|
||||
"""Check for hass-import-constant-alias."""
|
||||
if current_component == imported_component:
|
||||
# Check for `from homeassistant.components.self import DOMAIN as XYZ`
|
||||
for name, alias in node.names:
|
||||
if name == "DOMAIN" and (alias is not None and alias != "DOMAIN"):
|
||||
self.add_message(
|
||||
"hass-import-constant-unnecessary-alias",
|
||||
node=node,
|
||||
args=(alias, "DOMAIN"),
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
# Check for `from homeassistant.components.other import DOMAIN`
|
||||
|
Reference in New Issue
Block a user