mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 04:05:20 +01:00
Check fixtures for type hints in pylint plugin (#118313)
* Check fixtures for type hints in pylint plugin * Apply suggestion
This commit is contained in:
@@ -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",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user