Adjust relative-import plugin for tests (#78742)
This commit is contained in:
parent
cd6959d809
commit
0dcbc85684
2 changed files with 28 additions and 14 deletions
|
@ -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,18 +374,17 @@ 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"):
|
||||||
current_component = self.current_package.split(".")[2]
|
if self.current_package.startswith(f"{root}.components."):
|
||||||
if node.modname == "homeassistant.components":
|
current_component = self.current_package.split(".")[2]
|
||||||
for name in node.names:
|
if node.modname == f"{root}.components":
|
||||||
if name[0] == current_component:
|
for name in node.names:
|
||||||
self.add_message("hass-relative-import", node=node)
|
if name[0] == current_component:
|
||||||
return
|
self.add_message("hass-relative-import", node=node)
|
||||||
if node.modname.startswith(
|
return
|
||||||
f"homeassistant.components.{current_component}."
|
if node.modname.startswith(f"{root}.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 (
|
||||||
node.modname.endswith(".const")
|
node.modname.endswith(".const")
|
||||||
or "const" in {names[0] for names in node.names}
|
or "const" in {names[0] for names in node.names}
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue