mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add flexibility to HassEnforceClassModule (#125739)
* Add flexibility to HassEnforceClassModule * Adjust
This commit is contained in:
parent
1d3f431628
commit
c33ba541b0
@ -68,11 +68,14 @@ class HassEnforceClassModule(BaseChecker):
|
||||
# we only want to check components
|
||||
if not root_name.startswith("homeassistant.components."):
|
||||
return
|
||||
parts = root_name.split(".")
|
||||
current_module = parts[3] if len(parts) > 3 else ""
|
||||
|
||||
ancestors: list[ClassDef] | None = None
|
||||
|
||||
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
|
||||
|
||||
if ancestors is None:
|
||||
|
@ -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(
|
||||
linter: UnittestLinter, enforce_class_module_checker: BaseChecker, code: str
|
||||
linter: UnittestLinter,
|
||||
enforce_class_module_checker: BaseChecker,
|
||||
code: str,
|
||||
path: str,
|
||||
) -> None:
|
||||
"""Good test cases."""
|
||||
root_node = astroid.parse(code, "homeassistant.components.pylint_test.coordinator")
|
||||
root_node = astroid.parse(code, path)
|
||||
walker = ASTWalker(linter)
|
||||
walker.add_checker(enforce_class_module_checker)
|
||||
|
||||
@ -53,9 +63,19 @@ def test_enforce_class_module_good(
|
||||
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(
|
||||
linter: UnittestLinter,
|
||||
enforce_class_module_checker: BaseChecker,
|
||||
path: str,
|
||||
) -> None:
|
||||
"""Bad test case with coordinator extending directly."""
|
||||
root_node = astroid.parse(
|
||||
@ -66,7 +86,7 @@ def test_enforce_class_module_bad_simple(
|
||||
class TestCoordinator(DataUpdateCoordinator):
|
||||
pass
|
||||
""",
|
||||
"homeassistant.components.pylint_test",
|
||||
path,
|
||||
)
|
||||
walker = ASTWalker(linter)
|
||||
walker.add_checker(enforce_class_module_checker)
|
||||
@ -87,9 +107,19 @@ def test_enforce_class_module_bad_simple(
|
||||
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(
|
||||
linter: UnittestLinter,
|
||||
enforce_class_module_checker: BaseChecker,
|
||||
path: str,
|
||||
) -> None:
|
||||
"""Bad test case with nested coordinators."""
|
||||
root_node = astroid.parse(
|
||||
@ -103,7 +133,7 @@ def test_enforce_class_module_bad_nested(
|
||||
class NopeCoordinator(TestCoordinator):
|
||||
pass
|
||||
""",
|
||||
"homeassistant.components.pylint_test",
|
||||
path,
|
||||
)
|
||||
walker = ASTWalker(linter)
|
||||
walker.add_checker(enforce_class_module_checker)
|
||||
|
Loading…
x
Reference in New Issue
Block a user