mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Adjust pylint plugin for return type inheritance (#90046)
This commit is contained in:
parent
bec7bbeb92
commit
6e92dac61f
@ -42,7 +42,6 @@ class TypeHintMatch:
|
|||||||
"""named_arg_types is for named or keyword arguments"""
|
"""named_arg_types is for named or keyword arguments"""
|
||||||
kwargs_type: str | None = None
|
kwargs_type: str | None = None
|
||||||
"""kwargs_type is for the special case `**kwargs`"""
|
"""kwargs_type is for the special case `**kwargs`"""
|
||||||
check_return_type_inheritance: bool = False
|
|
||||||
has_async_counterpart: bool = False
|
has_async_counterpart: bool = False
|
||||||
|
|
||||||
def need_to_check_function(self, node: nodes.FunctionDef) -> bool:
|
def need_to_check_function(self, node: nodes.FunctionDef) -> bool:
|
||||||
@ -398,7 +397,6 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = {
|
|||||||
1: "ConfigType",
|
1: "ConfigType",
|
||||||
},
|
},
|
||||||
return_type=["DeviceScanner", None],
|
return_type=["DeviceScanner", None],
|
||||||
check_return_type_inheritance=True,
|
|
||||||
has_async_counterpart=True,
|
has_async_counterpart=True,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -466,7 +464,6 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = {
|
|||||||
2: "DiscoveryInfoType | None",
|
2: "DiscoveryInfoType | None",
|
||||||
},
|
},
|
||||||
return_type=["BaseNotificationService", None],
|
return_type=["BaseNotificationService", None],
|
||||||
check_return_type_inheritance=True,
|
|
||||||
has_async_counterpart=True,
|
has_async_counterpart=True,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -493,7 +490,6 @@ _CLASS_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
|||||||
0: "ConfigEntry",
|
0: "ConfigEntry",
|
||||||
},
|
},
|
||||||
return_type="OptionsFlow",
|
return_type="OptionsFlow",
|
||||||
check_return_type_inheritance=True,
|
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="async_step_dhcp",
|
function_name="async_step_dhcp",
|
||||||
@ -681,7 +677,6 @@ _RESTORE_ENTITY_MATCH: list[TypeHintMatch] = [
|
|||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="extra_restore_state_data",
|
function_name="extra_restore_state_data",
|
||||||
return_type=["ExtraStoredData", None],
|
return_type=["ExtraStoredData", None],
|
||||||
check_return_type_inheritance=True,
|
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
_TOGGLE_ENTITY_MATCH: list[TypeHintMatch] = [
|
_TOGGLE_ENTITY_MATCH: list[TypeHintMatch] = [
|
||||||
@ -2842,15 +2837,13 @@ def _is_valid_return_type(match: TypeHintMatch, node: nodes.NodeNG) -> bool:
|
|||||||
match, node.right
|
match, node.right
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if isinstance(match.return_type, (str, list)) and isinstance(node, nodes.Name):
|
||||||
match.check_return_type_inheritance
|
|
||||||
and isinstance(match.return_type, (str, list))
|
|
||||||
and isinstance(node, nodes.Name)
|
|
||||||
):
|
|
||||||
if isinstance(match.return_type, str):
|
if isinstance(match.return_type, str):
|
||||||
valid_types = {match.return_type}
|
valid_types = {match.return_type}
|
||||||
else:
|
else:
|
||||||
valid_types = {el for el in match.return_type if isinstance(el, str)}
|
valid_types = {el for el in match.return_type if isinstance(el, str)}
|
||||||
|
if "Mapping[str, Any]" in valid_types:
|
||||||
|
valid_types.add("TypedDict")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for infer_node in node.infer():
|
for infer_node in node.infer():
|
||||||
|
@ -724,6 +724,7 @@ def test_invalid_mapping_return_type(
|
|||||||
"-> Mapping[str, bool | int]",
|
"-> Mapping[str, bool | int]",
|
||||||
"-> dict[str, Any]",
|
"-> dict[str, Any]",
|
||||||
"-> dict[str, str]",
|
"-> dict[str, str]",
|
||||||
|
"-> CustomTypedDict",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_valid_mapping_return_type(
|
def test_valid_mapping_return_type(
|
||||||
@ -737,6 +738,11 @@ def test_valid_mapping_return_type(
|
|||||||
|
|
||||||
class_node = astroid.extract_node(
|
class_node = astroid.extract_node(
|
||||||
f"""
|
f"""
|
||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
|
class CustomTypedDict(TypedDict):
|
||||||
|
pass
|
||||||
|
|
||||||
class Entity():
|
class Entity():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user