diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 2077b865377..6d3b68cbeb6 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -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", diff --git a/tests/pylint/test_enforce_type_hints.py b/tests/pylint/test_enforce_type_hints.py index ad3b7d62be9..64dd472827e 100644 --- a/tests/pylint/test_enforce_type_hints.py +++ b/tests/pylint/test_enforce_type_hints.py @@ -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)