Adjust pylint plugin for tests directory (#78727)

* Add module_name to parametrize

* Add tests for tests directory

* Apply patch from mib1185

* Adjust plugin to allow imports from component being tested
This commit is contained in:
epenet
2022-09-19 14:27:21 +02:00
committed by GitHub
parent 3eab4a234b
commit 4b813f2460
2 changed files with 67 additions and 9 deletions

View File

@@ -333,12 +333,22 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
def visit_import(self, node: nodes.Import) -> None:
"""Called when a Import node is visited."""
if self.current_package is None:
return
for module, _alias in node.names:
if module.startswith(f"{self.current_package}."):
self.add_message("hass-relative-import", node=node)
continue
if module.startswith("homeassistant.components.") and module.endswith(
"const"
):
if (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == module.split(".")[2]
):
# Ignore check if the component being tested matches
# the component being imported from
continue
self.add_message("hass-component-root-import", node=node)
def _visit_importfrom_relative(
@@ -389,6 +399,13 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
node.modname.endswith(".const")
or "const" in {names[0] for names in node.names}
):
if (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == node.modname.split(".")[2]
):
# Ignore check if the component being tested matches
# the component being imported from
return
self.add_message("hass-component-root-import", node=node)
return
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):