mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Enable strict typing for system_log (#107914)
This commit is contained in:
parent
7bcfcfef5f
commit
ca1aaacc90
@ -388,6 +388,7 @@ homeassistant.components.switchbot_cloud.*
|
|||||||
homeassistant.components.switcher_kis.*
|
homeassistant.components.switcher_kis.*
|
||||||
homeassistant.components.synology_dsm.*
|
homeassistant.components.synology_dsm.*
|
||||||
homeassistant.components.system_health.*
|
homeassistant.components.system_health.*
|
||||||
|
homeassistant.components.system_log.*
|
||||||
homeassistant.components.systemmonitor.*
|
homeassistant.components.systemmonitor.*
|
||||||
homeassistant.components.tag.*
|
homeassistant.components.tag.*
|
||||||
homeassistant.components.tailscale.*
|
homeassistant.components.tailscale.*
|
||||||
|
@ -13,10 +13,12 @@ import voluptuous as vol
|
|||||||
from homeassistant import __path__ as HOMEASSISTANT_PATH
|
from homeassistant import __path__ as HOMEASSISTANT_PATH
|
||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
|
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
|
KeyType = tuple[str, tuple[str, int], str | None]
|
||||||
|
|
||||||
CONF_MAX_ENTRIES = "max_entries"
|
CONF_MAX_ENTRIES = "max_entries"
|
||||||
CONF_FIRE_EVENT = "fire_event"
|
CONF_FIRE_EVENT = "fire_event"
|
||||||
CONF_MESSAGE = "message"
|
CONF_MESSAGE = "message"
|
||||||
@ -60,7 +62,7 @@ SERVICE_WRITE_SCHEMA = vol.Schema(
|
|||||||
|
|
||||||
|
|
||||||
def _figure_out_source(
|
def _figure_out_source(
|
||||||
record: logging.LogRecord, paths_re: re.Pattern
|
record: logging.LogRecord, paths_re: re.Pattern[str]
|
||||||
) -> tuple[str, int]:
|
) -> tuple[str, int]:
|
||||||
"""Figure out where a log message came from."""
|
"""Figure out where a log message came from."""
|
||||||
# If a stack trace exists, extract file names from the entire call stack.
|
# If a stack trace exists, extract file names from the entire call stack.
|
||||||
@ -184,7 +186,7 @@ class LogEntry:
|
|||||||
self.count = 1
|
self.count = 1
|
||||||
self.key = (self.name, source, self.root_cause)
|
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."""
|
"""Convert object into dict to maintain backward compatibility."""
|
||||||
return {
|
return {
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
@ -198,10 +200,10 @@ class LogEntry:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class DedupStore(OrderedDict):
|
class DedupStore(OrderedDict[KeyType, LogEntry]):
|
||||||
"""Data store to hold max amount of deduped entries."""
|
"""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."""
|
"""Initialize a new DedupStore."""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.maxlen = maxlen
|
self.maxlen = maxlen
|
||||||
@ -227,7 +229,7 @@ class DedupStore(OrderedDict):
|
|||||||
# Removes the first record which should also be the oldest
|
# Removes the first record which should also be the oldest
|
||||||
self.popitem(last=False)
|
self.popitem(last=False)
|
||||||
|
|
||||||
def to_list(self):
|
def to_list(self) -> list[dict[str, Any]]:
|
||||||
"""Return reversed list of log entries - LIFO."""
|
"""Return reversed list of log entries - LIFO."""
|
||||||
return [value.to_dict() for value in reversed(self.values())]
|
return [value.to_dict() for value in reversed(self.values())]
|
||||||
|
|
||||||
@ -236,7 +238,11 @@ class LogErrorHandler(logging.Handler):
|
|||||||
"""Log handler for error messages."""
|
"""Log handler for error messages."""
|
||||||
|
|
||||||
def __init__(
|
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:
|
) -> None:
|
||||||
"""Initialize a new LogErrorHandler."""
|
"""Initialize a new LogErrorHandler."""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -276,7 +282,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
hass.data[DOMAIN] = handler
|
hass.data[DOMAIN] = handler
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_stop_handler(_) -> None:
|
def _async_stop_handler(_: Event) -> None:
|
||||||
"""Cleanup handler."""
|
"""Cleanup handler."""
|
||||||
logging.root.removeHandler(handler)
|
logging.root.removeHandler(handler)
|
||||||
del hass.data[DOMAIN]
|
del hass.data[DOMAIN]
|
||||||
|
10
mypy.ini
10
mypy.ini
@ -3642,6 +3642,16 @@ disallow_untyped_defs = true
|
|||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = 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.*]
|
[mypy-homeassistant.components.systemmonitor.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user