From 6034e36b055c4711849bb388441693ff397bd6a8 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Fri, 8 May 2020 11:09:08 +0200 Subject: [PATCH] mgr/cephadm: add some more type checks Signed-off-by: Sebastian Wagner (cherry picked from commit 85c812c4ed6a7eec25d0d7749dae376bea003fa8) --- src/pybind/mgr/cephadm/module.py | 39 +++++++++++++++----------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index 89aca87144c43..3e3c8d1a0b22c 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -6,13 +6,8 @@ from threading import Event from functools import wraps import string -try: - from typing import List, Dict, Optional, Callable, Tuple, TypeVar, Type, \ - Any, NamedTuple, Iterator, Set, Sequence - from typing import TYPE_CHECKING, cast -except ImportError: - TYPE_CHECKING = False # just for type checking - +from typing import List, Dict, Optional, Callable, Tuple, TypeVar, Type, \ + Any, NamedTuple, Iterator, Set, Sequence, TYPE_CHECKING, cast, Union import datetime import six @@ -60,6 +55,8 @@ except ImportError: logger = logging.getLogger(__name__) +T = TypeVar('T') + DEFAULT_SSH_CONFIG = """ Host * User root @@ -164,9 +161,9 @@ class AsyncCompletion(orchestrator.Completion): self._on_complete_ = inner -def forall_hosts(f): +def forall_hosts(f: Callable[..., T]) -> Callable[..., List[T]]: @wraps(f) - def forall_hosts_wrapper(*args) -> list: + def forall_hosts_wrapper(*args) -> List[T]: # Some weired logic to make calling functions with multiple arguments work. if len(args) == 1: @@ -1353,7 +1350,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def remove_daemons(self, names): - # type: (List[str]) -> orchestrator.Completion + # type: (List[str]) -> List[str] args = [] for host, dm in self.cache.daemons.items(): for name in names: @@ -1584,10 +1581,10 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): 'Reconfigured' if reconfig else 'Deployed', name, host) @forall_hosts - def _remove_daemons(self, name, host): + def _remove_daemons(self, name, host) -> str: return self._remove_daemon(name, host) - def _remove_daemon(self, name, host): + def _remove_daemon(self, name, host) -> str: """ Remove a daemon """ @@ -1818,7 +1815,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): e) def _add_daemon(self, daemon_type, spec, - create_func, config_func=None): + create_func: Callable[..., T], config_func=None) -> List[T]: """ Add (and place) a daemon. Require explicit host placement. Do not schedule, and do not apply the related scheduling limitations. @@ -1834,7 +1831,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def _create_daemons(self, daemon_type, spec, daemons, hosts, count, - create_func, config_func=None): + create_func: Callable[..., T], config_func=None) -> List[T]: if count > len(hosts): raise OrchestratorError('too few hosts: want %d, have %s' % ( count, hosts)) @@ -1876,12 +1873,12 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def add_mon(self, spec): - # type: (ServiceSpec) -> orchestrator.Completion + # type: (ServiceSpec) -> List[str] return self._add_daemon('mon', spec, self.mon_service.create) @trivial_completion def add_mgr(self, spec): - # type: (ServiceSpec) -> orchestrator.Completion + # type: (ServiceSpec) -> List[str] return self._add_daemon('mgr', spec, self.mgr_service.create) def _apply(self, spec: ServiceSpec) -> str: @@ -1946,7 +1943,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def add_iscsi(self, spec): - # type: (ServiceSpec) -> orchestrator.Completion + # type: (ServiceSpec) -> List[str] return self._add_daemon('iscsi', spec, self.iscsi_servcie.create, self.iscsi_servcie.config) @trivial_completion @@ -1983,7 +1980,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def add_node_exporter(self, spec): - # type: (ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> List[str] return self._add_daemon('node-exporter', spec, self.node_exporter_service.create) @@ -1993,7 +1990,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def add_crash(self, spec): - # type: (ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> List[str] return self._add_daemon('crash', spec, self.crash_service.create) @@ -2003,7 +2000,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def add_grafana(self, spec): - # type: (ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> List[str] return self._add_daemon('grafana', spec, self.grafana_service.create) @trivial_completion @@ -2012,7 +2009,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): @trivial_completion def add_alertmanager(self, spec): - # type: (ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> List[str] return self._add_daemon('alertmanager', spec, self.alertmanager_service.create) @trivial_completion -- 2.39.5