Adjust pylint plugin for relative imports (#78277)

This commit is contained in:
epenet 2022-09-14 00:11:57 +02:00 committed by GitHub
parent d3be06906b
commit f1c7fb7866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View File

@ -362,14 +362,18 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
):
self.add_message("hass-relative-import", node=node)
return
if (
self.current_package.startswith("homeassistant.components")
and node.modname == "homeassistant.components"
):
for name in node.names:
if name[0] == self.current_package.split(".")[2]:
self.add_message("hass-relative-import", node=node)
return
if self.current_package.startswith("homeassistant.components."):
current_component = self.current_package.split(".")[2]
if node.modname == "homeassistant.components":
for name in node.names:
if name[0] == current_component:
self.add_message("hass-relative-import", node=node)
return
if node.modname.startswith(
f"homeassistant.components.{current_component}."
):
self.add_message("hass-relative-import", node=node)
return
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):
for name_tuple in node.names:
for obsolete_import in obsolete_imports:

View File

@ -19,6 +19,11 @@ from . import assert_adds_messages, assert_no_messages
"homeassistant.const",
"CONSTANT",
),
(
"homeassistant.components.pylint_test.sensor",
"homeassistant.components.pylint_testing",
"CONSTANT",
),
("homeassistant.components.pylint_test.sensor", ".const", "CONSTANT"),
("homeassistant.components.pylint_test.sensor", ".", "CONSTANT"),
("homeassistant.components.pylint_test.sensor", "..", "pylint_test"),
@ -90,6 +95,12 @@ def test_good_import(
"pylint_test",
"hass-relative-import",
),
(
"homeassistant.components.pylint_test.api.hub",
"homeassistant.components.pylint_test.const",
"CONSTANT",
"hass-relative-import",
),
],
)
def test_bad_import(