From: Kefu Chai Date: Tue, 5 May 2026 01:35:00 +0000 (+0800) Subject: mgr: narrow get_metadata return type with @overload X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5722e489f8fbacb865eabca5f94221c36d7eea00;p=ceph.git mgr: narrow get_metadata return type with @overload Enable type narrowing for get_metadata() when a non-None default is provided. Previously, the return type was always `Optional[Dict[str, str]]`, forcing callers to use defensive `assert metadata` checks even when a result was guaranteed. The wrapper returns either the metadata from `_ceph_get_metadata()` or the caller-supplied default. Providing an `@overload` allows type checkers to prove the result is non-None, avoiding invalid assertions for falsy defaults (like an empty defaultdict). This is a hygienic change with no runtime impact. Signed-off-by: Kefu Chai --- diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 3a217fb06dd8..3a27154fc171 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -11,6 +11,7 @@ from typing import ( NamedTuple, no_type_check, Optional, + overload, Sequence, Set, TYPE_CHECKING, @@ -1747,6 +1748,26 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): """ return cast(List[ServerInfoT], self._ceph_get_server(None)) + @overload + def get_metadata(self, + svc_type: str, + svc_id: str) -> Optional[Dict[str, str]]: + ... + + @overload + def get_metadata(self, + svc_type: str, + svc_id: str, + default: None) -> Optional[Dict[str, str]]: + ... + + @overload + def get_metadata(self, + svc_type: str, + svc_id: str, + default: Dict[str, str]) -> Dict[str, str]: + ... + def get_metadata(self, svc_type: str, svc_id: str, @@ -1756,12 +1777,14 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin): ceph-mgr fetches metadata asynchronously, so are windows of time during addition/removal of services where the metadata is not available to - modules. ``None`` is returned if no metadata is available. + modules. ``None`` is returned if no metadata is available, unless + ``default`` is provided, in which case ``default`` is returned. :param str svc_type: service type (e.g., 'mds', 'osd', 'mon') :param str svc_id: service id. convert OSD integer IDs to strings when calling this - :rtype: dict, or None if no metadata found + :param default: value to return when no metadata is available + :rtype: dict, or None if no metadata found and no default given """ metadata = self._ceph_get_metadata(svc_type, svc_id) if not metadata: