Skip to content

Commit 70b18af

Browse files
committed
Refactor types for future ansible-core compatibility
Related: ansible/ansible#4548 Related: https://issues.redhat.com/browse/AAP-41586
1 parent cb6a422 commit 70b18af

24 files changed

+259
-187
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -157,81 +157,41 @@ repos:
157157
rev: v1.15.0
158158
hooks:
159159
- id: mypy
160-
# empty args needed in order to match mypy cli behavior
161-
args: [--strict]
162-
additional_dependencies:
160+
# "." and pass_files are used to make pre-commit mypy behave the same as standalone mypy
161+
args: ["."]
162+
pass_filenames: false
163+
additional_dependencies: &deps
163164
- ansible-compat>=25.1.3
164165
- black>=22.10.0
165166
- cryptography>=39.0.1
166167
- filelock>=3.12.2
167168
- importlib_metadata
168169
- jinja2
169170
- license-expression >= 30.3.0
171+
- pip>=22.3.1
170172
- pytest-mock
171173
- pytest>=7.2.2
172-
- pip>=22.3.1
174+
- pyyaml>=6.0.2 # types-PyYAML is not enough
173175
- ruamel-yaml-clib>=0.2.8
174176
- ruamel-yaml>=0.18.6
175177
- subprocess-tee
176-
- types-PyYAML
177178
- types-jsonschema>=4.20.0.0
179+
- nodejs-wheel-binaries
178180
- types-setuptools
179181
- wcmatch
180-
exclude: >
181-
(?x)^(
182-
.config/.*|
183-
collections/.*|
184-
test/local-content/.*|
185-
plugins/.*
186-
)$
182+
- yamllint>=1.34.0
187183
- repo: https://github.com/RobertCraigie/pyright-python
188-
rev: v1.1.396
184+
rev: v1.1.397
189185
hooks:
190186
- id: pyright
191-
additional_dependencies:
192-
- nodejs-wheel-binaries
193-
- ansible-compat>=25.1.3
194-
- black>=22.10.0
195-
- cryptography>=39.0.1
196-
- filelock>=3.12.2
197-
- importlib_metadata
198-
- jinja2
199-
- license-expression >= 30.3.0
200-
- pip>=22.3.1
201-
- pytest-mock
202-
- pytest>=7.2.2
203-
- ruamel-yaml-clib>=0.2.8
204-
- ruamel-yaml>=0.18.6
205-
- subprocess-tee
206-
- types-PyYAML
207-
- types-jsonschema>=4.20.0.0
208-
- types-setuptools
209-
- wcmatch
210-
- yamllint
187+
additional_dependencies: *deps
211188
- repo: https://github.com/pycqa/pylint
212-
rev: v3.3.5
189+
rev: v3.3.6
213190
hooks:
214191
- id: pylint
215192
args:
216193
- --output-format=colorized
217-
additional_dependencies:
218-
- ansible-compat>=25.1.3
219-
- ansible-core>=2.16.0
220-
- black>=24.10.0
221-
- docutils
222-
- filelock>=3.12.2
223-
- importlib_metadata
224-
- jsonschema>=4.20.0
225-
- license-expression >= 30.3.0
226-
- pytest-mock
227-
- pytest>=7.2.2
228-
- pyyaml
229-
- ruamel-yaml-clib>=0.2.7
230-
- ruamel-yaml>=0.18.2
231-
- setuptools # needed for pkg_resources import
232-
- typing_extensions
233-
- wcmatch
234-
- yamllint
194+
additional_dependencies: *deps
235195
- repo: https://github.com/jendrikseipp/vulture
236196
rev: v2.14
237197
hooks:

examples/playbooks/action_plugins/some_action.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from ansible.plugins.action import ActionBase
44

55

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

99
def run(self, tmp=None, task_vars=None): # type: ignore[no-untyped-def]
1010
"""."""
11-
super().run(tmp, task_vars)
11+
super().run(tmp, task_vars) # type: ignore[no-untyped-call]
1212
ret = {"foo": "bar"}
1313
return {"ansible_facts": ret}

examples/playbooks/command-check-success.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@
6060
- name: Restart something # noqa: fqcn[action-core] no-changed-when
6161
command: do something
6262
- name: Foo # noqa: fqcn[action-core] deprecated-module
63-
include: handlers/included-handlers.yml
63+
include_tasks: handlers/included-handlers.yml
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2-
- hosts: all
2+
- name: Some test fixture
3+
hosts: all
34
tasks:
4-
- import_tasks: tasks/malformed.yml
5+
- name: Some import
6+
ansible.builtin.import_tasks: tasks/malformed.yml

