Replace object with enum for pylint sentinel (#76030)
* Replace object with enum for pylint sentinel * Use standard enumpull/76047/head
parent
deff0ad61e
commit
27ed3d324f
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from enum import Enum
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from astroid import nodes
|
from astroid import nodes
|
||||||
|
@ -10,8 +11,13 @@ from pylint.lint import PyLinter
|
||||||
|
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
|
|
||||||
DEVICE_CLASS = object()
|
|
||||||
UNDEFINED = object()
|
class _Special(Enum):
|
||||||
|
"""Sentinel values"""
|
||||||
|
|
||||||
|
UNDEFINED = 1
|
||||||
|
DEVICE_CLASS = 2
|
||||||
|
|
||||||
|
|
||||||
_PLATFORMS: set[str] = {platform.value for platform in Platform}
|
_PLATFORMS: set[str] = {platform.value for platform in Platform}
|
||||||
|
|
||||||
|
@ -21,7 +27,7 @@ class TypeHintMatch:
|
||||||
"""Class for pattern matching."""
|
"""Class for pattern matching."""
|
||||||
|
|
||||||
function_name: str
|
function_name: str
|
||||||
return_type: list[str] | str | None | object
|
return_type: list[str | _Special | None] | str | _Special | None
|
||||||
arg_types: dict[int, str] | None = None
|
arg_types: dict[int, str] | None = None
|
||||||
"""arg_types is for positional arguments"""
|
"""arg_types is for positional arguments"""
|
||||||
named_arg_types: dict[str, str] | None = None
|
named_arg_types: dict[str, str] | None = None
|
||||||
|
@ -361,7 +367,7 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = {
|
||||||
0: "HomeAssistant",
|
0: "HomeAssistant",
|
||||||
1: "ConfigEntry",
|
1: "ConfigEntry",
|
||||||
},
|
},
|
||||||
return_type=UNDEFINED,
|
return_type=_Special.UNDEFINED,
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="async_get_device_diagnostics",
|
function_name="async_get_device_diagnostics",
|
||||||
|
@ -370,7 +376,7 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = {
|
||||||
1: "ConfigEntry",
|
1: "ConfigEntry",
|
||||||
2: "DeviceEntry",
|
2: "DeviceEntry",
|
||||||
},
|
},
|
||||||
return_type=UNDEFINED,
|
return_type=_Special.UNDEFINED,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
@ -499,7 +505,7 @@ _ENTITY_MATCH: list[TypeHintMatch] = [
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="device_class",
|
function_name="device_class",
|
||||||
return_type=[DEVICE_CLASS, "str", None],
|
return_type=[_Special.DEVICE_CLASS, "str", None],
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="unit_of_measurement",
|
function_name="unit_of_measurement",
|
||||||
|
@ -1407,11 +1413,11 @@ def _is_valid_type(
|
||||||
in_return: bool = False,
|
in_return: bool = False,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Check the argument node against the expected type."""
|
"""Check the argument node against the expected type."""
|
||||||
if expected_type is UNDEFINED:
|
if expected_type is _Special.UNDEFINED:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Special case for device_class
|
# Special case for device_class
|
||||||
if expected_type == DEVICE_CLASS and in_return:
|
if expected_type is _Special.DEVICE_CLASS and in_return:
|
||||||
return (
|
return (
|
||||||
isinstance(node, nodes.Name)
|
isinstance(node, nodes.Name)
|
||||||
and node.name.endswith("DeviceClass")
|
and node.name.endswith("DeviceClass")
|
||||||
|
|
Loading…
Reference in New Issue