From 1f3c2fe62215f9627c27ea0ae423a6302c9071d4 Mon Sep 17 00:00:00 2001 From: Samuel Allan Date: Wed, 2 Oct 2024 10:39:02 +0930 Subject: [PATCH] Add type casting to fix failing mypy lint The mypy lints were failing since the release of ops 2.17.0. The problem does not appear to be with ops, but rather with how type narrowing works with mypy, and how the charm assigns the unit status in different methods. See https://github.com/canonical/operator/issues/1401 for more context. We can fix the issue with no changes to the charm logic by adding type casting to undo the type narrowing in this case. --- src/charm.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/charm.py b/src/charm.py index 029bda9..c4c4df8 100755 --- a/src/charm.py +++ b/src/charm.py @@ -36,7 +36,7 @@ ) from ops.framework import StoredState from ops.main import main -from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus +from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, StatusBase from storage_connector import metrics_utils, nrpe_utils import utils # noqa @@ -158,7 +158,8 @@ def __init__(self, *args: Any) -> None: def _on_install(self, _: InstallEvent) -> None: """Handle install state.""" - self.unit.status = MaintenanceStatus("Installing charm software") + # type casting is to keep mypy happy; see https://github.com/canonical/operator/issues/1401 + self.unit.status = cast(StatusBase, MaintenanceStatus("Installing charm software")) if self._check_if_container(): return @@ -199,7 +200,8 @@ def _render_config(self, _: ConfigChangedEvent) -> None: if self._check_if_container(): return - self.unit.status = MaintenanceStatus("Validating charm configuration") + # type casting is to keep mypy happy; see https://github.com/canonical/operator/issues/1401 + self.unit.status = cast(StatusBase, MaintenanceStatus("Validating charm configuration")) self._check_mandatory_config() if isinstance(self.unit.status, BlockedStatus): return @@ -209,7 +211,8 @@ def _render_config(self, _: ConfigChangedEvent) -> None: if isinstance(self.unit.status, BlockedStatus): return - self.unit.status = MaintenanceStatus("Rendering charm configuration") + # type casting is to keep mypy happy; see https://github.com/canonical/operator/issues/1401 + self.unit.status = cast(StatusBase, MaintenanceStatus("Rendering charm configuration")) self._create_directories() tenv = Environment(loader=FileSystemLoader("templates"))