pyproject.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ disallow_untyped_calls = true
8989
disallow_untyped_defs = true
9090
error_summary = true
9191
# site-packages is here to help vscode mypy integration getting confused
92-
exclude = "(.cache|.config|.eggs|.tox|_readthedocs|build|dist|test/local-content|site|site-packages|~/.pyenv|examples|plugins/modules)"
92+
exclude = "(.cache|.config|.eggs|.tox|_readthedocs|build|dist|test/local-content|site|site-packages|~/.pyenv|examples|plugins/modules).*"
9393
# https://github.com/python/mypy/issues/12664
94+
follow_untyped_imports = true
9495
incremental = false
9596
no_implicit_optional = true
9697
python_version = "3.10"
@@ -105,10 +106,15 @@ warn_unused_configs = true
105106
ignore_errors = true
106107
ignore_missing_imports = true
107108
module = [
108-
"ansible.*",
109109
"ansiblelint._version", # generated
110110
"license_expression",
111-
"ruamel.yaml",
111+
"ruamel.yaml"
112+
]
113+
114+
[[tool.mypy.overrides]]
115+
implicit_reexport = true
116+
module = [
117+
"ansible.*",
112118
"yamllint.*"
113119
]
114120

src/ansiblelint/_internal/rules.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ def rule_config(self) -> dict[str, Any]:
187187
@property
188188
def options(self) -> Options | None:
189189
"""Used to access linter configuration."""
190+
if self.unloadable:
191+
# internal rules are not configurable
192+
return None
190193
if self._collection is None:
191194
msg = f"A rule ({self.id}) that is not part of a collection cannot access its configuration."
192195
_logger.warning(msg)

src/ansiblelint/file_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,11 @@ def data(self) -> Any:
436436
from ansiblelint.skip_utils import append_skipped_rules
437437

438438
# pylint: disable=possibly-used-before-assignment
439-
self.state = append_skipped_rules(
440-
self.state,
441-
self,
442-
)
439+
if self.state:
440+
self.state = append_skipped_rules(
441+
self.state,
442+
self,
443+
)
443444
else:
444445
_logger.debug(
445446
"data set to None for %s due to being '%s' (%s) kind.",

src/ansiblelint/rules/args.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ class ValidationPassedError(Exception):
7070
"""Exception to be raised when validation passes."""
7171

7272

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

7676
def __init__(self, *args: Any, **kwargs: Any) -> None:
7777
"""Initialize AnsibleModule mock."""
7878
kwargs["no_log"] = True
79-
super().__init__(*args, **kwargs)
79+
super().__init__(*args, **kwargs) # type: ignore[no-untyped-call]
8080
raise ValidationPassedError
8181

8282

@@ -106,7 +106,7 @@ def matchtask(
106106
if module_name in self.module_aliases:
107107
return []
108108

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

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

src/ansiblelint/rules/deprecated_bare_vars.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
from ansiblelint.text import has_glob, has_jinja, is_fqcn_or_name
3131

3232
if TYPE_CHECKING:
33+
from collections.abc import Mapping
34+
3335
from ansiblelint.file_utils import Lintable
3436
from ansiblelint.utils import Task
3537

@@ -81,7 +83,7 @@ def matchtask(
8183
def _matchvar(
8284
self,
8385
varstring: str,
84-
task: dict[str, Any],
86+
task: Mapping[str, Any],
8587
loop_type: str,
8688
) -> bool | str:
8789
if (

src/ansiblelint/rules/deprecated_module.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
from __future__ import annotations
66

7-
from typing import TYPE_CHECKING, Any
7+
from typing import TYPE_CHECKING
88

99
from ansiblelint.rules import AnsibleLintRule
1010

1111
if TYPE_CHECKING:
1212
from ansiblelint.file_utils import Lintable
13+
from ansiblelint.utils import Task
1314

1415

1516
class DeprecatedModuleRule(AnsibleLintRule):
@@ -69,7 +70,7 @@ class DeprecatedModuleRule(AnsibleLintRule):
6970

7071
def matchtask(
7172
self,
72-
task: dict[str, Any],
73+
task: Task,
7374
file: Lintable | None = None,
7475
) -> bool | str:
7576
module = task["action"]["__ansible_module__"]

0 commit comments

Comments
 (0)