Skip to content

Commit 17ec11e

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 17ec11e

27 files changed

+266
-194
lines changed

.github/workflows/tox.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ jobs:
106106

107107
- name: Set ansible cache(s)
108108
uses: actions/cache@v4
109-
if: ${{ matrix.name != 'pkg' && matrix.name != 'schemas' }}
110109
with:
111110
path: |
112111
.cache/eco
113112
examples/playbooks/collections/ansible_collections
114113
~/.cache/ansible-compat
115114
~/.ansible/collections
116115
~/.ansible/roles
117-
key: ${{ matrix.name }}-${{ hashFiles('tools/test-eco.sh', 'requirements.yml', 'examples/playbooks/collections/requirements.yml') }}
116+
.tox
117+
key: ${{ matrix.name }}-${{ matrix.os }}--${{ hashFiles('tools/test-eco.sh', 'requirements.yml', 'examples/playbooks/collections/requirements.yml') }}
118118

119119
- name: Set up Python ${{ matrix.python_version || '3.10' }}
120120
if: "!contains(matrix.shell, 'wsl')"

.pre-commit-config.yaml

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -157,81 +157,40 @@ 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
178179
- types-setuptools
179180
- wcmatch
180-
exclude: >
181-
(?x)^(
182-
.config/.*|
183-
collections/.*|
184-
test/local-content/.*|
185-
plugins/.*
186-
)$
181+
- yamllint>=1.34.0
187182
- repo: https://github.com/RobertCraigie/pyright-python
188-
rev: v1.1.396
183+
rev: v1.1.397
189184
hooks:
190185
- 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
186+
additional_dependencies: *deps
211187
- repo: https://github.com/pycqa/pylint
212-
rev: v3.3.5
188+
rev: v3.3.6
213189
hooks:
214190
- id: pylint
215191
args:
216192
- --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
193+
additional_dependencies: *deps
235194
- repo: https://github.com/jendrikseipp/vulture
236195
rev: v2.14
237196
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: 12 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

@@ -364,7 +370,10 @@ exclude = [
364370
".ansible",
365371
".cache",
366372
".config",
373+
".direnv",
374+
".eggs",
367375
".tox",
376+
".venv",
368377
"ansible_collections",
369378
"build",
370379
"dist",

requirements.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ collections:
44
- ansible.posix
55
- ansible.windows
66
- community.crypto
7-
- community.docker
7+
- name: community.docker
8+
version: ">=4.5.1"
89
- community.general
910
- community.molecule
1011
- community.windows

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

0 commit comments

Comments
 (0)