From 4fd8f045c48db258335072975a85818ec9b26a91 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Sun, 1 Oct 2023 12:49:59 -0400 Subject: [PATCH] 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 --- src/cephadm/cephadmlib/daemon_identity.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cephadm/cephadmlib/daemon_identity.py b/src/cephadm/cephadmlib/daemon_identity.py index 4d83ec8f9bfa1..aa47f2815d495 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 -- 2.39.5