Adjust relative-import plugin for tests (#78742)

This commit is contained in:
epenet 2022-09-19 12:57:07 +02:00 committed by GitHub
parent cd6959d809
commit 0dcbc85684
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View file

@ -345,8 +345,10 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
self, current_package: str, node: nodes.ImportFrom self, current_package: str, node: nodes.ImportFrom
) -> None: ) -> None:
"""Called when a ImportFrom node is visited.""" """Called when a ImportFrom node is visited."""
if node.level <= 1 or not current_package.startswith( if (
"homeassistant.components" node.level <= 1
or not current_package.startswith("homeassistant.components.")
and not current_package.startswith("tests.components.")
): ):
return return
split_package = current_package.split(".") split_package = current_package.split(".")
@ -372,16 +374,15 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
): ):
self.add_message("hass-relative-import", node=node) self.add_message("hass-relative-import", node=node)
return return
if self.current_package.startswith("homeassistant.components."): for root in ("homeassistant", "tests"):
if self.current_package.startswith(f"{root}.components."):
current_component = self.current_package.split(".")[2] current_component = self.current_package.split(".")[2]
if node.modname == "homeassistant.components": if node.modname == f"{root}.components":
for name in node.names: for name in node.names:
if name[0] == current_component: if name[0] == current_component:
self.add_message("hass-relative-import", node=node) self.add_message("hass-relative-import", node=node)
return return
if node.modname.startswith( if node.modname.startswith(f"{root}.components.{current_component}."):
f"homeassistant.components.{current_component}."
):
self.add_message("hass-relative-import", node=node) self.add_message("hass-relative-import", node=node)
return return
if node.modname.startswith("homeassistant.components.") and ( if node.modname.startswith("homeassistant.components.") and (

View file

@ -35,6 +35,7 @@ from . import assert_adds_messages, assert_no_messages
("homeassistant.components.pylint_test.api.hub", "..const", "CONSTANT"), ("homeassistant.components.pylint_test.api.hub", "..const", "CONSTANT"),
("homeassistant.components.pylint_test.api.hub", "..", "CONSTANT"), ("homeassistant.components.pylint_test.api.hub", "..", "CONSTANT"),
("homeassistant.components.pylint_test.api.hub", "...", "pylint_test"), ("homeassistant.components.pylint_test.api.hub", "...", "pylint_test"),
("tests.components.pylint_test.api.hub", "..const", "CONSTANT"),
], ],
) )
def test_good_import( def test_good_import(
@ -101,6 +102,18 @@ def test_good_import(
"CONSTANT", "CONSTANT",
"hass-relative-import", "hass-relative-import",
), ),
(
"tests.components.pylint_test.api.hub",
"tests.components.pylint_test.const",
"CONSTANT",
"hass-relative-import",
),
(
"tests.components.pylint_test.api.hub",
"...const",
"CONSTANT",
"hass-absolute-import",
),
], ],
) )
def test_bad_import( def test_bad_import(