Skip to content

Commit

Permalink
Enable strict typing for system_log (#107914)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Jan 13, 2024
1 parent 7bcfcfe commit ca1aaac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down
22 changes: 14 additions & 8 deletions homeassistant/components/system_log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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())]

Expand All @@ -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__()
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca1aaac

Please sign in to comment.