From 2008191007db77286c4f841bcebf82416773df05 Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Thu, 8 Feb 2024 13:50:56 +0100 Subject: [PATCH 1/3] tests: add mypy.ini This adds a mypy.ini configuration file. Signed-off-by: Guillaume Abrioux (cherry picked from commit 0ab763d7cb0e4bc1ed0f1d22db5b0581557545a4) --- mypy.ini | 9 +++++++++ tox.ini | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 mypy.ini diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..fd210cd --- /dev/null +++ b/mypy.ini @@ -0,0 +1,9 @@ +[mypy] +strict_optional = True +no_implicit_optional = True +warn_incomplete_stub = True +check_untyped_defs = True +show_error_context = True +allow_redefinition = True +disallow_untyped_defs = True + diff --git a/tox.ini b/tox.ini index eec4de3..5efee7d 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ skipsdist = True basepython = python3 deps = mypy -commands = mypy {toxinidir}/library {toxinidir}/module_utils +commands = mypy --config-file ./mypy.ini {toxinidir}/library {toxinidir}/module_utils [testenv:flake8] basepython = python3 From f72105ac51f4e420089268bed1e7423da27ab177 Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Thu, 8 Feb 2024 14:08:46 +0100 Subject: [PATCH 2/3] tests: address mypy errors The new mypy options used brought out new mypy errors. This commit addresses them. Signed-off-by: Guillaume Abrioux (cherry picked from commit 15ec250c6e5ac8ffc73a0bfccd7cf21414909854) --- library/ceph_config.py | 6 +++--- library/ceph_orch_apply.py | 2 +- library/cephadm_bootstrap.py | 10 +++++----- module_utils/ceph_common.py | 10 ++++++---- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/library/ceph_config.py b/library/ceph_config.py index 57df331..d7a52b7 100644 --- a/library/ceph_config.py +++ b/library/ceph_config.py @@ -3,7 +3,7 @@ # Author: Guillaume Abrioux from __future__ import absolute_import, division, print_function -from typing import List, Tuple +from typing import Any, Dict, List, Tuple, Union __metaclass__ = type from ansible.module_utils.basic import AnsibleModule # type: ignore @@ -97,7 +97,7 @@ def set_option(module: "AnsibleModule", return rc, cmd, out.strip(), err -def get_config_dump(module: "AnsibleModule"): +def get_config_dump(module: "AnsibleModule") -> Tuple[int, List[str], str, str]: cmd = build_base_cmd_shell(module) cmd.extend(['ceph', 'config', 'dump', '--format', 'json']) rc, out, err = module.run_command(cmd) @@ -107,7 +107,7 @@ def get_config_dump(module: "AnsibleModule"): return rc, cmd, out, err -def get_current_value(who, option, config_dump): +def get_current_value(who: str, option: str, config_dump: List[Dict[str, Any]]) -> Union[str, None]: for config in config_dump: if config['section'] == who and config['name'] == option: return config['value'] diff --git a/library/ceph_orch_apply.py b/library/ceph_orch_apply.py index a397328..f110618 100644 --- a/library/ceph_orch_apply.py +++ b/library/ceph_orch_apply.py @@ -82,7 +82,7 @@ def apply_spec(module: "AnsibleModule", return rc, cmd, out, err -def main(): +def main() -> None: module = AnsibleModule( argument_spec=dict( fsid=dict(type='str', required=False), diff --git a/library/cephadm_bootstrap.py b/library/cephadm_bootstrap.py index 7f34c8d..bfa01fb 100644 --- a/library/cephadm_bootstrap.py +++ b/library/cephadm_bootstrap.py @@ -249,14 +249,14 @@ def run_module() -> None: ceph_pubkey = 'ceph.pub' def extend_append(key: str, parameters: dict) -> None: - if parameters[key]["type"] == 'bool': - cmd.append("--" + k.replace('_', '-')) + if parameters[key]['type'] == 'bool': + cmd.append('--' + k.replace('_', '-')) else: - cmd.extend(["--" + k.replace('_', '-'), module.params.get(k)]) + cmd.extend(['--' + k.replace('_', '-'), module.params.get(k)]) if fsid: if os.path.exists(os.path.join(data_dir, fsid)): - out = f"A cluster with fsid {fsid} is already deployed." + out = f'A cluster with fsid {fsid} is already deployed.' exit_module( rc=0, startd=startd, @@ -339,7 +339,7 @@ def extend_append(key: str, parameters: dict) -> None: ) -def main(): +def main() -> None: run_module() diff --git a/module_utils/ceph_common.py b/module_utils/ceph_common.py index 99c5da8..cc19df4 100644 --- a/module_utils/ceph_common.py +++ b/module_utils/ceph_common.py @@ -1,14 +1,16 @@ import datetime import time -from typing import TYPE_CHECKING, List, Dict +from typing import TYPE_CHECKING, Any, List, Dict, Callable, Type, TypeVar if TYPE_CHECKING: from ansible.module_utils.basic import AnsibleModule # type: ignore +ExceptionType = TypeVar('ExceptionType', bound=BaseException) -def retry(exceptions, retries=20, delay=1): - def decorator(f): - def _retry(*args, **kwargs): + +def retry(exceptions: Type[ExceptionType], retries: int = 20, delay: int = 1) -> Callable: + def decorator(f: Callable) -> Callable: + def _retry(*args: Any, **kwargs: Any) -> Callable: _tries = retries while _tries > 1: try: From 6961cfb958f0585ea403786a44d3b6cf74549993 Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Tue, 13 Feb 2024 10:42:58 +0100 Subject: [PATCH 3/3] library: fix error message in cephadm_bootstrap module 2645b6ad95be2ed787a51d43739deb8a7e098143 introduced a cosmetic issue. That message was intended to print the path using f-strings. This commit addresses this. Signed-off-by: Guillaume Abrioux (cherry picked from commit a31eef878860a1bde01c5d6d1e06f3bb61cb200a) --- library/cephadm_bootstrap.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/cephadm_bootstrap.py b/library/cephadm_bootstrap.py index bfa01fb..15ae6e4 100644 --- a/library/cephadm_bootstrap.py +++ b/library/cephadm_bootstrap.py @@ -270,8 +270,9 @@ def extend_append(key: str, parameters: dict) -> None: ceph_keyring, ceph_pubkey]: if not allow_overwrite: - if os.path.exists(os.path.join(data_dir, f)): - out = '{} already exists, skipping.' + path: str = os.path.join(data_dir, f) + if os.path.exists(path): + out = f'{path} already exists, skipping.' exit_module( rc=0, startd=startd,