diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 8194bb72ca5..fc7be8ba8e8 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -588,7 +588,18 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc] "Used when method return type is incorrect", ), } - options = () + options = ( + ( + "ignore-missing-annotations", + { + "default": True, + "type": "yn", + "metavar": "", + "help": "Set to ``no`` if you wish to check functions that do not " + "have any type hints.", + }, + ), + ) def __init__(self, linter: PyLinter | None = None) -> None: super().__init__(linter) @@ -641,7 +652,11 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc] def _check_function(self, node: nodes.FunctionDef, match: TypeHintMatch) -> None: # Check that at least one argument is annotated. annotations = _get_all_annotations(node) - if node.returns is None and not _has_valid_annotations(annotations): + if ( + self.linter.config.ignore_missing_annotations + and node.returns is None + and not _has_valid_annotations(annotations) + ): return # Check that all arguments are correctly annotated. diff --git a/tests/pylint/test_enforce_type_hints.py b/tests/pylint/test_enforce_type_hints.py index 86b06a894d0..c07014add7f 100644 --- a/tests/pylint/test_enforce_type_hints.py +++ b/tests/pylint/test_enforce_type_hints.py @@ -116,7 +116,7 @@ def test_regex_a_or_b( """ ], ) -def test_ignore_not_annotations( +def test_ignore_no_annotations( hass_enforce_type_hints: ModuleType, type_hint_checker: BaseChecker, code: str ) -> None: """Ensure that _is_valid_type is not run if there are no annotations.""" @@ -133,6 +133,41 @@ def test_ignore_not_annotations( is_valid_type.assert_not_called() +@pytest.mark.parametrize( + "code", + [ + """ + async def setup( #@ + arg1, arg2 + ): + pass + """ + ], +) +def test_bypass_ignore_no_annotations( + hass_enforce_type_hints: ModuleType, type_hint_checker: BaseChecker, code: str +) -> None: + """Test `ignore-missing-annotations` option. + + Ensure that `_is_valid_type` is run if there are no annotations + but `ignore-missing-annotations` option is forced to False. + """ + # Set bypass option + type_hint_checker.config.ignore_missing_annotations = False + + func_node = astroid.extract_node( + code, + "homeassistant.components.pylint_test", + ) + type_hint_checker.visit_module(func_node.parent) + + with patch.object( + hass_enforce_type_hints, "_is_valid_type", return_value=True + ) as is_valid_type: + type_hint_checker.visit_asyncfunctiondef(func_node) + is_valid_type.assert_called() + + @pytest.mark.parametrize( "code", [