Skip to content

Commit

Permalink
Clean up types
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Dec 2, 2024
1 parent 4f55256 commit d77a4e4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/molecule/dependency/ansible_galaxy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/dependency/ansible_galaxy/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
4 changes: 2 additions & 2 deletions src/molecule/dependency/ansible_galaxy/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
11 changes: 6 additions & 5 deletions src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/molecule/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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]


Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]

Expand Down
10 changes: 4 additions & 6 deletions src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/verifier/testinfra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -284,31 +286,31 @@ 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
assert options == {}


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
assert options == {}


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) == []
assert options == {"verbose": True}


def test_filter_verbose_permutation() -> None: # noqa: D103
options = {
options: Options = {
"v": True,
"vv": True,
"vvv": True,
Expand Down

0 comments on commit d77a4e4

Please sign in to comment.