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
|
# 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:
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user