Skip to content

Commit 43f7a0c

Browse files
committed
Add some typing to ansible_runner module
This silent errors reported by mypy with --check-untyped-defs
1 parent ee6ca09 commit 43f7a0c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

testinfra/utils/ansible_runner.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import json
1717
import os
1818
import tempfile
19+
from typing import Dict
20+
from typing import List
21+
from typing import Optional
22+
from typing import Union
1923

2024

2125
import testinfra
@@ -87,7 +91,7 @@ def get_ansible_host(config, inventory, host, ssh_config=None, ssh_identity_file
8791
port = config.get("defaults", "remote_port", fallback=None)
8892
if "ansible_port" in hostvars:
8993
port = hostvars.get("ansible_port")
90-
kwargs = {}
94+
kwargs: Dict[str, Union[str, bool]] = {}
9195
if hostvars.get("ansible_become", False):
9296
kwargs["sudo"] = True
9397
kwargs["sudo_user"] = hostvars.get("ansible_become_user")
@@ -143,7 +147,7 @@ def is_empty_inventory(inventory):
143147

144148

145149
class AnsibleRunner:
146-
_runners = {}
150+
_runners: Dict[Optional[str], "AnsibleRunner"] = {}
147151
_known_options = {
148152
# Boolean arguments.
149153
"become": {
@@ -250,8 +254,8 @@ def options_to_cli(self, options):
250254
args = {"become": False, "check": True}
251255
args.update(options)
252256

253-
cli = []
254-
cli_args = []
257+
cli: List[str] = []
258+
cli_args: List[str] = []
255259
if verbose:
256260
cli.append("-" + "v" * verbose)
257261
for arg_name, value in args.items():
@@ -262,6 +266,7 @@ def options_to_cli(self, options):
262266
if value:
263267
cli.append(opt_cli)
264268
elif opt_type == "string":
269+
assert isinstance(value, str)
265270
cli.append(opt_cli + " %s")
266271
cli_args.append(value)
267272
elif opt_type == "json":
@@ -273,7 +278,7 @@ def options_to_cli(self, options):
273278
return " ".join(cli), cli_args
274279

275280
def run_module(self, host, module_name, module_args, **options):
276-
cmd, args = "ansible --tree %s", [None]
281+
cmd, args = "ansible --tree %s", []
277282
if self.inventory_file:
278283
cmd += " -i %s"
279284
args += [self.inventory_file]
@@ -289,7 +294,7 @@ def run_module(self, host, module_name, module_args, **options):
289294
cmd += " %s"
290295
args += [host]
291296
with tempfile.TemporaryDirectory() as d:
292-
args[0] = d
297+
args.insert(0, d)
293298
out = local.run_expect([0, 2, 8], cmd, *args)
294299
files = os.listdir(d)
295300
if not files and "skipped" in out.stdout.lower():

0 commit comments

Comments
 (0)