Add flexibility to HassEnforceClassModule (#125739)

* Add flexibility to HassEnforceClassModule

* Adjust
This commit is contained in:
epenet 2024-09-11 15:11:03 +02:00 committed by GitHub
parent 1d3f431628
commit c33ba541b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 5 deletions

View File

@ -68,11 +68,14 @@ class HassEnforceClassModule(BaseChecker):
# we only want to check components # we only want to check components
if not root_name.startswith("homeassistant.components."): if not root_name.startswith("homeassistant.components."):
return return
parts = root_name.split(".")
current_module = parts[3] if len(parts) > 3 else ""
ancestors: list[ClassDef] | None = None ancestors: list[ClassDef] | None = None
for match in _MODULES: for match in _MODULES:
if root_name.endswith(f".{match.expected_module}"): # Allow module.py and module/sub_module.py
if current_module == match.expected_module:
continue continue
if ancestors is None: if ancestors is None:

View File

@ -41,11 +41,21 @@ from . import assert_adds_messages, assert_no_messages
), ),
], ],
) )
@pytest.mark.parametrize(
"path",
[
"homeassistant.components.pylint_test.coordinator",
"homeassistant.components.pylint_test.coordinator.my_coordinator",
],
)
def test_enforce_class_module_good( def test_enforce_class_module_good(
linter: UnittestLinter, enforce_class_module_checker: BaseChecker, code: str linter: UnittestLinter,
enforce_class_module_checker: BaseChecker,
code: str,
path: str,
) -> None: ) -> None:
"""Good test cases.""" """Good test cases."""
root_node = astroid.parse(code, "homeassistant.components.pylint_test.coordinator") root_node = astroid.parse(code, path)
walker = ASTWalker(linter) walker = ASTWalker(linter)
walker.add_checker(enforce_class_module_checker) walker.add_checker(enforce_class_module_checker)
@ -53,9 +63,19 @@ def test_enforce_class_module_good(
walker.walk(root_node) walker.walk(root_node)
@pytest.mark.parametrize(
"path",
[
"homeassistant.components.pylint_test",
"homeassistant.components.pylint_test.my_coordinator",
"homeassistant.components.pylint_test.coordinator_other",
"homeassistant.components.pylint_test.sensor",
],
)
def test_enforce_class_module_bad_simple( def test_enforce_class_module_bad_simple(
linter: UnittestLinter, linter: UnittestLinter,
enforce_class_module_checker: BaseChecker, enforce_class_module_checker: BaseChecker,
path: str,
) -> None: ) -> None:
"""Bad test case with coordinator extending directly.""" """Bad test case with coordinator extending directly."""
root_node = astroid.parse( root_node = astroid.parse(
@ -66,7 +86,7 @@ def test_enforce_class_module_bad_simple(
class TestCoordinator(DataUpdateCoordinator): class TestCoordinator(DataUpdateCoordinator):
pass pass
""", """,
"homeassistant.components.pylint_test", path,
) )
walker = ASTWalker(linter) walker = ASTWalker(linter)
walker.add_checker(enforce_class_module_checker) walker.add_checker(enforce_class_module_checker)
@ -87,9 +107,19 @@ def test_enforce_class_module_bad_simple(
walker.walk(root_node) walker.walk(root_node)
@pytest.mark.parametrize(
"path",
[
"homeassistant.components.pylint_test",
"homeassistant.components.pylint_test.my_coordinator",
"homeassistant.components.pylint_test.coordinator_other",
"homeassistant.components.pylint_test.sensor",
],
)
def test_enforce_class_module_bad_nested( def test_enforce_class_module_bad_nested(
linter: UnittestLinter, linter: UnittestLinter,
enforce_class_module_checker: BaseChecker, enforce_class_module_checker: BaseChecker,
path: str,
) -> None: ) -> None:
"""Bad test case with nested coordinators.""" """Bad test case with nested coordinators."""
root_node = astroid.parse( root_node = astroid.parse(
@ -103,7 +133,7 @@ def test_enforce_class_module_bad_nested(
class NopeCoordinator(TestCoordinator): class NopeCoordinator(TestCoordinator):
pass pass
""", """,
"homeassistant.components.pylint_test", path,
) )
walker = ASTWalker(linter) walker = ASTWalker(linter)
walker.add_checker(enforce_class_module_checker) walker.add_checker(enforce_class_module_checker)