mirror of
https://github.com/home-assistant/core.git
synced 2025-08-10 16:15:08 +02:00
Use blacklist
This commit is contained in:
@@ -396,11 +396,11 @@ _OBSOLETE_IMPORT: dict[str, list[ObsoleteImportMatch]] = {
|
|||||||
|
|
||||||
# Should be gradually synchronised with pyproject.toml
|
# Should be gradually synchronised with pyproject.toml
|
||||||
# [tool.ruff.lint.flake8-import-conventions.extend-aliases]
|
# [tool.ruff.lint.flake8-import-conventions.extend-aliases]
|
||||||
_NAMESPACE_IMPORT: dict[str, str] = {
|
_FORCE_NAMESPACE_IMPORT: dict[tuple[str, str], str] = {
|
||||||
"homeassistant.helpers.area_registry": "ar",
|
("homeassistant.helpers.area_registry", "async_get"): "ar.async_get",
|
||||||
"homeassistant.helpers.device_registry": "dr",
|
("homeassistant.helpers.device_registry", "async_get"): "dr.async_get",
|
||||||
"homeassistant.helpers.entity_registry": "er",
|
("homeassistant.helpers.entity_registry", "async_get"): "er.async_get",
|
||||||
"homeassistant.helpers.issue_registry": "ir",
|
("homeassistant.helpers.issue_registry", "async_get"): "ir.async_get",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -539,21 +539,16 @@ class HassImportsFormatChecker(BaseChecker):
|
|||||||
args=(import_match.string, obsolete_import.reason),
|
args=(import_match.string, obsolete_import.reason),
|
||||||
)
|
)
|
||||||
for name in node.names:
|
for name in node.names:
|
||||||
if self._has_invalid_namespace_import(node, node.modname, name[0], name[1]):
|
if self._has_invalid_namespace_import(node, node.modname, name[0]):
|
||||||
return
|
return
|
||||||
|
|
||||||
def _has_invalid_namespace_import(
|
def _has_invalid_namespace_import(
|
||||||
self, node: nodes.ImportFrom, module: str, name: str, alias: str | None
|
self, node: nodes.ImportFrom, module: str, name: str
|
||||||
) -> bool:
|
) -> bool:
|
||||||
# Rule only applies to function imports
|
for key, value in _FORCE_NAMESPACE_IMPORT.items():
|
||||||
if not name[0].islower():
|
if module == key[0] and name == key[1]:
|
||||||
return False
|
|
||||||
for helper, shorthand in _NAMESPACE_IMPORT.items():
|
|
||||||
if module.startswith(helper) and alias != shorthand:
|
|
||||||
self.add_message(
|
self.add_message(
|
||||||
"hass-helper-namespace-import",
|
"hass-helper-namespace-import", node=node, args=(name, value)
|
||||||
node=node,
|
|
||||||
args=(name, f"{shorthand}.{name}"),
|
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
@@ -254,47 +254,18 @@ def test_bad_root_import(
|
|||||||
imports_checker.visit_importfrom(node)
|
imports_checker.visit_importfrom(node)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
("import_node", "module_name"),
|
|
||||||
[
|
|
||||||
(
|
|
||||||
"from homeassistant.helpers.issue_registry import AClass",
|
|
||||||
"tests.components.pylint_test.climate",
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"from homeassistant.helpers.issue_registry import A_CONSTANT",
|
|
||||||
"tests.components.pylint_test.climate",
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_good_namespace_import(
|
|
||||||
linter: UnittestLinter,
|
|
||||||
imports_checker: BaseChecker,
|
|
||||||
import_node: str,
|
|
||||||
module_name: str,
|
|
||||||
) -> None:
|
|
||||||
"""Ensure good namespace imports are accepted."""
|
|
||||||
|
|
||||||
node = astroid.extract_node(
|
|
||||||
f"{import_node} #@",
|
|
||||||
module_name,
|
|
||||||
)
|
|
||||||
imports_checker.visit_module(node.parent)
|
|
||||||
|
|
||||||
with assert_no_messages(linter):
|
|
||||||
if import_node.startswith("import"):
|
|
||||||
imports_checker.visit_import(node)
|
|
||||||
if import_node.startswith("from"):
|
|
||||||
imports_checker.visit_importfrom(node)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("import_node", "module_name", "expected_args"),
|
("import_node", "module_name", "expected_args"),
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
"from homeassistant.helpers.issue_registry import a_function",
|
"from homeassistant.helpers.issue_registry import async_get",
|
||||||
"tests.components.pylint_test.climate",
|
"tests.components.pylint_test.climate",
|
||||||
("a_function", "ir.a_function"),
|
("async_get", "ir.async_get"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"from homeassistant.helpers.issue_registry import async_get as async_get_issue_registry",
|
||||||
|
"tests.components.pylint_test.climate",
|
||||||
|
("async_get", "ir.async_get"),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -325,7 +296,4 @@ def test_bad_namespace_import(
|
|||||||
end_col_offset=len(import_node),
|
end_col_offset=len(import_node),
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
if import_node.startswith("import"):
|
imports_checker.visit_importfrom(node)
|
||||||
imports_checker.visit_import(node)
|
|
||||||
if import_node.startswith("from"):
|
|
||||||
imports_checker.visit_importfrom(node)
|
|
||||||
|
Reference in New Issue
Block a user