Run pylint sorted platform check also when platform has type annotations (#111407)

This commit is contained in:
Jan-Philipp Benecke 2024-02-25 21:18:07 +01:00 committed by GitHub
parent e116d2a721
commit 1c5be598f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 72 additions and 22 deletions

View File

@ -12,11 +12,11 @@ from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
PLATFORMS: list[Platform] = [
Platform.SWITCH,
Platform.BINARY_SENSOR,
Platform.CLIMATE,
Platform.COVER,
Platform.LIGHT,
Platform.CLIMATE,
Platform.BINARY_SENSOR,
Platform.SWITCH,
]

View File

@ -18,7 +18,7 @@ from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
from .coordinator import EcoforestCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.NUMBER, Platform.SWITCH]
PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
_LOGGER = logging.getLogger(__name__)

View File

@ -18,7 +18,7 @@ from .coordinator import (
ElectricKiwiHOPDataCoordinator,
)
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SELECT]
PLATFORMS: list[Platform] = [Platform.SELECT, Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View File

@ -11,7 +11,7 @@ from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.typing import ConfigType
DOMAIN = "jewish_calendar"
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.BINARY_SENSOR]
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
CONF_DIASPORA = "diaspora"
CONF_LANGUAGE = "language"

View File

@ -29,7 +29,7 @@ from .const import (
SMART_METER_SCAN_INTERVAL,
)
PLATFORMS: Final = [Platform.SENSOR, Platform.BINARY_SENSOR]
PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.SENSOR]
@dataclass

View File

@ -18,7 +18,7 @@ from .config_flow import CONF_SITE, create_omada_client
from .const import DOMAIN
from .controller import OmadaSiteController
PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.UPDATE, Platform.BINARY_SENSOR]
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SWITCH, Platform.UPDATE]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View File

@ -13,9 +13,9 @@ from .coordinator import V2CUpdateCoordinator
PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.NUMBER,
Platform.SENSOR,
Platform.SWITCH,
Platform.NUMBER,
]

View File

@ -46,10 +46,10 @@ CONFIG_SCHEMA = vol.Schema(
)
PLATFORMS: list[str] = [
Platform.SENSOR,
Platform.FAN,
Platform.BINARY_SENSOR,
Platform.FAN,
Platform.NUMBER,
Platform.SENSOR,
Platform.SWITCH,
]

View File

@ -23,7 +23,7 @@ from .const import (
)
from .coordinator import WeatherKitDataUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.WEATHER, Platform.SENSOR]
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.WEATHER]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View File

@ -20,18 +20,28 @@ class HassEnforceSortedPlatformsChecker(BaseChecker):
}
options = ()
def visit_annassign(self, node: nodes.AnnAssign) -> None:
"""Check for sorted PLATFORMS const with type annotations."""
self._do_sorted_check(node.target, node)
def visit_assign(self, node: nodes.Assign) -> None:
"""Check for sorted PLATFORMS const."""
"""Check for sorted PLATFORMS const without type annotations."""
for target in node.targets:
if (
isinstance(target, nodes.AssignName)
and target.name == "PLATFORMS"
and isinstance(node.value, nodes.List)
):
platforms = [v.as_string() for v in node.value.elts]
sorted_platforms = sorted(platforms)
if platforms != sorted_platforms:
self.add_message("hass-enforce-sorted-platforms", node=node)
self._do_sorted_check(target, node)
def _do_sorted_check(
self, target: nodes.NodeNG, node: nodes.Assign | nodes.AnnAssign
) -> None:
"""Check for sorted PLATFORMS const."""
if (
isinstance(target, nodes.AssignName)
and target.name == "PLATFORMS"
and isinstance(node.value, nodes.List)
):
platforms = [v.as_string() for v in node.value.elts]
sorted_platforms = sorted(platforms)
if platforms != sorted_platforms:
self.add_message("hass-enforce-sorted-platforms", node=node)
def register(linter: PyLinter) -> None:

View File

@ -27,6 +27,18 @@ from . import assert_adds_messages, assert_no_messages
""",
id="multiple_platforms",
),
pytest.param(
"""
PLATFORMS: list[str] = [Platform.SENSOR]
""",
id="typed_on_platform",
),
pytest.param(
"""
PLATFORMS: list[str] = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR]
""",
id="typed_multiple_platform",
),
],
)
def test_enforce_sorted_platforms(
@ -69,3 +81,31 @@ def test_enforce_sorted_platforms_bad(
),
):
enforce_sorted_platforms_checker.visit_assign(assign_node)
def test_enforce_sorted_platforms_bad_typed(
linter: UnittestLinter,
enforce_sorted_platforms_checker: BaseChecker,
) -> None:
"""Bad typed test case."""
assign_node = astroid.extract_node(
"""
PLATFORMS: list[str] = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.BUTTON]
""",
"homeassistant.components.pylint_test",
)
with assert_adds_messages(
linter,
MessageTest(
msg_id="hass-enforce-sorted-platforms",
line=2,
node=assign_node,
args=None,
confidence=UNDEFINED,
col_offset=0,
end_line=2,
end_col_offset=81,
),
):
enforce_sorted_platforms_checker.visit_annassign(assign_node)