diff --git a/src/molecule/dependency/ansible_galaxy/base.py b/src/molecule/dependency/ansible_galaxy/base.py index 23ab6a2c0f..04244f5bf3 100644 --- a/src/molecule/dependency/ansible_galaxy/base.py +++ b/src/molecule/dependency/ansible_galaxy/base.py @@ -120,7 +120,7 @@ def options(self) -> MutableMapping[str, str | bool]: Returns: Merged and filtered options for this dependency. """ - opts: MutableMapping[str, str | bool] = self._config.config["dependency"]["options"] + opts = self._config.config["dependency"]["options"] # NOTE(retr0h): Remove verbose options added by the user while in # debug. if self._config.debug: diff --git a/src/molecule/dependency/ansible_galaxy/collections.py b/src/molecule/dependency/ansible_galaxy/collections.py index 4a2e43c2da..0fb5841170 100644 --- a/src/molecule/dependency/ansible_galaxy/collections.py +++ b/src/molecule/dependency/ansible_galaxy/collections.py @@ -5,7 +5,7 @@ import logging from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, cast from molecule import util from molecule.dependency.ansible_galaxy.base import AnsibleGalaxyBase @@ -58,4 +58,4 @@ def requirements_file(self) -> str: Returns: Path to the requirements file for this dependency. """ - return self.options["requirements-file"] # type: ignore[return-value] + return cast(str, self.options["requirements-file"]) diff --git a/src/molecule/dependency/ansible_galaxy/roles.py b/src/molecule/dependency/ansible_galaxy/roles.py index ec2e67b59a..49d105c44b 100644 --- a/src/molecule/dependency/ansible_galaxy/roles.py +++ b/src/molecule/dependency/ansible_galaxy/roles.py @@ -5,7 +5,7 @@ import logging from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, cast from molecule import util from molecule.dependency.ansible_galaxy.base import AnsibleGalaxyBase @@ -57,4 +57,4 @@ def requirements_file(self) -> str: Returns: Path to the requirements file for this dependency. """ - return self.options["role-file"] # type: ignore[return-value] + return cast(str, self.options["role-file"]) diff --git a/src/molecule/provisioner/ansible.py b/src/molecule/provisioner/ansible.py index 6cfe0b082e..bbd8a0cbf1 100644 --- a/src/molecule/provisioner/ansible.py +++ b/src/molecule/provisioner/ansible.py @@ -39,9 +39,10 @@ if TYPE_CHECKING: - from collections.abc import MutableMapping from typing import Any + from molecule.types import Options + Vivify = collections.defaultdict[str, Any | "Vivify"] @@ -578,17 +579,17 @@ def config_options(self) -> dict[str, Any]: ) @property - def options(self) -> MutableMapping[str, str | bool]: # noqa: D102 + def options(self) -> Options: # noqa: D102 if self._config.action in ["create", "destroy"]: return self.default_options - o: MutableMapping[str, str | bool] = self._config.config["provisioner"]["options"] + opts = self._config.config["provisioner"]["options"] # NOTE(retr0h): Remove verbose options added by the user while in # debug. if self._config.debug: - o = util.filter_verbose_permutation(o) + opts = util.filter_verbose_permutation(opts) - return util.merge_dicts(self.default_options, o) + return util.merge_dicts(self.default_options, opts) @property def env(self) -> dict[str, str]: # noqa: D102 diff --git a/src/molecule/types.py b/src/molecule/types.py index b3cf74c201..6004f65312 100644 --- a/src/molecule/types.py +++ b/src/molecule/types.py @@ -2,11 +2,14 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Literal, TypedDict +from typing import TYPE_CHECKING, TypedDict if TYPE_CHECKING: - from typing import Any + from collections.abc import MutableMapping + from typing import Any, Literal, TypeAlias + + Options: TypeAlias = MutableMapping[str, str | bool] class DependencyData(TypedDict, total=False): @@ -23,7 +26,7 @@ class DependencyData(TypedDict, total=False): name: str command: str | None enabled: bool - options: dict[str, str | bool] + options: Options env: dict[str, str] @@ -128,10 +131,10 @@ class ProvisionerData(TypedDict, total=False): """ name: str - config_options: dict[str, str | bool] + config_options: dict[str, Any] ansible_args: list[str] - connection_options: dict[str, str | bool] - options: dict[str, str | bool] + connection_options: dict[str, Any] + options: Options env: dict[str, str] inventory: InventoryData children: dict[str, Any] @@ -176,7 +179,7 @@ class VerifierData(TypedDict, total=False): name: str directory: str enabled: bool - options: dict[str, str | bool] + options: Options env: dict[str, str] additional_files_or_dirs: list[str] diff --git a/src/molecule/util.py b/src/molecule/util.py index ade8dde0a3..844c012b83 100644 --- a/src/molecule/util.py +++ b/src/molecule/util.py @@ -43,12 +43,12 @@ if TYPE_CHECKING: - from collections.abc import Generator, Iterable, Mapping, MutableMapping + from collections.abc import Generator, Iterable, MutableMapping from io import TextIOWrapper from typing import Any, AnyStr, NoReturn, TypeVar from warnings import WarningMessage - from molecule.types import CommandArgs, ConfigData, PlatformData + from molecule.types import CommandArgs, ConfigData, Options, PlatformData NestedDict = MutableMapping[str, Any] _T = TypeVar("_T", bound=NestedDict) @@ -355,7 +355,7 @@ def instance_with_scenario_name(instance_name: str, scenario_name: str) -> str: return f"{instance_name}-{scenario_name}" -def verbose_flag(options: MutableMapping[str, str | bool]) -> list[str]: +def verbose_flag(options: Options) -> list[str]: """Return computed verbosity flag. Args: @@ -378,9 +378,7 @@ def verbose_flag(options: MutableMapping[str, str | bool]) -> list[str]: return flags -def filter_verbose_permutation( - options: Mapping[str, str | bool], -) -> MutableMapping[str, str | bool]: +def filter_verbose_permutation(options: Options) -> Options: """Clean verbose information. Args: diff --git a/src/molecule/verifier/testinfra.py b/src/molecule/verifier/testinfra.py index 1bf137d0d9..d419786501 100644 --- a/src/molecule/verifier/testinfra.py +++ b/src/molecule/verifier/testinfra.py @@ -148,7 +148,7 @@ def options(self) -> MutableMapping[str, str | bool]: Returns: The combined dictionary of default options and those specified in the config. """ - o: MutableMapping[str, str | bool] = self._config.config["verifier"]["options"] + o = self._config.config["verifier"]["options"] # NOTE(retr0h): Remove verbose options added by the user while in # debug. if self._config.debug: diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index 28f4c03bb4..095e10dbf9 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -42,6 +42,8 @@ from pytest_mock import MockerFixture + from molecule.types import Options + def test_print_debug() -> None: # noqa: D103 expected = "DEBUG: test_title:\ntest_data\n" @@ -284,7 +286,7 @@ def test_instance_with_scenario_name() -> None: # noqa: D103 def test_verbose_flag() -> None: # noqa: D103 - options = {"verbose": True, "v": True} + options: Options = {"verbose": True, "v": True} assert util.verbose_flag(options) == ["-v"] # pylint: disable=use-implicit-booleaness-not-comparison @@ -292,7 +294,7 @@ def test_verbose_flag() -> None: # noqa: D103 def test_verbose_flag_extra_verbose() -> None: # noqa: D103 - options = {"verbose": True, "vvv": True} + options: Options = {"verbose": True, "vvv": True} assert util.verbose_flag(options) == ["-vvv"] # pylint: disable=use-implicit-booleaness-not-comparison @@ -300,7 +302,7 @@ def test_verbose_flag_extra_verbose() -> None: # noqa: D103 def test_verbose_flag_preserves_verbose_option() -> None: # noqa: D103 - options = {"verbose": True} + options: Options = {"verbose": True} # pylint: disable=use-implicit-booleaness-not-comparison assert util.verbose_flag(options) == [] @@ -308,7 +310,7 @@ def test_verbose_flag_preserves_verbose_option() -> None: # noqa: D103 def test_filter_verbose_permutation() -> None: # noqa: D103 - options = { + options: Options = { "v": True, "vv": True, "vvv": True,