Skip to content

Commit

Permalink
Continue to add hints and docstrings (#4291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos authored Oct 7, 2024
1 parent fce2298 commit 000d200
Show file tree
Hide file tree
Showing 19 changed files with 411 additions and 288 deletions.
102 changes: 1 addition & 101 deletions .config/pydoclint-baseline.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ allow-init-docstring = true
arg-type-hints-in-docstring = false
baseline = ".config/pydoclint-baseline.txt"
check-return-types = false
check-yield-types = false
exclude = '\.cache|\.git|\.tox|build|out|venv'
should-document-private-class-attributes = true
show-filenames-in-every-violation-message = true
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def execute_subcommand(
"""
(subcommand, *args) = subcommand_and_args.split(" ")
command_module = getattr(molecule.command, subcommand)
command = getattr(command_module, text.camelize(subcommand)) # type: ignore[no-untyped-call]
command = getattr(command_module, text.camelize(subcommand))

# knowledge of the current action is used by some provisioners
# to ensure they behave correctly during certain sequence steps,
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/command/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ def check(ctx, scenario_name, parallel): # type: ignore[no-untyped-def] # pragm
command_args = {"parallel": parallel, "subcommand": subcommand}

if parallel:
util.validate_parallel_cmd_args(command_args) # type: ignore[no-untyped-call]
util.validate_parallel_cmd_args(command_args)

base.execute_cmdline_scenarios(scenario_name, args, command_args)
2 changes: 1 addition & 1 deletion src/molecule/command/destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ def destroy(ctx, scenario_name, driver_name, __all, parallel): # type: ignore[n
scenario_name = None

if parallel:
util.validate_parallel_cmd_args(command_args) # type: ignore[no-untyped-call]
util.validate_parallel_cmd_args(command_args)

base.execute_cmdline_scenarios(scenario_name, args, command_args)
2 changes: 1 addition & 1 deletion src/molecule/command/idempotence.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _non_idempotent_tasks(self, output): # type: ignore[no-untyped-def] # noqa
output = re.sub(r"\n\s*\n*", "\n", output)

# Remove ansi escape sequences.
output = strip_ansi_escape(output) # type: ignore[no-untyped-call]
output = strip_ansi_escape(output)

# Split the output into a list and go through it.
output_lines = output.split("\n")
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/command/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ def test( # type: ignore[no-untyped-def] # noqa: ANN201, PLR0913
scenario_name = None

if parallel:
util.validate_parallel_cmd_args(command_args) # type: ignore[no-untyped-call]
util.validate_parallel_cmd_args(command_args)

base.execute_cmdline_scenarios(scenario_name, args, command_args, ansible_args)
4 changes: 2 additions & 2 deletions src/molecule/dependency/ansible_galaxy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
# NOTE(retr0h): Remove verbose options added by the user while in
# debug.
if self._config.debug:
o = util.filter_verbose_permutation(o) # type: ignore[no-untyped-call]
o = util.filter_verbose_permutation(o)

o = util.merge_dicts(self.default_options, o)
return self.filter_options(o, self.FILTER_OPTS) # type: ignore[no-untyped-call]
Expand All @@ -101,7 +101,7 @@ def default_env(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def bake(self): # type: ignore[no-untyped-def] # noqa: ANN201
"""Bake an ``ansible-galaxy`` command so it's ready to execute and returns None."""
options = self.options
verbose_flag = util.verbose_flag(options) # type: ignore[no-untyped-call]
verbose_flag = util.verbose_flag(options)

self._sh_command = [
self.command,
Expand Down
8 changes: 4 additions & 4 deletions src/molecule/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def github_actions_groups(func: Callable) -> Callable: # type: ignore[type-arg]
def wrapper(*args, **kwargs): # type: ignore[no-untyped-def] # noqa: ANN002, ANN003, ANN202
self = args[0]
scenario = self._config.scenario.name
subcommand = underscore(self.__class__.__name__) # type: ignore[no-untyped-call]
subcommand = underscore(self.__class__.__name__)
console.print(
"::group::",
f"[ci_info]Molecule[/] [scenario]{scenario}[/] > [action]{subcommand}[/]",
Expand Down Expand Up @@ -122,7 +122,7 @@ def gitlab_ci_sections(func: Callable) -> Callable: # type: ignore[type-arg]
def wrapper(*args, **kwargs): # type: ignore[no-untyped-def] # noqa: ANN002, ANN003, ANN202
self = args[0]
scenario = self._config.scenario.name
subcommand = underscore(self.__class__.__name__) # type: ignore[no-untyped-call]
subcommand = underscore(self.__class__.__name__)
console.print(
f"section_start:{int(time.time())}:{scenario}.{subcommand}",
end=clear_line,
Expand Down Expand Up @@ -159,7 +159,7 @@ def travis_ci_folds(func: Callable) -> Callable: # type: ignore[type-arg]
def wrapper(*args, **kwargs): # type: ignore[no-untyped-def] # noqa: ANN002, ANN003, ANN202
self = args[0]
scenario = self._config.scenario.name
subcommand = underscore(self.__class__.__name__) # type: ignore[no-untyped-call]
subcommand = underscore(self.__class__.__name__)
console.print(
f"travis_fold:start:{scenario}.{subcommand}",
f"[ci_info]Molecule[/] [scenario]{scenario}[/] > [action]{subcommand}[/]",
Expand Down Expand Up @@ -190,7 +190,7 @@ def wrapper(*args, **kwargs): # type: ignore[no-untyped-def] # noqa: ANN002, A
LOG.info(
"[info]Running [scenario]%s[/] > [action]%s[/][/]",
self._config.scenario.name,
underscore(self.__class__.__name__), # type: ignore[no-untyped-call]
underscore(self.__class__.__name__),
extra={"markup": True},
)
rt = func(*args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
# NOTE(retr0h): Remove verbose options added by the user while in
# debug.
if self._config.debug:
o = util.filter_verbose_permutation(o) # type: ignore[no-untyped-call]
o = util.filter_verbose_permutation(o)

return util.merge_dicts(self.default_options, o)

Expand Down Expand Up @@ -768,7 +768,7 @@ def verify(self, action_args=None): # type: ignore[no-untyped-def] # noqa: ANN

def write_config(self): # type: ignore[no-untyped-def] # noqa: ANN201
"""Write the provisioner's config file to disk and returns None."""
template = util.render_template( # type: ignore[no-untyped-call]
template = util.render_template(
self._get_config_template(), # type: ignore[no-untyped-call]
config_options=self.config_options,
)
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def bake(self): # type: ignore[no-untyped-def] # noqa: ANN201
# inventory sources located under
self.add_cli_arg("inventory", self._config.provisioner.inventory_directory) # type: ignore[no-untyped-call]
options = util.merge_dicts(self._config.provisioner.options, self._cli)
verbose_flag = util.verbose_flag(options) # type: ignore[no-untyped-call]
verbose_flag = util.verbose_flag(options)
if self._playbook != self._config.provisioner.playbooks.converge: # noqa: SIM102
if options.get("become"):
del options["become"]
Expand Down Expand Up @@ -111,7 +111,7 @@ def execute(self, action_args=None): # type: ignore[no-untyped-def] # noqa: AN
cwd = self._config.scenario_path
result = util.run_command(
cmd=self._ansible_command, # type: ignore[arg-type]
env=self._env,
env=self._env, # type: ignore[arg-type]
debug=self._config.debug,
cwd=cwd,
)
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def prune(self: Scenario) -> None:
self.config.state.state_file,
*self.config.driver.safe_files,
]
files = util.os_walk(self.ephemeral_directory, "*") # type: ignore[no-untyped-call]
for f in files:
existing_files = util.os_walk(self.ephemeral_directory, "*")
for f in existing_files:
if not any(sf for sf in safe_files if fnmatch.fnmatch(f, sf)):
try:
os.remove(f) # noqa: PTH107
Expand Down
106 changes: 80 additions & 26 deletions src/molecule/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,107 @@

import re

from typing import TYPE_CHECKING

def camelize(string): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
"""Format string as camel-case."""

if TYPE_CHECKING:
from typing import AnyStr


def camelize(string: str) -> str:
"""Format string as camel-case.
Args:
string: The string to be formatted.
Returns:
The formatted string.
"""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
return re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), string)


def underscore(string: str) -> str:
"""Format string to underlined notation.
Args:
string: The string to be formatted.
Returns:
The formatted string.
"""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
string = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", string)
string = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", string)
string = string.replace("-", "_")

return string.lower()


def title(word: str) -> str:
"""Format title.
Args:
word: The string to be formatted.
Returns:
The formatted string.
"""
return " ".join(x.capitalize() or "_" for x in word.split("_"))


def chomp(text: str) -> str:
"""Remove any training spaces from string."""
"""Remove any training spaces from a multiline string.
Args:
text: Block of text to strip.
Returns:
Text with trailing spaces stripped from newlines.
"""
return "\n".join([x.rstrip() for x in text.splitlines()])


def strip_ansi_escape(data): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
def strip_ansi_escape(data: AnyStr) -> str:
"""Remove all ANSI escapes from string or bytes.
If bytes is passed instead of string, it will be converted to string
using UTF-8.
Args:
data: Text to clean in string or bytes.
Returns:
String cleaned of ANSI escape sequences.
"""
if isinstance(data, bytes):
data = data.decode("utf-8")
text = _to_unicode(data)
return re.sub(r"\x1b[^m]*m", "", text)

return re.sub(r"\x1b[^m]*m", "", data)

def strip_ansi_color(data: AnyStr) -> str:
"""Remove ANSI colors from string or bytes.
def strip_ansi_color(data): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
"""Remove ANSI colors from string or bytes."""
if isinstance(data, bytes):
data = data.decode("utf-8")
Args:
data: Text to clean in string or bytes.
Returns:
String cleaned of ANSI colors.
"""
# Taken from tabulate
invisible_codes = re.compile(r"\x1b\[\d*m")

return re.sub(invisible_codes, "", data)
text = _to_unicode(data)
return re.sub(invisible_codes, "", text)


def underscore(string): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
"""Format string to underlined notation."""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
string = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", string)
string = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", string)
string = string.replace("-", "_")

return string.lower()
def _to_unicode(data: AnyStr) -> str:
"""Ensure data is a UTF-8 string.
Args:
data: A string that can be bytes or str.
def title(word: str) -> str:
"""Format title."""
return " ".join(x.capitalize() or "_" for x in word.split("_"))
Returns:
A UTF-8 string.
"""
if isinstance(data, bytes):
return data.decode("utf-8")
return data
Loading

0 comments on commit 000d200

Please sign in to comment.