From 4dd58342b8c4415a2a5c06a05a7d1ecd83c6ec0e Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 11 Feb 2026 10:17:52 +0100 Subject: [PATCH] Fix RestartPolicy type annotation for runtime type checking (#6546) * Fix RestartPolicy type annotation for runtime type checking The restart_policy property returned a plain str from the Docker API instead of a RestartPolicy instance, causing TypeCheckError with typeguard. Use explicit mapping via _restart_policy_from_model(), consistent with the existing _container_state_from_model() pattern, to always return a proper RestartPolicy enum member. Unknown values from Docker are logged and default to RestartPolicy.NO. Co-Authored-By: Claude Opus 4.6 * Drop unnecessary _RESTART_POLICY_MAP --------- Co-authored-by: Claude Opus 4.6 --- supervisor/docker/interface.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/supervisor/docker/interface.py b/supervisor/docker/interface.py index 80028723c..ce5e4d664 100644 --- a/supervisor/docker/interface.py +++ b/supervisor/docker/interface.py @@ -58,6 +58,22 @@ MAP_ARCH: dict[CpuArch, str] = { } +def _restart_policy_from_model(meta_host: dict[str, Any]) -> RestartPolicy | None: + """Get restart policy from host config model.""" + if "RestartPolicy" not in meta_host: + return None + + name = meta_host["RestartPolicy"].get("Name") + if not name: + return RestartPolicy.NO + + if name in RestartPolicy: + return RestartPolicy(name) + + _LOGGER.warning("Unknown Docker restart policy '%s', treating as no", name) + return RestartPolicy.NO + + def _container_state_from_model(container_metadata: dict[str, Any]) -> ContainerState: """Get container state from model.""" if "State" not in container_metadata: @@ -157,11 +173,7 @@ class DockerInterface(JobGroup, ABC): @property def restart_policy(self) -> RestartPolicy | None: """Return restart policy of container.""" - if "RestartPolicy" not in self.meta_host: - return None - - policy = self.meta_host["RestartPolicy"].get("Name") - return policy if policy else RestartPolicy.NO + return _restart_policy_from_model(self.meta_host) @property def security_opt(self) -> list[str]: