mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 09:17:53 +00:00
Fix namespace issue in pylint plugin (#87627)
This commit is contained in:
parent
be564e0162
commit
8f8bdac451
@ -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:
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user