mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Force root import of const from other components (#78014)
* Force root import of const from other components * Add missing commit * Add tests * Add tests * Apply suggestion Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> * Apply suggestion Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
@@ -310,6 +310,12 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
|
||||
"hass-absolute-import",
|
||||
"Used when relative import should be replaced with absolute import",
|
||||
),
|
||||
"W7424": (
|
||||
"Import should be using the component root",
|
||||
"hass-component-root-import",
|
||||
"Used when an import from another component should be "
|
||||
"from the component root",
|
||||
),
|
||||
}
|
||||
options = ()
|
||||
|
||||
@@ -330,6 +336,10 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
|
||||
for module, _alias in node.names:
|
||||
if module.startswith(f"{self.current_package}."):
|
||||
self.add_message("hass-relative-import", node=node)
|
||||
if module.startswith("homeassistant.components.") and module.endswith(
|
||||
"const"
|
||||
):
|
||||
self.add_message("hass-component-root-import", node=node)
|
||||
|
||||
def _visit_importfrom_relative(
|
||||
self, current_package: str, node: nodes.ImportFrom
|
||||
@@ -374,6 +384,12 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
|
||||
):
|
||||
self.add_message("hass-relative-import", node=node)
|
||||
return
|
||||
if node.modname.startswith("homeassistant.components.") and (
|
||||
node.modname.endswith(".const")
|
||||
or "const" in {names[0] for names in node.names}
|
||||
):
|
||||
self.add_message("hass-component-root-import", node=node)
|
||||
return
|
||||
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):
|
||||
for name_tuple in node.names:
|
||||
for obsolete_import in obsolete_imports:
|
||||
|
||||
Reference in New Issue
Block a user