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://" )
@@ -140,8 +140,8 @@ def get_ansible_host(
140140 }
141141
142142 def get_config (
143- name : str , default : Union [ None , bool , str ] = None
144- ) -> Union [ None , bool , str ] :
143+ name : str , default : None | bool | str = None
144+ ) -> None | bool | str :
145145 value = default
146146 option = options .get (name , {})
147147
@@ -164,7 +164,7 @@ def get_config(
164164 password = get_config ("ansible_ssh_pass" )
165165 port = get_config ("ansible_port" )
166166
167- kwargs : dict [str , Union [ None , str , bool ] ] = {}
167+ kwargs : dict [str , None | str | bool ] = {}
168168 if get_config ("ansible_become" , False ):
169169 kwargs ["sudo" ] = True
170170 kwargs ["sudo_user" ] = get_config ("ansible_become_user" )
@@ -233,7 +233,7 @@ def is_empty_inventory(inventory: Inventory) -> bool:
233233
234234
235235class AnsibleRunner :
236- _runners : dict [Optional [ str ] , "AnsibleRunner" ] = {}
236+ _runners : dict [str | None , "AnsibleRunner" ] = {}
237237 _known_options = {
238238 # Boolean arguments.
239239 "become" : {
@@ -272,9 +272,9 @@ class AnsibleRunner:
272272 },
273273 }
274274
275- def __init__ (self , inventory_file : Optional [ str ] = None ):
275+ def __init__ (self , inventory_file : str | None = None ):
276276 self .inventory_file = inventory_file
277- self ._host_cache : dict [str , Optional [ testinfra .host .Host ] ] = {}
277+ self ._host_cache : dict [str , testinfra .host .Host | None ] = {}
278278 super ().__init__ ()
279279
280280 def get_hosts (self , pattern : str = "all" ) -> list [str ]:
@@ -327,7 +327,7 @@ def get_variables(self, host: str) -> dict[str, Any]:
327327 hostvars .setdefault ("groups" , groups )
328328 return hostvars
329329
330- def get_host (self , host : str , ** kwargs : Any ) -> Optional [ testinfra .host .Host ] :
330+ def get_host (self , host : str , ** kwargs : Any ) -> testinfra .host .Host | None :
331331 try :
332332 return self ._host_cache [host ]
333333 except KeyError :
@@ -369,8 +369,8 @@ def run_module(
369369 self ,
370370 host : str ,
371371 module_name : str ,
372- module_args : Optional [ str ] ,
373- get_encoding : Optional [ Callable [[], str ]] = None ,
372+ module_args : str | None ,
373+ get_encoding : Callable [[], str ] | None = None ,
374374 ** options : Any ,
375375 ) -> Any :
376376 cmd , args = "ansible --tree %s" , []
@@ -411,7 +411,7 @@ def run_module(
411411 return json .load (f )
412412
413413 @classmethod
414- def get_runner (cls , inventory : Optional [ str ] ) -> "AnsibleRunner" :
414+ def get_runner (cls , inventory : str | None ) -> "AnsibleRunner" :
415415 try :
416416 return cls ._runners [inventory ]
417417 except KeyError :
0 commit comments