Skip to content

Commit

Permalink
Resolve mypy disallow_any_generics (partial) (#1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Feb 14, 2024
1 parent 417d4bf commit 99063c5
Show file tree
Hide file tree
Showing 65 changed files with 284 additions and 296 deletions.
5 changes: 2 additions & 3 deletions docs/_ext/regenerate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from ansible_navigator.configuration_subsystem import NavigatorConfiguration
from ansible_navigator.configuration_subsystem.definitions import SettingsEntry
from ansible_navigator.utils.functions import oxfordcomma
from ansible_navigator.version import __version__


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -46,7 +45,7 @@
]


def _mk_row(row: tuple) -> list:
def _mk_row(row: tuple[str, ...]) -> list[str]:
"""Convert a row as a markdown definition list entry.
:param row: The row tuple, with name, description and list of options
Expand Down Expand Up @@ -97,7 +96,7 @@ def md_settings_dump() -> str:
return "\n".join(lines)


def _params_row_for_entry(entry: SettingsEntry) -> tuple:
def _params_row_for_entry(entry: SettingsEntry) -> tuple[str, ...]:
# pylint: disable=too-many-branches
"""Create a row entry for one settings parameter.
Expand Down
49 changes: 0 additions & 49 deletions mypy.ini

This file was deleted.

24 changes: 18 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ data_file = ".tox/.coverage"
parallel = true
source_pkgs = ["ansible_navigator"]

[tool.mypy]
python_version = "3.10"
# strict = true
color_output = true
error_summary = true
# disallow_untyped_calls = true
# disallow_untyped_defs = true
# disallow_any_generics = true
incremental = false

[tool.pydoclint]
allow-init-docstring = true
arg-type-hints-in-docstring = false
Expand Down Expand Up @@ -240,7 +250,9 @@ PYTEST_MAX_TEST_ID_LENGTH = 0
fix = true
line-length = 100
builtins = ["__"]
select = ["ALL"]
exclude = ["tests/fixtures/**", ".git"]

