1717import json
1818import os
1919import tempfile
20- from collections .abc import Iterator
21- from typing import Any , Callable , Optional , Union
20+ from collections .abc import Callable , Iterator
21+ from typing import Any
2222
2323import testinfra
2424import testinfra .host
@@ -50,7 +50,7 @@ def get_ansible_config() -> configparser.ConfigParser:
5050
5151
5252def get_ansible_inventory (
53- config : configparser .ConfigParser , inventory_file : Optional [ str ]
53+ config : configparser .ConfigParser , inventory_file : str | None
5454) -> Inventory :
5555 # Disable ansible verbosity to avoid
5656 # https://github.com/ansible/ansible/issues/59973
@@ -66,9 +66,9 @@ def get_ansible_host(
6666 config : configparser .ConfigParser ,
6767 inventory : Inventory ,
6868 host : str ,
69- ssh_config : Optional [ str ] = None ,
70- ssh_identity_file : Optional [ str ] = None ,
71- ) -> Optional [ testinfra .host .Host ] :
69+ ssh_config : str | None = None ,
70+ ssh_identity_file : str | None = None ,
71+ ) -> testinfra .host .Host | None :
7272 if is_empty_inventory (inventory ):
7373 if host == "localhost" :
7474 return testinfra .get_host ("local://" )
@@ -139,9 +139,7 @@ def get_ansible_host(
139139 },
140140 }
141141
142- def get_config (
143- name : str , default : Union [None , bool , str ] = None
144- ) -> Union [None , bool , str ]:
142+ def get_config (name : str , default : None | bool | str = None ) -> None | bool | str :
145143 value = default
146144 option = options .get (name , {})
147145
@@ -164,7 +162,7 @@ def get_config(
164162 password = get_config ("ansible_ssh_pass" )
165163 port = get_config ("ansible_port" )
166164
167- kwargs : dict [str , Union [ None , str , bool ] ] = {}
165+ kwargs : dict [str , None | str | bool ] = {}
168166 if get_config ("ansible_become" , False ):
169167 kwargs ["sudo" ] = True
170168 kwargs ["sudo_user" ] = get_config ("ansible_become_user" )
@@ -233,7 +231,7 @@ def is_empty_inventory(inventory: Inventory) -> bool:
233231
234232
235233class AnsibleRunner :
236- _runners : dict [Optional [ str ] , "AnsibleRunner" ] = {}
234+ _runners : dict [str | None , "AnsibleRunner" ] = {}
237235 _known_options = {
238236 # Boolean arguments.
239237 "become" : {
@@ -272,9 +270,9 @@ class AnsibleRunner:
272270 },
273271 }
274272
275- def __init__ (self , inventory_file : Optional [ str ] = None ):
273+ def __init__ (self , inventory_file : str | None = None ):
276274 self .inventory_file = inventory_file
277- self ._host_cache : dict [str , Optional [ testinfra .host .Host ] ] = {}
275+ self ._host_cache : dict [str , testinfra .host .Host | None ] = {}
278276 super ().__init__ ()
279277
280278 def get_hosts (self , pattern : str = "all" ) -> list [str ]:
@@ -327,7 +325,7 @@ def get_variables(self, host: str) -> dict[str, Any]:
327325 hostvars .setdefault ("groups" , groups )
328326 return hostvars
329327
330- def get_host (self , host : str , ** kwargs : Any ) -> Optional [ testinfra .host .Host ] :
328+ def get_host (self , host : str , ** kwargs : Any ) -> testinfra .host .Host | None :
331329 try :
332330 return self ._host_cache [host ]
333331 except KeyError :
@@ -369,8 +367,8 @@ def run_module(
369367 self ,
370368 host : str ,
371369 module_name : str ,
372- module_args : Optional [ str ] ,
373- get_encoding : Optional [ Callable [[], str ]] = None ,
370+ module_args : str | None ,
371+ get_encoding : Callable [[], str ] | None = None ,
374372 ** options : Any ,
375373 ) -> Any :
376374 cmd , args = "ansible --tree %s" , []
@@ -411,7 +409,7 @@ def run_module(
411409 return json .load (f )
412410
413411 @classmethod
414- def get_runner (cls , inventory : Optional [ str ] ) -> "AnsibleRunner" :
412+ def get_runner (cls , inventory : str | None ) -> "AnsibleRunner" :
415413 try :
416414 return cls ._runners [inventory ]
417415 except KeyError :
0 commit comments