Adjust "hass" type hint for test fixtures in pylint plugin (#118548)

Adjust "hass" type hint in pylint plugin
This commit is contained in:
epenet 2024-05-31 13:06:49 +02:00 committed by GitHub
parent 7e1f4cd3fb
commit 0974ea9a5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 28 deletions

View file

@ -113,6 +113,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"entity_registry_enabled_by_default": "None", "entity_registry_enabled_by_default": "None",
"event_loop": "AbstractEventLoop", "event_loop": "AbstractEventLoop",
"freezer": "FrozenDateTimeFactory", "freezer": "FrozenDateTimeFactory",
"hass": "HomeAssistant",
"hass_access_token": "str", "hass_access_token": "str",
"hass_admin_credential": "Credentials", "hass_admin_credential": "Credentials",
"hass_admin_user": "MockUser", "hass_admin_user": "MockUser",
@ -3218,16 +3219,6 @@ class HassTypeHintChecker(BaseChecker):
if self._ignore_function(node, annotations): if self._ignore_function(node, annotations):
return return
# Check that common arguments are correctly typed.
for arg_name, expected_type in _COMMON_ARGUMENTS.items():
arg_node, annotation = _get_named_annotation(node, arg_name)
if arg_node and not _is_valid_type(expected_type, annotation):
self.add_message(
"hass-argument-type",
node=arg_node,
args=(arg_name, expected_type, node.name),
)
# Check method or function matchers. # Check method or function matchers.
if node.is_method(): if node.is_method():
matchers = _METHOD_MATCH matchers = _METHOD_MATCH
@ -3246,6 +3237,16 @@ class HassTypeHintChecker(BaseChecker):
return return
matchers = self._function_matchers matchers = self._function_matchers
# Check that common arguments are correctly typed.
for arg_name, expected_type in _COMMON_ARGUMENTS.items():
arg_node, annotation = _get_named_annotation(node, arg_name)
if arg_node and not _is_valid_type(expected_type, annotation):
self.add_message(
"hass-argument-type",
node=arg_node,
args=(arg_name, expected_type, node.name),
)
for match in matchers: for match in matchers:
if not match.need_to_check_function(node): if not match.need_to_check_function(node):
continue continue

View file

@ -1174,15 +1174,6 @@ def test_pytest_invalid_function(
with assert_adds_messages( with assert_adds_messages(
linter, 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( pylint.testutils.MessageTest(
msg_id="hass-return-type", msg_id="hass-return-type",
node=func_node, node=func_node,
@ -1228,6 +1219,15 @@ def test_pytest_invalid_function(
end_line=6, end_line=6,
end_col_offset=36, end_col_offset=36,
), ),
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=hass_node,
args=("hass", "HomeAssistant", "test_sample"),
line=3,
col_offset=4,
end_line=3,
end_col_offset=19,
),
): ):
type_hint_checker.visit_asyncfunctiondef(func_node) type_hint_checker.visit_asyncfunctiondef(func_node)
@ -1281,15 +1281,6 @@ def test_pytest_invalid_fixture(
with assert_adds_messages( with assert_adds_messages(
linter, linter,
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=hass_node,
args=("hass", ["HomeAssistant", "HomeAssistant | None"], "sample_fixture"),
line=6,
col_offset=4,
end_line=6,
end_col_offset=19,
),
pylint.testutils.MessageTest( pylint.testutils.MessageTest(
msg_id="hass-argument-type", msg_id="hass-argument-type",
node=caplog_node, node=caplog_node,
@ -1308,6 +1299,15 @@ def test_pytest_invalid_fixture(
end_line=8, end_line=8,
end_col_offset=29, end_col_offset=29,
), ),
pylint.testutils.MessageTest(
msg_id="hass-argument-type",
node=hass_node,
args=("hass", "HomeAssistant", "sample_fixture"),
line=6,
col_offset=4,
end_line=6,
end_col_offset=19,
),
): ):
type_hint_checker.visit_asyncfunctiondef(func_node) type_hint_checker.visit_asyncfunctiondef(func_node)