mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
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:
parent
01a4a83bab
commit
187d56b88b
@ -588,7 +588,18 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
|
|||||||
"Used when method return type is incorrect",
|
"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:
|
def __init__(self, linter: PyLinter | None = None) -> None:
|
||||||
super().__init__(linter)
|
super().__init__(linter)
|
||||||
@ -641,7 +652,11 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
|
|||||||
def _check_function(self, node: nodes.FunctionDef, match: TypeHintMatch) -> None:
|
def _check_function(self, node: nodes.FunctionDef, match: TypeHintMatch) -> None:
|
||||||
# Check that at least one argument is annotated.
|
# Check that at least one argument is annotated.
|
||||||
annotations = _get_all_annotations(node)
|
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
|
return
|
||||||
|
|
||||||
# Check that all arguments are correctly annotated.
|
# Check that all arguments are correctly annotated.
|
||||||
|
@ -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
|
hass_enforce_type_hints: ModuleType, type_hint_checker: BaseChecker, code: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Ensure that _is_valid_type is not run if there are no annotations."""
|
"""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()
|
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(
|
@pytest.mark.parametrize(
|
||||||
"code",
|
"code",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user