From f85eb70ca4500ef55f47c11f710616d5a979c9a8 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Fri, 26 Apr 2024 20:07:35 +1200 Subject: [PATCH] fix: tell type checkers that the config options are strings (#587) * Tell type checkers that the config options are strings. * Revert accidental commit. * Fix formatting. --- src/charm.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/charm.py b/src/charm.py index 9b51c3bf..b7e5a1b6 100755 --- a/src/charm.py +++ b/src/charm.py @@ -235,7 +235,7 @@ def __init__(self, *args): def _on_collect_unit_status(self, event: CollectStatusEvent): # "Pull" statuses retention_time = self.model.config.get("metrics_retention_time", "") - if not self._is_valid_timespec(retention_time): + if not self._is_valid_timespec(cast(str, retention_time)): event.add_status(BlockedStatus(f"Invalid time spec : {retention_time}")) # "Push" statuses @@ -300,7 +300,7 @@ def self_scraping_job(self): def log_level(self): """The log level configured for the charm.""" allowed_log_levels = ["debug", "info", "warn", "error", "fatal"] - log_level = self.model.config["log_level"].lower() + log_level = cast(str, self.model.config["log_level"]).lower() if log_level not in allowed_log_levels: logging.warning( @@ -732,11 +732,15 @@ def _generate_command(self) -> str: if config.get("metrics_wal_compression"): args.append("--storage.tsdb.wal-compression") - if self._is_valid_timespec(retention_time := config.get("metrics_retention_time", "")): + if self._is_valid_timespec( + retention_time := cast(str, config.get("metrics_retention_time", "")) + ): args.append(f"--storage.tsdb.retention.time={retention_time}") try: - ratio = self._percent_string_to_ratio(config.get("maximum_retention_size", "")) + ratio = self._percent_string_to_ratio( + cast(str, config.get("maximum_retention_size", "")) + ) except ValueError as e: logger.warning(e) @@ -902,9 +906,9 @@ def _prometheus_global_config(self) -> dict: } if config.get("evaluation_interval") and self._is_valid_timespec( - config["evaluation_interval"] + cast(str, config["evaluation_interval"]) ): - global_config["evaluation_interval"] = config["evaluation_interval"] + global_config["evaluation_interval"] = cast(str, config["evaluation_interval"]) return global_config