Skip to content

Commit

Permalink
More types (#1691)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Feb 17, 2024
1 parent ba22124 commit 10e51fe
Show file tree
Hide file tree
Showing 40 changed files with 263 additions and 207 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ python_version = "3.10"
# strict = true
color_output = true
error_summary = true
# check_untyped_defs = true
# disallow_untyped_calls = true
# disallow_untyped_defs = true
disallow_any_generics = true
Expand Down
4 changes: 2 additions & 2 deletions src/ansible_navigator/actions/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def names_factory(package: str) -> Callable[..., Any]:
return functools.partial(names, package)


def run_interactive(package: str, action: str, *args: Any, **_kwargs: Any) -> Any:
def run_interactive(package: str, action: str, *args: Any, **_kwargs: dict[str, Any]) -> Any:
"""Call the given action's ``run()`` method.
:param package: The name of the package
Expand Down Expand Up @@ -172,7 +172,7 @@ def run_interactive_factory(package: str) -> Callable[..., Any]:
return functools.partial(run_interactive, package)


def run_stdout(package: str, action: str, *args: Any, **_kwargs: Any) -> RunStdoutReturn:
def run_stdout(package: str, action: str, *args: Any, **_kwargs: dict[str, Any]) -> RunStdoutReturn:
"""Call the given action's ``run_stdout()`` method.
:param package: The name of the package
Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/actions/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _set_inventories_mtime(self) -> None:
else:
self._inventories_mtime = None

def update(self):
def update(self) -> None:
"""Request calling app update, inventory update checked in ``run()``."""
self._calling_app.update()

Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/actions/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def __init__(self, args: ApplicationConfiguration):
"""Task name storage from playbook_on_start using the task uuid as the key"""

@property
def mode(self):
def mode(self) -> str:
"""Determine the mode and if playbook artifact creation is enabled.
If so, run in interactive mode, but print stdout.
Expand Down
10 changes: 6 additions & 4 deletions src/ansible_navigator/command_runner/command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ def worker(
class CommandRunner:
"""Functionality for running commands."""

def __init__(self):
def __init__(self) -> None:
"""Initialize the command runner."""
self._completed_queue: Queue | None = None
self._pending_queue: Queue | None = None
self._completed_queue: Queue[Any] | None = None
self._pending_queue: Queue[Command | None] | None = None

@staticmethod
def run_single_process(commands: list[Command]):
Expand Down Expand Up @@ -134,7 +134,7 @@ def run_multi_process(self, commands: list[Command]) -> list[Command]:
results.append(self._completed_queue.get())
return results

def start_workers(self, jobs):
def start_workers(self, jobs: list[Command]) -> None:
"""Start the workers.
:param jobs: List of commands to be run
Expand All @@ -148,6 +148,8 @@ def start_workers(self, jobs):
)
processes.append(proc)
proc.start()
if not self._pending_queue:
raise RuntimeError
for job in jobs:
self._pending_queue.put(job)
for _proc in range(worker_count):
Expand Down
18 changes: 12 additions & 6 deletions src/ansible_navigator/data/image_introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ class CommandRunner:
Run commands using single or multiple processes.
"""

def __init__(self):
def __init__(self) -> None:
"""Initialize the command runner."""
self._completed_queue: Queue[Any] | None = None
self._pending_queue: Queue[Any] | None = None

def run_multi_thread(self, command_classes):
def run_multi_thread(self, command_classes: list[CmdParser]) -> list[CmdParser]:
"""Run commands with multiple threads.
Workers are started to read from pending queue.
Expand All @@ -94,17 +94,16 @@ def run_multi_thread(self, command_classes):
self._completed_queue = Queue()
if self._pending_queue is None:
self._pending_queue = Queue()
results = {}
all_commands = tuple(
command for command_class in command_classes for command in command_class.commands
)
self.start_workers(all_commands)
results = []
results: list[CmdParser] = []
while len(results) != len(all_commands):
results.append(self._completed_queue.get())
return results

