From ca1aaacc90592a3554b9b0cc97a089513a48b334 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:21:11 +0100 Subject: [PATCH] Enable strict typing for system_log (#107914) --- .strict-typing | 1 + .../components/system_log/__init__.py | 22 ++++++++++++------- mypy.ini | 10 +++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.strict-typing b/.strict-typing index 39e03820582d6..f7a02feec67ff 100644 --- a/.strict-typing +++ b/.strict-typing @@ -388,6 +388,7 @@ homeassistant.components.switchbot_cloud.* homeassistant.components.switcher_kis.* homeassistant.components.synology_dsm.* homeassistant.components.system_health.* +homeassistant.components.system_log.* homeassistant.components.systemmonitor.* homeassistant.components.tag.* homeassistant.components.tailscale.* diff --git a/homeassistant/components/system_log/__init__.py b/homeassistant/components/system_log/__init__.py index fab2b7ee2914a..3ede14a2ad61a 100644 --- a/homeassistant/components/system_log/__init__.py +++ b/homeassistant/components/system_log/__init__.py @@ -13,10 +13,12 @@ from homeassistant import __path__ as HOMEASSISTANT_PATH from homeassistant.components import websocket_api from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE -from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.core import Event, HomeAssistant, ServiceCall, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType +KeyType = tuple[str, tuple[str, int], str | None] + CONF_MAX_ENTRIES = "max_entries" CONF_FIRE_EVENT = "fire_event" CONF_MESSAGE = "message" @@ -60,7 +62,7 @@ def _figure_out_source( - record: logging.LogRecord, paths_re: re.Pattern + record: logging.LogRecord, paths_re: re.Pattern[str] ) -> tuple[str, int]: """Figure out where a log message came from.""" # If a stack trace exists, extract file names from the entire call stack. @@ -184,7 +186,7 @@ def __init__(self, record: logging.LogRecord, source: tuple[str, int]) -> None: self.count = 1 self.key = (self.name, source, self.root_cause) - def to_dict(self): + def to_dict(self) -> dict[str, Any]: """Convert object into dict to maintain backward compatibility.""" return { "name": self.name, @@ -198,10 +200,10 @@ def to_dict(self): } -class DedupStore(OrderedDict): +class DedupStore(OrderedDict[KeyType, LogEntry]): """Data store to hold max amount of deduped entries.""" - def __init__(self, maxlen=50): + def __init__(self, maxlen: int = 50) -> None: """Initialize a new DedupStore.""" super().__init__() self.maxlen = maxlen @@ -227,7 +229,7 @@ def add_entry(self, entry: LogEntry) -> None: # Removes the first record which should also be the oldest self.popitem(last=False) - def to_list(self): + def to_list(self) -> list[dict[str, Any]]: """Return reversed list of log entries - LIFO.""" return [value.to_dict() for value in reversed(self.values())] @@ -236,7 +238,11 @@ class LogErrorHandler(logging.Handler): """Log handler for error messages.""" def __init__( - self, hass: HomeAssistant, maxlen: int, fire_event: bool, paths_re: re.Pattern + self, + hass: HomeAssistant, + maxlen: int, + fire_event: bool, + paths_re: re.Pattern[str], ) -> None: """Initialize a new LogErrorHandler.""" super().__init__() @@ -276,7 +282,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: hass.data[DOMAIN] = handler @callback - def _async_stop_handler(_) -> None: + def _async_stop_handler(_: Event) -> None: """Cleanup handler.""" logging.root.removeHandler(handler) del hass.data[DOMAIN] diff --git a/mypy.ini b/mypy.ini index e349f5d83fc38..f3be41c30363c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -3642,6 +3642,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.system_log.*] +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.systemmonitor.*] check_untyped_defs = true disallow_incomplete_defs = true