Add ability to run plugin on unannotated functions (#73520)

* Add ability to run plugin on unannotated functions

* Use options

* Adjust help text

* Add test for the option
This commit is contained in:
epenet 2022-06-16 20:12:30 +02:00 committed by GitHub
parent 01a4a83bab
commit 187d56b88b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View File

@ -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": "<y or n>",
"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.

View File

@ -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",
[