def start_workers(self, jobs):
def start_workers(self, jobs: tuple[Command, ...]) -> None:
"""Start workers and submit jobs to pending queue.
:param jobs: The jobs to be run
Expand All @@ -118,6 +117,8 @@ def start_workers(self, jobs):
)
processes.append(proc)
proc.start()
if not self._pending_queue:
raise RuntimeError
for job in jobs:
self._pending_queue.put(job)
for _proc in range(worker_count):
Expand All @@ -129,6 +130,11 @@ def start_workers(self, jobs):
class CmdParser:
"""A base class for command parsers with common parsing functions."""

@property
def commands(self) -> list[Command]:
"""List of commands to be executed."""
return []

@staticmethod
def _strip(value: str) -> str:
"""Remove quotes, leading and trailing whitespace.
Expand Down Expand Up @@ -387,7 +393,7 @@ def main(serialize: bool = True) -> dict[str, JSONTypes] | None:
response["environment_variables"] = {"details": dict(os.environ)}
try:
command_runner = CommandRunner()
commands = [
commands: list[CmdParser] = [
AnsibleCollections(),
AnsibleVersion(),
OsRelease(),
Expand Down
14 changes: 7 additions & 7 deletions src/ansible_navigator/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Collector:

name: str

def start(self, color: bool):
def start(self, color: bool) -> None:
"""Output start information to the console.
:param color: Whether to color the message
Expand Down Expand Up @@ -94,20 +94,20 @@ class Diagnostics:
settings_file: dict[str, JSONTypes]


def register(collector: Collector):
def register(collector: Collector) -> Callable[..., Any]:
"""Register a collector.
:param collector: The collector to register
:returns: The decorator
"""

def decorator(func):
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
"""Add the dunder collector to the func.
:param func: The function to decorate
:returns: The decorated function
"""
func.__collector__ = collector
func.__collector__ = collector # type: ignore[attr-defined]
return func

return decorator
Expand All @@ -128,14 +128,14 @@ def __init__(self, errors: dict[str, JSONTypes]) -> None:
self.errors = errors


def diagnostic_runner(func) -> Callable[..., Any]:
def diagnostic_runner(func: Callable[..., Any]) -> Callable[..., Any]:
"""Wrap and run a collector.
:param func: The function to wrap
:returns: The decorator
"""

def wrapper(*args, **kwargs):
def wrapper(*args: Any, **kwargs: dict[str, Any]) -> Callable[..., Any]:
"""Wrap and run the collector.
:param args: The positional arguments
Expand All @@ -145,7 +145,7 @@ def wrapper(*args, **kwargs):
global DIAGNOSTIC_FAILURES
start = datetime.now(timezone.utc)
color = args[0].color
collector = func.__collector__
collector = func.__collector__ # type: ignore[attr-defined]
collector.start(color=color)
try:
result = func(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/ui_framework/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class FormPresenter(CursesWindow):
"""Present the form to the user."""

# pylint: disable=too-many-instance-attributes
def __init__(self, form, screen, ui_config):
def __init__(self, form, screen, ui_config) -> None:
"""Initialize the form presenter.
:param form: The form to present to the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SettingsFile(Migration):

name = "Settings file migration base class"

def __init__(self):
def __init__(self) -> None:
"""Initialize the settings file migration."""
super().__init__()
self.content: dict[Any, Any] = {}
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def pytest_sessionstart(session: pytest.Session):
USER_ENVIRONMENT = {}


def pytest_configure(config: pytest.Config):
def pytest_configure(config: pytest.Config) -> None:
"""Attempt to save a contributor some troubleshooting.
:param config: The pytest config object
Expand Down Expand Up @@ -359,7 +359,7 @@ def pytest_configure(config: pytest.Config):
pytest.exit("Please install tmux before testing.")


def pytest_unconfigure(config: pytest.Config):
def pytest_unconfigure(config: pytest.Config) -> None:
"""Restore the environment variables that start with ANSIBLE_.
:param config: The pytest config object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""An ansible test filter plugin."""

from typing import Any

def filter_1():

def filter_1() -> None:
"""Convert strings to Candlepin labels."""
return

Expand All @@ -10,7 +12,7 @@ def filter_1():
class FilterModule:
"""Coll_1 filter."""

def filters(self):
def filters(self) -> dict[str, Any]:
"""Convert an arbitrary string to a valid Candlepin label.
:returns: converted Candlepin label
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/builder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def fixture_tmux_session(self, request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
"""Run the tests for ``builder``, mode and ``ee`` set in child class.
:param request: A fixture providing details about the test caller
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def fixture_tmux_session(self, request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
# pylint: disable=too-many-locals
"""Run the tests for ``config``, mode and ``ee`` set in child class.
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/actions/doc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def fixture_tmux_doc_session(request):

def test(
self,
request,
tmux_doc_session,
index,
user_input,
comment,
testname,
expected_in_output,
request: pytest.FixtureRequest,
tmux_doc_session: TmuxSession,
index: int,
user_input: str,
comment: str,
testname: str,
expected_in_output: list[str] | None,
):
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/images/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def fixture_tmux_session(request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
"""Run the tests for images, mode and ``ee`` set in child class.
:param request: A fixture providing details about the test caller
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/inventory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def fixture_tmux_session(request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
"""Run the tests for inventory, mode and ``ee`` set in child class.
:param request: A fixture providing details about the test caller
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/actions/replay/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ def fixture_tmux_session(request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, index, user_input, comment, search_within_response):
def test(
self,
request: pytest.FixtureRequest,
tmux_session: TmuxSession,
index,
user_input,
comment,
search_within_response,
):
# pylint: disable=too-many-arguments
"""Run the tests for replay, mode and ``ee`` set in child class.
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/run/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def fixture_tmux_session(request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
"""Run the tests for run, mode and ``ee`` set in child class.
:param request: A fixture providing details about the test caller
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def fixture_tmux_session(self, request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
# pylint: disable=too-many-locals
"""Run the tests for ``settings``, mode and ``ee`` set in child class.
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/actions/stdout/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ def fixture_tmux_session(request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, index, user_input, comment, search_within_response):
def test(
self,
request: pytest.FixtureRequest,
tmux_session: TmuxSession,
index,
user_input,
comment,
search_within_response,
):
# pylint:disable=too-many-arguments
"""Run the tests for stdout, mode and EE set in child class.
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/templar/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def fixture_tmux_session(request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step):
"""Test interactive and ``stdout`` mode ``config``.
:param request: A fixture providing details about the test caller
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_execution_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def run_test(
cli_entry: str,
config_fixture: str,
expected: dict[str, str],
):
) -> None:
# pylint: disable=too-many-arguments
"""Confirm execution of ``cli.main()`` produces the desired results.
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_execution_environment_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def run_test(
cli_entry: str,
config_fixture: str,
expected: dict[str, str],
):
) -> None:
# pylint: disable=too-many-arguments
"""Confirm execution of ``cli.main()`` produces the desired results.
Expand Down
Loading

0 comments on commit 10e51fe

Please sign in to comment.