[tool.ruff.lint]
ignore = [
'ANN001', # Missing type annotation for function argument `output`
'ANN002', # Missing type annotation for `*args`
Expand Down Expand Up @@ -277,8 +289,8 @@ ignore = [
'D105', # Missing docstring in magic method
'D400', # [*] First line should end with a period
'D403', # [*] First word of the first line should be capitalized: `tokenize` -> `Tokenize`
'E501', # line-too-long, already covered by black
'ERA001', # [*] Found commented-out code
'F401', # [*] `ansible_navigator.version.__version__` imported but unused
'FBT001', # Boolean positional arg in function definition
'FBT002', # Boolean default value in function definition
'FBT003', # Boolean positional value in function call
Expand Down Expand Up @@ -370,18 +382,18 @@ ignore = [
'TRY400', # Use `logging.exception` instead of `logging.error`
'TRY401' # Redundant exception object included in `logging.exception` call
]
exclude = ["tests/fixtures/**", ".git"]
select = ["ALL"]

[tool.ruff.flake8-pytest-style]
[tool.ruff.lint.flake8-pytest-style]
parametrize-values-type = "tuple"

[tool.ruff.isort]
[tool.ruff.lint.isort]
force-single-line = true # Force from .. import to be 1 per line
known-first-party = ["ansible_navigator"]
lines-after-imports = 2 # Ensures consistency for cases when there's variable vs function/class definitions after imports
lines-between-types = 1 # Separate import/from with 1 line

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "pep257"

[tool.setuptools.dynamic]
Expand Down
6 changes: 3 additions & 3 deletions src/ansible_navigator/action_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def __init__(self, args: ApplicationConfiguration, name: str, logger_name: str =
self._calling_app: AppPublic
self._interaction: Interaction
self._name = name
self._previous_filter: Pattern | None
self._previous_filter: Pattern[str] | None
self._previous_scroll: int
self.stdout: list = []
self.stdout: list[str] = []
self.steps = Steps()

@staticmethod
Expand Down Expand Up @@ -168,7 +168,7 @@ def update(self) -> None:

def _update_args(
self,
params: list,
params: list[str],
apply_previous_cli_entries: C = C.ALL,
attach_cdc: bool = False,
) -> bool:
Expand Down
1 change: 0 additions & 1 deletion src/ansible_navigator/action_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

from typing import NamedTuple
from typing import Union


class RunReturn(NamedTuple):
Expand Down
3 changes: 1 addition & 2 deletions src/ansible_navigator/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from collections.abc import Callable
from typing import Any
from typing import Optional

from ansible_navigator.action_defs import RunStdoutReturn
from ansible_navigator.app_public import AppPublic
Expand All @@ -31,7 +30,7 @@

names = actions.names_factory(__package__)

kegexes: Callable = actions.kegexes_factory(__package__)
kegexes: Callable[..., Any] = actions.kegexes_factory(__package__)

run_action_stdout: Callable[
[str, ApplicationConfiguration],
Expand Down
20 changes: 10 additions & 10 deletions src/ansible_navigator/actions/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Kegex = namedtuple("Kegex", ("name", "kegex"))

# Dictionary with information about all registered actions
_ACTIONS: dict[str, dict] = {}
_ACTIONS: dict[str, dict[str, Any]] = {}


def _import(package: str, action: str) -> None:
Expand Down Expand Up @@ -60,7 +60,7 @@ def register(cls: Any) -> Any:
return cls


def get(package: str, action: str) -> Callable:
def get(package: str, action: str) -> Callable[..., Any]:
"""Import and return a given action.
:param package: The name of the package
Expand All @@ -71,7 +71,7 @@ def get(package: str, action: str) -> Callable:
return _ACTIONS[package][action].cls


def get_factory(package: str) -> Callable:
def get_factory(package: str) -> Callable[..., Any]:
"""Create a ``get()`` function for one package.
:param package: The name of the package
Expand All @@ -80,7 +80,7 @@ def get_factory(package: str) -> Callable:
return functools.partial(get, package)


def kegex(package: str, action: str) -> tuple:
def kegex(package: str, action: str) -> tuple[str, str, str]:
"""Return a tuple of name, class, ``kegex`` for an action.
:param package: The name of the package
Expand All @@ -91,7 +91,7 @@ def kegex(package: str, action: str) -> tuple:
return _ACTIONS[package][action]


def kegexes(package: str) -> Generator:
def kegexes(package: str) -> Generator[tuple[str, str, str], None, None]:
"""Return a tuple of tuples, name, ``kegex`` for all actions.
:param package: The name of the package
Expand All @@ -101,7 +101,7 @@ def kegexes(package: str) -> Generator:
return (kegex(package, name) for name in names(package))


def kegexes_factory(package: str) -> Callable:
def kegexes_factory(package: str) -> Callable[..., Any]:
"""Create a ``kegexes()`` function for all packages.
:param package: The name of the package
Expand All @@ -110,7 +110,7 @@ def kegexes_factory(package: str) -> Callable:
return functools.partial(kegexes, package)


def names(package: str) -> list:
def names(package: str) -> list[str]:
"""List all actions in one package.
:param package: The name of the package
Expand All @@ -120,7 +120,7 @@ def names(package: str) -> list:
return sorted(_ACTIONS[package])


def names_factory(package: str) -> Callable:
def names_factory(package: str) -> Callable[..., Any]:
"""Create a ``names()`` function for one package.
:param package: The name of the package
Expand Down Expand Up @@ -163,7 +163,7 @@ def run_interactive(package: str, action: str, *args: Any, **_kwargs: Any) -> An
return None


def run_interactive_factory(package: str) -> Callable:
def run_interactive_factory(package: str) -> Callable[..., Any]:
"""Create a ``run_interactive()`` function for one package.
:param package: The name of the package
Expand All @@ -186,7 +186,7 @@ def run_stdout(package: str, action: str, *args: Any, **_kwargs: Any) -> RunStdo
return action_cls(args).run_stdout()


def run_stdout_factory(package: str) -> Callable:
def run_stdout_factory(package: str) -> Callable[..., Any]:
"""Create a ``run_stdout()`` function for one package.
:param package: The name of the package
Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/actions/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run_stdout(self) -> RunStdoutReturn:
_out, error, return_code = response
return RunStdoutReturn(message=error, return_code=return_code)

def _run_runner(self) -> tuple | None:
def _run_runner(self) -> tuple[str, str, int] | None:
"""Spin up runner.
:raises RuntimeError: When ansible-builder can not be found
Expand Down
18 changes: 10 additions & 8 deletions src/ansible_navigator/actions/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def __init__(self, args: ApplicationConfiguration):
self._adjacent_collection_dir: str
self._collection_cache: KeyValueStore
self._collection_cache_path: str
self._collection_scanned_paths: list = []
self._collections: list = []
self._stats: dict = {}
self._collection_scanned_paths: list[str] = []
self._collections: list[Any] = []
self._stats: dict[str, Any] = {}

def update(self) -> None:
"""Request calling app update, no collection update is required."""
Expand Down Expand Up @@ -596,13 +596,15 @@ def _parse(self, output) -> None:

return None

def _get_collection_plugins_details(self, selected_collection: dict) -> dict:
def _get_collection_plugins_details(
self, selected_collection: dict[str, Any]
) -> dict[str, Any]:
"""Get plugin details for the given collection.
:param selected_collection: The selected collection
:returns: The plugin details like full-name, type and short description.
"""
plugins_details: dict = {}
plugins_details: dict[str, Any] = {}

for plugin_checksum, plugin_info in selected_collection["plugin_checksums"].items():
plugin_type = plugin_info.get("type")
Expand Down Expand Up @@ -639,13 +641,13 @@ def _get_collection_plugins_details(self, selected_collection: dict) -> dict:

return plugins_details

def _parse_collection_info_stdout(self) -> dict:
def _parse_collection_info_stdout(self) -> dict[str, Any]:
# pylint: disable=too-many-nested-blocks
"""Parse collection information from catalog collection cache.
:returns: The collection information to be displayed on stdout
"""
collections_info: dict = {
collections_info: dict[str, Any] = {
"collections": [],
}
collection_exclude_keys = [
Expand All @@ -662,7 +664,7 @@ def _parse_collection_info_stdout(self) -> dict:
for collection in self._collections:
plugins_details = self._get_collection_plugins_details(collection)

collection_stdout: dict = {}
collection_stdout: dict[str, Any] = {}
for info_name, info_value in collection.items():
info_name = remove_dbl_un(info_name)
if info_name in collection_exclude_keys:
Expand Down
11 changes: 8 additions & 3 deletions src/ansible_navigator/actions/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ def run_stdout(self) -> RunStdoutReturn:
"""
self._logger.debug("config requested in stdout mode")
response = self._run_runner()
if response is None:
if (
response is None
or response[0] is None
or response[1] is None
or not isinstance(response[2], int)
):
self._logger.error("Unexpected response: %s", response)
return RunStdoutReturn(message="Please review the log for errors.", return_code=1)
_out, error, return_code = response
Expand Down Expand Up @@ -209,7 +214,7 @@ def _build_option_content(self):
index=self.steps.current.index,
)

def _run_runner(self) -> tuple | None:
def _run_runner(self) -> tuple[str, str, int] | None:
"""Use the runner subsystem to retrieve the configuration.
:raises RuntimeError: When the ansible-config command cannot be found with execution
Expand Down Expand Up @@ -298,7 +303,7 @@ def _run_runner(self) -> tuple | None:
self._runner = Command(executable_cmd=ansible_config_path, **kwargs)
stdout_return = self._runner.run()
return stdout_return
return (None, None, None)
return None

def _parse_and_merge(self, list_output, dump_output) -> None:
"""Parse the list and dump output. Merge dump into list.
Expand Down
4 changes: 2 additions & 2 deletions src/ansible_navigator/actions/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, args: ApplicationConfiguration):
self._plugin_type: str | None = None
self._runner: Command | AnsibleDoc

def generate_content_heading(self, _obj: dict, screen_w: int) -> CursesLines:
def generate_content_heading(self, _obj: dict[Any, Any], screen_w: int) -> CursesLines:
"""Create a heading for doc content.
:param _obj: The content going to be shown
Expand Down Expand Up @@ -133,7 +133,7 @@ def run_stdout(self) -> RunStdoutReturn:
_out, error, return_code = response
return RunStdoutReturn(message=error, return_code=return_code)

def _run_runner(self) -> dict | tuple[str, str, int] | None:
def _run_runner(self) -> dict[Any, Any] | tuple[str, str, int] | None:
# pylint: disable=no-else-return
"""Use the runner subsystem to retrieve the configuration.
Expand Down
Loading

0 comments on commit 99063c5

Please sign in to comment.