Skip to content

Commit 2644a9b

Browse files
authored
More mypy fixes (#4428)
1 parent 1b11890 commit 2644a9b

File tree

9 files changed

+29
-23
lines changed

9 files changed

+29
-23
lines changed

examples/playbooks/action_plugins/some_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ansible.plugins.action import ActionBase
44

55

6-
class ActionModule(ActionBase): # type: ignore[misc]
6+
class ActionModule(ActionBase): # type: ignore[misc,no-any-unimported]
77
"""Sample module."""
88

99
def run(self, tmp=None, task_vars=None): # type: ignore[no-untyped-def]

pyproject.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,25 @@ parallel = true
8383
source = ["src"]
8484

8585
[tool.mypy]
86+
check_untyped_defs = true
8687
color_output = true
8788
disallow_any_generics = true
89+
disallow_any_unimported = true
8890
disallow_untyped_calls = true
8991
disallow_untyped_defs = true
9092
error_summary = true
91-
# disallow_any_unimported = True
92-
# warn_redundant_casts = True
93-
# warn_return_any = True
94-
# warn_unused_configs = True
9593
# site-packages is here to help vscode mypy integration getting confused
9694
exclude = "(.config|build|dist|test/local-content|site-packages|~/.pyenv|examples/playbooks/collections|plugins/modules)"
9795
# https://github.com/python/mypy/issues/12664
9896
incremental = false
97+
no_implicit_optional = true
9998
python_version = "3.10"
99+
show_error_code_links = true
100+
show_error_codes = true
100101
strict = true
102+
warn_redundant_casts = true
103+
warn_return_any = true
104+
warn_unused_configs = true
101105

102106
[[tool.mypy.overrides]]
103107
ignore_errors = true

src/ansiblelint/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
_logger = logging.getLogger(__package__)
36-
_CACHED_APP = None
36+
_CACHED_APP: App | None = None
3737

3838

3939
class App:

src/ansiblelint/rules/args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ValidationPassedError(Exception):
7171
"""Exception to be raised when validation passes."""
7272

7373

74-
class CustomAnsibleModule(basic.AnsibleModule): # type: ignore[misc]
74+
class CustomAnsibleModule(basic.AnsibleModule): # type: ignore[misc,no-any-unimported]
7575
"""Mock AnsibleModule class."""
7676

7777
def __init__(self, *args: Any, **kwargs: Any) -> None:
@@ -107,7 +107,7 @@ def matchtask(
107107
if module_name in self.module_aliases:
108108
return []
109109

110-
loaded_module: PluginLoadContext = load_plugin(module_name)
110+
loaded_module: PluginLoadContext = load_plugin(module_name) # type: ignore[no-any-unimported]
111111

112112
# https://github.com/ansible/ansible-lint/issues/3200
113113
# since "ps1" modules cannot be executed on POSIX platforms, we will

src/ansiblelint/skip_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def get_rule_skips_from_line(
9595
return result
9696

9797

98-
def append_skipped_rules(
98+
def append_skipped_rules( # type: ignore[no-any-unimported]
9999
pyyaml_data: AnsibleBaseYAMLObject,
100100
lintable: Lintable,
101101
) -> AnsibleBaseYAMLObject:
@@ -143,7 +143,7 @@ def load_data(file_text: str) -> Any:
143143
return yaml.load_all(file_text)
144144

145145

146-
def _append_skipped_rules(
146+
def _append_skipped_rules( # type: ignore[no-any-unimported]
147147
pyyaml_data: AnsibleBaseYAMLObject,
148148
lintable: Lintable,
149149
) -> AnsibleBaseYAMLObject | None:

src/ansiblelint/testing/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pathlib import Path
1111
from typing import TYPE_CHECKING, Any
1212

13-
from ansiblelint.app import get_app
13+
from ansiblelint.app import App, get_app
1414

1515
if TYPE_CHECKING:
1616
# https://github.com/PyCQA/pylint/issues/3240
@@ -27,7 +27,7 @@
2727
class RunFromText:
2828
"""Use Runner on temp files created from testing text snippets."""
2929

30-
app = None
30+
app: App | None = None
3131

3232
def __init__(self, collection: RulesCollection) -> None:
3333
"""Initialize a RunFromText instance with rules collection."""

src/ansiblelint/utils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
_logger = logging.getLogger(__name__)
9797

9898

99-
def parse_yaml_from_file(filepath: str) -> AnsibleBaseYAMLObject:
99+
def parse_yaml_from_file(filepath: str) -> AnsibleBaseYAMLObject: # type: ignore[no-any-unimported]
100100
"""Extract a decrypted YAML object from file."""
101101
dataloader = DataLoader()
102102
if hasattr(dataloader, "set_vault_secrets"):
@@ -114,7 +114,7 @@ def path_dwim(basedir: str, given: str) -> str:
114114
return str(dataloader.path_dwim(given))
115115

116116

117-
def ansible_templar(basedir: Path, templatevars: Any) -> Templar:
117+
def ansible_templar(basedir: Path, templatevars: Any) -> Templar: # type: ignore[no-any-unimported]
118118
"""Create an Ansible Templar using templatevars."""
119119
# `basedir` is the directory containing the lintable file.
120120
# Therefore, for tasks in a role, `basedir` has the form
@@ -244,7 +244,7 @@ def tokenize(value: str) -> tuple[list[str], dict[str, str]]:
244244
return (args, kwargs)
245245

246246

247-
def playbook_items(pb_data: AnsibleBaseYAMLObject) -> ItemsView: # type: ignore[type-arg]
247+
def playbook_items(pb_data: AnsibleBaseYAMLObject) -> ItemsView: # type: ignore[type-arg,no-any-unimported]
248248
"""Return a list of items from within the playbook."""
249249
if isinstance(pb_data, dict):
250250
return pb_data.items()
@@ -740,7 +740,7 @@ def task_to_str(task: dict[str, Any]) -> str:
740740

741741

742742
# pylint: disable=too-many-nested-blocks
743-
def extract_from_list(
743+
def extract_from_list( # type: ignore[no-any-unimported]
744744
blocks: AnsibleBaseYAMLObject,
745745
candidates: list[str],
746746
*,
@@ -913,15 +913,16 @@ def get_error_line(self, path: list[str | int]) -> int:
913913
return line
914914

915915

916-
def task_in_list(
916+
def task_in_list( # type: ignore[no-any-unimported]
917917
data: AnsibleBaseYAMLObject,
918918
file: Lintable,
919919
kind: str,
920920
position: str = ".",
921921
) -> Iterator[Task]:
922922
"""Get action tasks from block structures."""
923923

924-
def each_entry(data: AnsibleBaseYAMLObject, position: str) -> Iterator[Task]:
924+
def each_entry(data: AnsibleBaseYAMLObject, position: str) -> Iterator[Task]: # type: ignore[no-any-unimported]
925+
925926
if not data or not isinstance(data, Iterable):
926927
return
927928
for entry_index, entry in enumerate(data):
@@ -962,7 +963,7 @@ def each_entry(data: AnsibleBaseYAMLObject, position: str) -> Iterator[Task]:
962963
yield from each_entry(data, position)
963964

964965

965-
def add_action_type(
966+
def add_action_type( # type: ignore[no-any-unimported]
966967
actions: AnsibleBaseYAMLObject, action_type: str
967968
) -> AnsibleSequence:
968969
"""Add action markers to task objects."""
@@ -980,7 +981,7 @@ def add_action_type(
980981

981982

982983
@cache
983-
def parse_yaml_linenumbers(
984+
def parse_yaml_linenumbers( # type: ignore[no-any-unimported]
984985
lintable: Lintable,
985986
) -> AnsibleBaseYAMLObject | None:
986987
"""Parse yaml as ansible.utils.parse_yaml but with linenumbers.
@@ -1001,7 +1002,7 @@ def compose_node(parent: yaml.nodes.Node | None, index: int) -> yaml.nodes.Node:
10011002
return node
10021003

10031004
# signature of AnsibleConstructor.construct_mapping
1004-
def construct_mapping(
1005+
def construct_mapping( # type: ignore[no-any-unimported]
10051006
node: yaml.MappingNode,
10061007
deep: bool = False, # noqa: FBT002
10071008
) -> AnsibleMapping:
@@ -1183,7 +1184,7 @@ def parse_examples_from_plugin(lintable: Lintable) -> tuple[int, str]:
11831184

11841185

11851186
@lru_cache
1186-
def load_plugin(name: str) -> PluginLoadContext:
1187+
def load_plugin(name: str) -> PluginLoadContext: # type: ignore[no-any-unimported]
11871188
"""Return loaded ansible plugin/module."""
11881189
loaded_module = action_loader.find_plugin_with_context(
11891190
name,

src/ansiblelint/yaml_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
_logger = logging.getLogger(__name__)
4646

4747

48-
class CustomYamlLintConfig(YamlLintConfig): # type: ignore[misc]
48+
class CustomYamlLintConfig(YamlLintConfig): # type: ignore[misc,no-any-unimported]
4949
"""Extension of YamlLintConfig."""
5050

5151
def __init__(

test/test_skiputils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def test_playbook_noqa2(default_text_runner: RunFromText) -> None:
187187
),
188188
),
189189
)
190+
# type: ignore[no-any-unimported]
190191
def test_append_skipped_rules(
191192
lintable: Lintable,
192193
yaml: AnsibleBaseYAMLObject,

0 commit comments

Comments
 (0)