Check fixtures for type hints in pylint plugin (#118313)

* Check fixtures for type hints in pylint plugin

* Apply suggestion
This commit is contained in:
epenet
2024-05-31 12:42:42 +02:00
committed by GitHub
parent 9fc51891ca
commit 7e1f4cd3fb
2 changed files with 99 additions and 7 deletions

View File

@@ -1232,6 +1232,86 @@ def test_pytest_invalid_function(
type_hint_checker.visit_asyncfunctiondef(func_node)
def test_pytest_fixture(linter: UnittestLinter, type_hint_checker: BaseChecker) -> None:
"""Ensure valid hints are accepted for a test fixture."""
func_node = astroid.extract_node(
"""
import pytest
@pytest.fixture
def sample_fixture( #@
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
aiohttp_server: Callable[[], TestServer],
unused_tcp_port_factory: Callable[[], int],
enable_custom_integrations: None,
) -> None:
pass
""",
"tests.components.pylint_test.notify",
)
type_hint_checker.visit_module(func_node.parent)
with assert_no_messages(
linter,
):
type_hint_checker.visit_asyncfunctiondef(func_node)
@pytest.mark.parametrize("decorator", ["@pytest.fixture", "@pytest.fixture()"])
def test_pytest_invalid_fixture(
linter: UnittestLinter, type_hint_checker: BaseChecker, decorator: str
) -> None:
"""Ensure invalid hints are rejected for a test fixture."""
func_node, hass_node, caplog_node, none_node = astroid.extract_node(
f"""
import pytest
{decorator}
def sample_fixture( #@
hass: Something, #@
caplog: SomethingElse, #@
current_request_with_host, #@
) -> Any:
pass
""",
"tests.components.pylint_test.notify",
)
type_hint_checker.visit_module(func_node.parent)
with assert_adds_messages(
linter,
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=hass_node,
args=("hass", ["HomeAssistant", "HomeAssistant | None"], "sample_fixture"),
line=6,
col_offset=4,
end_line=6,
end_col_offset=19,
),
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=caplog_node,
args=("caplog", "pytest.LogCaptureFixture", "sample_fixture"),
line=7,
col_offset=4,
end_line=7,
end_col_offset=25,
),
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=none_node,
args=("current_request_with_host", "None", "sample_fixture"),
line=8,
col_offset=4,
end_line=8,
end_col_offset=29,
),
):
type_hint_checker.visit_asyncfunctiondef(func_node)
@pytest.mark.parametrize(
"entry_annotation",
[