Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix namespace issue in pylint plugin #87627

Merged
merged 1 commit into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion pylint/plugins/hass_enforce_type_hints.py
Expand Up @@ -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:
Expand Down
71 changes: 71 additions & 0 deletions tests/pylint/test_enforce_type_hints.py
Expand Up @@ -1040,3 +1040,74 @@ class CustomNotificationService(BaseNotificationService):
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)