From 8f8bdac451f184dfecce80fd0f28c402faf92301 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 7 Feb 2023 13:07:21 +0100 Subject: [PATCH] Fix namespace issue in pylint plugin (#87627) --- pylint/plugins/hass_enforce_type_hints.py | 4 +- tests/pylint/test_enforce_type_hints.py | 71 +++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index d2355d0b4d2..46b3717aa2c 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -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: diff --git a/tests/pylint/test_enforce_type_hints.py b/tests/pylint/test_enforce_type_hints.py index 80029eb704c..365ccc111d0 100644 --- a/tests/pylint/test_enforce_type_hints.py +++ b/tests/pylint/test_enforce_type_hints.py @@ -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)