From: John Mulligan Date: Sun, 1 Oct 2023 16:49:59 +0000 (-0400) Subject: cephadm: add a utilty method for type checking DaemonSubIdentity types X-Git-Tag: v19.3.0~284^2~29 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=4fd8f045c48db258335072975a85818ec9b26a91;p=ceph.git cephadm: add a utilty method for type checking DaemonSubIdentity types Add a `must` method that ensures a DaemonIdentity is a DaemonSubIdentity in a single call. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadmlib/daemon_identity.py b/src/cephadm/cephadmlib/daemon_identity.py index 4d83ec8f9bfa..aa47f2815d49 100644 --- a/src/cephadm/cephadmlib/daemon_identity.py +++ b/src/cephadm/cephadmlib/daemon_identity.py @@ -5,7 +5,7 @@ import os import pathlib import re -from typing import Union +from typing import Union, Optional from .context import CephadmContext @@ -169,3 +169,12 @@ class DaemonSubIdentity(DaemonIdentity): parent.daemon_id, subcomponent, ) + + @classmethod + def must(cls, value: Optional[DaemonIdentity]) -> 'DaemonSubIdentity': + """Helper to assert value is of the correct type. Mostly to make mypy + happy. + """ + if not isinstance(value, cls): + raise TypeError(f'{value!r} is not a {cls}') + return value