From b3f997156a7a52f84eb8743e6ab048485a19bf39 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 3 Jan 2024 09:06:26 +0100 Subject: [PATCH] Enable strict typing for counter (#106906) --- .strict-typing | 1 + homeassistant/components/counter/__init__.py | 14 ++++++++------ mypy.ini | 10 ++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.strict-typing b/.strict-typing index 51164a552ab8..e46f439e4ca9 100644 --- a/.strict-typing +++ b/.strict-typing @@ -121,6 +121,7 @@ homeassistant.components.cloud.* homeassistant.components.co2signal.* homeassistant.components.command_line.* homeassistant.components.configurator.* +homeassistant.components.counter.* homeassistant.components.cover.* homeassistant.components.cpuspeed.* homeassistant.components.crownstone.* diff --git a/homeassistant/components/counter/__init__.py b/homeassistant/components/counter/__init__.py index 42676498c9f3..7d69025fb97f 100644 --- a/homeassistant/components/counter/__init__.py +++ b/homeassistant/components/counter/__init__.py @@ -2,7 +2,7 @@ from __future__ import annotations import logging -from typing import Self +from typing import Any, Self, TypeVar import voluptuous as vol @@ -22,6 +22,8 @@ from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType +_T = TypeVar("_T") + _LOGGER = logging.getLogger(__name__) ATTR_INITIAL = "initial" @@ -59,7 +61,7 @@ STORAGE_FIELDS = { } -def _none_to_empty_dict(value): +def _none_to_empty_dict(value: _T | None) -> _T | dict[str, Any]: if value is None: return {} return value @@ -140,12 +142,12 @@ class CounterStorageCollection(collection.DictStorageCollection): async def _process_create_data(self, data: dict) -> dict: """Validate the config is valid.""" - return self.CREATE_UPDATE_SCHEMA(data) + return self.CREATE_UPDATE_SCHEMA(data) # type: ignore[no-any-return] @callback def _get_suggested_id(self, info: dict) -> str: """Suggest an ID based on the config.""" - return info[CONF_NAME] + return info[CONF_NAME] # type: ignore[no-any-return] async def _update_data(self, item: dict, update_data: dict) -> dict: """Return a new updated data object.""" @@ -211,9 +213,9 @@ class Counter(collection.CollectionEntity, RestoreEntity): @property def unique_id(self) -> str | None: """Return unique id of the entity.""" - return self._config[CONF_ID] + return self._config[CONF_ID] # type: ignore[no-any-return] - def compute_next_state(self, state) -> int: + def compute_next_state(self, state: int | None) -> int | None: """Keep the state within the range of min/max values.""" if self._config[CONF_MINIMUM] is not None: state = max(self._config[CONF_MINIMUM], state) diff --git a/mypy.ini b/mypy.ini index 00ea7662ed69..53f5b0715ce4 100644 --- a/mypy.ini +++ b/mypy.ini @@ -970,6 +970,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.counter.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.cover.*] check_untyped_defs = true disallow_incomplete_defs = true