Add ability to check kwargs type annotation

This commit is contained in:
epenet
2022-06-15 07:29:43 +00:00
parent 1b8dd3368a
commit a2e68f8424

View File

@@ -22,6 +22,7 @@ class TypeHintMatch:
function_name: str function_name: str
return_type: list[str] | str | None | object return_type: list[str] | str | None | object
arg_types: dict[int, str] | None = None arg_types: dict[int, str] | None = None
kwargs_type: str | None = None
check_return_type_inheritance: bool = False check_return_type_inheritance: bool = False
@@ -613,7 +614,7 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
priority = -1 priority = -1
msgs = { msgs = {
"W7431": ( "W7431": (
"Argument %d should be of type %s", "Argument %s should be of type %s",
"hass-argument-type", "hass-argument-type",
"Used when method argument type is incorrect", "Used when method argument type is incorrect",
), ),
@@ -709,6 +710,16 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
args=(key + 1, expected_type), args=(key + 1, expected_type),
) )
# Check that kwargs is correctly annotated.
if match.kwargs_type and not _is_valid_type(
match.kwargs_type, node.args.kwargannotation
):
self.add_message(
"hass-argument-type",
node=node,
args=(node.args.kwarg, match.kwargs_type),
)
# Check the return type. # Check the return type.
if not _is_valid_return_type(match, node.returns): if not _is_valid_return_type(match, node.returns):
self.add_message( self.add_message(