Fix namespace issue in pylint plugin (#87627)

This commit is contained in:
epenet 2023-02-07 13:07:21 +01:00 committed by GitHub
parent be564e0162
commit 8f8bdac451
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 1 deletions

View File

@ -2779,7 +2779,9 @@ def _is_valid_type(
return True
# Attribute occurs when a namespace is used, eg. "core.HomeAssistant"
return isinstance(node, nodes.Attribute) and node.attrname == expected_type
return isinstance(node, nodes.Attribute) and (
node.attrname == expected_type or node.as_string() == expected_type
)
def _is_valid_return_type(match: TypeHintMatch, node: nodes.NodeNG) -> bool:

View File

@ -1040,3 +1040,74 @@ def test_notify_get_service(
linter,
):
type_hint_checker.visit_asyncfunctiondef(func_node)
def test_pytest_function(
linter: UnittestLinter, type_hint_checker: BaseChecker
) -> None:
"""Ensure valid hints are accepted for async_get_service."""
func_node = astroid.extract_node(
"""
async def test_sample( #@
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
pass
""",
"tests.components.pylint_test.notify",
)
type_hint_checker.visit_module(func_node.parent)
with assert_no_messages(
linter,
):
type_hint_checker.visit_asyncfunctiondef(func_node)
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(
"""
async def test_sample( #@
hass: Something, #@
caplog: SomethingElse, #@
) -> Anything:
pass
""",
"tests.components.pylint_test.notify",
)
type_hint_checker.visit_module(func_node.parent)
with assert_adds_messages(
linter,
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=hass_node,
args=("hass", ["HomeAssistant", "HomeAssistant | None"], "test_sample"),
line=3,
col_offset=4,
end_line=3,
end_col_offset=19,
),
pylint.testutils.MessageTest(
msg_id="hass-return-type",
node=func_node,
args=("None", "test_sample"),
line=2,
col_offset=0,
end_line=2,
end_col_offset=21,
),
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=caplog_node,
args=("caplog", "pytest.LogCaptureFixture", "test_sample"),
line=4,
col_offset=4,
end_line=4,
end_col_offset=25,
),
):
type_hint_checker.visit_asyncfunctiondef(func_node)