1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-05-08 08:58:31 +01:00

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 <noreply@anthropic.com>

* Drop unnecessary _RESTART_POLICY_MAP

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Stefan Agner
2026-02-11 10:17:52 +01:00
committed by GitHub
parent 825ff415e0
commit 4dd58342b8
+17 -5
View File
@@ -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]: