Add check for usefixtures decorator in pylint plugin (#118456)
This commit is contained in:
parent
baaf16e9b3
commit
9221eeb2f7
2 changed files with 47 additions and 4 deletions
|
@ -3113,6 +3113,12 @@ class HassTypeHintChecker(BaseChecker):
|
||||||
"hass-return-type",
|
"hass-return-type",
|
||||||
"Used when method return type is incorrect",
|
"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 = (
|
options = (
|
||||||
(
|
(
|
||||||
|
@ -3308,6 +3314,12 @@ class HassTypeHintChecker(BaseChecker):
|
||||||
# Check that all positional arguments are correctly annotated.
|
# Check that all positional arguments are correctly annotated.
|
||||||
for arg_name, expected_type in _TEST_FIXTURES.items():
|
for arg_name, expected_type in _TEST_FIXTURES.items():
|
||||||
arg_node, annotation = _get_named_annotation(node, arg_name)
|
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):
|
if arg_node and not _is_valid_type(expected_type, annotation):
|
||||||
self.add_message(
|
self.add_message(
|
||||||
"hass-argument-type",
|
"hass-argument-type",
|
||||||
|
|
|
@ -1152,16 +1152,20 @@ def test_pytest_function(
|
||||||
def test_pytest_invalid_function(
|
def test_pytest_invalid_function(
|
||||||
linter: UnittestLinter, type_hint_checker: BaseChecker
|
linter: UnittestLinter, type_hint_checker: BaseChecker
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Ensure invalid hints are rejected for async_get_service."""
|
"""Ensure invalid hints are rejected for a test function."""
|
||||||
func_node, hass_node, caplog_node = astroid.extract_node(
|
func_node, hass_node, caplog_node, first_none_node, second_none_node = (
|
||||||
"""
|
astroid.extract_node(
|
||||||
|
"""
|
||||||
async def test_sample( #@
|
async def test_sample( #@
|
||||||
hass: Something, #@
|
hass: Something, #@
|
||||||
caplog: SomethingElse, #@
|
caplog: SomethingElse, #@
|
||||||
|
current_request_with_host, #@
|
||||||
|
enable_custom_integrations: None, #@
|
||||||
) -> Anything:
|
) -> Anything:
|
||||||
pass
|
pass
|
||||||
""",
|
""",
|
||||||
"tests.components.pylint_test.notify",
|
"tests.components.pylint_test.notify",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
type_hint_checker.visit_module(func_node.parent)
|
type_hint_checker.visit_module(func_node.parent)
|
||||||
|
|
||||||
|
@ -1194,6 +1198,33 @@ def test_pytest_invalid_function(
|
||||||
end_line=4,
|
end_line=4,
|
||||||
end_col_offset=25,
|
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)
|
type_hint_checker.visit_asyncfunctiondef(func_node)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue