mirror of
https://github.com/home-assistant/core.git
synced 2026-11-23 14:17:19 -04:00
Adjust pylint plugin for absolute/relative imports (#77219)
* Adjust pylint plugin for absolute/relative imports * Adjust components * One more * Adjust mqtt * Adjust mqtt.DOMAIN import * Adjust internal import * Add tests for valid local component imports * Adjust relative path check * Fixes * Fixes
This commit is contained in:
@@ -32,3 +32,21 @@ def type_hint_checker_fixture(hass_enforce_type_hints, linter) -> BaseChecker:
|
||||
type_hint_checker = hass_enforce_type_hints.HassTypeHintChecker(linter)
|
||||
type_hint_checker.module = "homeassistant.components.pylint_test"
|
||||
return type_hint_checker
|
||||
|
||||
|
||||
@pytest.fixture(name="hass_imports", scope="session")
|
||||
def hass_imports_fixture() -> ModuleType:
|
||||
"""Fixture to provide a requests mocker."""
|
||||
loader = SourceFileLoader(
|
||||
"hass_imports",
|
||||
str(BASE_PATH.joinpath("pylint/plugins/hass_imports.py")),
|
||||
)
|
||||
return loader.load_module(None)
|
||||
|
||||
|
||||
@pytest.fixture(name="imports_checker")
|
||||
def imports_checker_fixture(hass_imports, linter) -> BaseChecker:
|
||||
"""Fixture to provide a requests mocker."""
|
||||
type_hint_checker = hass_imports.HassImportsFormatChecker(linter)
|
||||
type_hint_checker.module = "homeassistant.components.pylint_test"
|
||||
return type_hint_checker
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
"""Tests for pylint hass_imports plugin."""
|
||||
# pylint:disable=protected-access
|
||||
from __future__ import annotations
|
||||
|
||||
import astroid
|
||||
from pylint.checkers import BaseChecker
|
||||
import pylint.testutils
|
||||
from pylint.testutils.unittest_linter import UnittestLinter
|
||||
import pytest
|
||||
|
||||
from . import assert_adds_messages, assert_no_messages
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("module_name", "import_from", "import_what"),
|
||||
[
|
||||
(
|
||||
"homeassistant.components.pylint_test.sensor",
|
||||
"homeassistant.const",
|
||||
"CONSTANT",
|
||||
),
|
||||
("homeassistant.components.pylint_test.sensor", ".const", "CONSTANT"),
|
||||
("homeassistant.components.pylint_test.sensor", ".", "CONSTANT"),
|
||||
("homeassistant.components.pylint_test.sensor", "..", "pylint_test"),
|
||||
(
|
||||
"homeassistant.components.pylint_test.api.hub",
|
||||
"homeassistant.const",
|
||||
"CONSTANT",
|
||||
),
|
||||
("homeassistant.components.pylint_test.api.hub", "..const", "CONSTANT"),
|
||||
("homeassistant.components.pylint_test.api.hub", "..", "CONSTANT"),
|
||||
("homeassistant.components.pylint_test.api.hub", "...", "pylint_test"),
|
||||
],
|
||||
)
|
||||
def test_good_import(
|
||||
linter: UnittestLinter,
|
||||
imports_checker: BaseChecker,
|
||||
module_name: str,
|
||||
import_from: str,
|
||||
import_what: str,
|
||||
) -> None:
|
||||
"""Ensure good imports pass through ok."""
|
||||
|
||||
import_node = astroid.extract_node(
|
||||
f"from {import_from} import {import_what} #@",
|
||||
module_name,
|
||||
)
|
||||
imports_checker.visit_module(import_node.parent)
|
||||
|
||||
with assert_no_messages(linter):
|
||||
imports_checker.visit_importfrom(import_node)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("module_name", "import_from", "import_what", "error_code"),
|
||||
[
|
||||
(
|
||||
"homeassistant.components.pylint_test.sensor",
|
||||
"homeassistant.components.pylint_test.const",
|
||||
"CONSTANT",
|
||||
"hass-relative-import",
|
||||
),
|
||||
(
|
||||
"homeassistant.components.pylint_test.sensor",
|
||||
"..const",
|
||||
"CONSTANT",
|
||||
"hass-absolute-import",
|
||||
),
|
||||
(
|
||||
"homeassistant.components.pylint_test.sensor",
|
||||
"...const",
|
||||
"CONSTANT",
|
||||
"hass-absolute-import",
|
||||
),
|
||||
(
|
||||
"homeassistant.components.pylint_test.api.hub",
|
||||
"homeassistant.components.pylint_test.api.const",
|
||||
"CONSTANT",
|
||||
"hass-relative-import",
|
||||
),
|
||||
(
|
||||
"homeassistant.components.pylint_test.api.hub",
|
||||
"...const",
|
||||
"CONSTANT",
|
||||
"hass-absolute-import",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_bad_import(
|
||||
linter: UnittestLinter,
|
||||
imports_checker: BaseChecker,
|
||||
module_name: str,
|
||||
import_from: str,
|
||||
import_what: str,
|
||||
error_code: str,
|
||||
) -> None:
|
||||
"""Ensure bad imports are rejected."""
|
||||
|
||||
import_node = astroid.extract_node(
|
||||
f"from {import_from} import {import_what} #@",
|
||||
module_name,
|
||||
)
|
||||
imports_checker.visit_module(import_node.parent)
|
||||
|
||||
with assert_adds_messages(
|
||||
linter,
|
||||
pylint.testutils.MessageTest(
|
||||
msg_id=error_code,
|
||||
node=import_node,
|
||||
args=None,
|
||||
line=1,
|
||||
col_offset=0,
|
||||
end_line=1,
|
||||
end_col_offset=len(import_from) + 21,
|
||||
),
|
||||
):
|
||||
imports_checker.visit_importfrom(import_node)
|
||||
Reference in New Issue
Block a user