Add check for usefixtures decorator in pylint plugin (#118456)

pull/118464/head
epenet 2024-05-30 08:54:56 +02:00 committed by GitHub
parent baaf16e9b3
commit 9221eeb2f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 4 deletions

View File

@ -3113,6 +3113,12 @@ class HassTypeHintChecker(BaseChecker):
"hass-return-type",
"Used when method return type is incorrect",
),
"W7433": (
"Argument %s is of type %s and could be move to "
"`@pytest.mark.usefixtures` decorator in %s",
"hass-consider-usefixtures-decorator",
"Used when an argument type is None and could be a fixture",
),
}
options = (
(
@ -3308,6 +3314,12 @@ class HassTypeHintChecker(BaseChecker):
# Check that all positional arguments are correctly annotated.
for arg_name, expected_type in _TEST_FIXTURES.items():
arg_node, annotation = _get_named_annotation(node, arg_name)
if arg_node and expected_type == "None":
self.add_message(
"hass-consider-usefixtures-decorator",
node=arg_node,
args=(arg_name, expected_type, node.name),
)
if arg_node and not _is_valid_type(expected_type, annotation):
self.add_message(
"hass-argument-type",

View File

@ -1152,16 +1152,20 @@ def test_pytest_function(
def test_pytest_invalid_function(
linter: UnittestLinter, type_hint_checker: BaseChecker
) -> None:
"""Ensure invalid hints are rejected for async_get_service."""
func_node, hass_node, caplog_node = astroid.extract_node(
"""
"""Ensure invalid hints are rejected for a test function."""
func_node, hass_node, caplog_node, first_none_node, second_none_node = (
astroid.extract_node(
"""
async def test_sample( #@
hass: Something, #@
caplog: SomethingElse, #@
current_request_with_host, #@
enable_custom_integrations: None, #@
) -> Anything:
pass
""",
"tests.components.pylint_test.notify",
"tests.components.pylint_test.notify",
)
)
type_hint_checker.visit_module(func_node.parent)
@ -1194,6 +1198,33 @@ def test_pytest_invalid_function(
end_line=4,
end_col_offset=25,
),
pylint.testutils.MessageTest(
msg_id="hass-consider-usefixtures-decorator",
node=first_none_node,
args=("current_request_with_host", "None", "test_sample"),
line=5,
col_offset=4,
end_line=5,
end_col_offset=29,
),
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=first_none_node,
args=("current_request_with_host", "None", "test_sample"),
line=5,
col_offset=4,
end_line=5,
end_col_offset=29,
),
pylint.testutils.MessageTest(
msg_id="hass-consider-usefixtures-decorator",
node=second_none_node,
args=("enable_custom_integrations", "None", "test_sample"),
line=6,
col_offset=4,
end_line=6,
end_col_offset=36,
),
):
type_hint_checker.visit_asyncfunctiondef(func_node)