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..15ae6e4 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, @@ -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, @@ -339,7 +340,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: 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 35fad01..f5aa3f0 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