mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Adjust pylint plugin for relative imports (#78277)
This commit is contained in:
parent
d3be06906b
commit
f1c7fb7866
@ -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:
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user