Skip to content

Commit 04cccc3

Browse files
shenxianpengphilpep
authored andcommitted
chore: bump ruff target-version to py310 and apply pyupgrade fixes
- ruff.toml: target-version py39 -> py310 - Modernize type annotations: Optional[X] -> X | None, Union[X, Y] -> X | Y - Remove unused typing imports (Optional, Union) - Migrate typing.Callable -> collections.abc.Callable
1 parent 752d282 commit 04cccc3

9 files changed

Lines changed: 48 additions & 51 deletions

File tree

ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
target-version = "py39"
1+
target-version = "py310"
22

33
[lint]
44
select = [

testinfra/backend/ansible.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import json
1414
import logging
1515
import pprint
16-
from typing import Any, Optional
16+
from typing import Any
1717

1818
from testinfra.backend import base
1919
from testinfra.utils.ansible_runner import AnsibleRunner
@@ -28,9 +28,9 @@ class AnsibleBackend(base.BaseBackend):
2828
def __init__(
2929
self,
3030
host: str,
31-
ansible_inventory: Optional[str] = None,
32-
ssh_config: Optional[str] = None,
33-
ssh_identity_file: Optional[str] = None,
31+
ansible_inventory: str | None = None,
32+
ssh_config: str | None = None,
33+
ssh_identity_file: str | None = None,
3434
force_ansible: bool = False,
3535
*args: Any,
3636
**kwargs: Any,
@@ -73,7 +73,7 @@ def run(self, command: str, *args: str, **kwargs: Any) -> base.CommandResult:
7373
return self.result(rc, self.encode(command), stdout, stderr)
7474

7575
def run_ansible(
76-
self, module_name: str, module_args: Optional[str] = None, **kwargs: Any
76+
self, module_name: str, module_args: str | None = None, **kwargs: Any
7777
) -> Any:
7878
def get_encoding() -> str:
7979
return self.encoding

testinfra/backend/base.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import shlex
1818
import subprocess
1919
import urllib.parse
20-
from typing import TYPE_CHECKING, Any, Optional, Union
20+
from typing import TYPE_CHECKING, Any
2121

2222
if TYPE_CHECKING:
2323
import testinfra.host
@@ -28,9 +28,9 @@
2828
@dataclasses.dataclass
2929
class HostSpec:
3030
name: str
31-
port: Optional[str]
32-
user: Optional[str]
33-
password: Optional[str]
31+
port: str | None
32+
user: str | None
33+
password: str | None
3434

3535

3636
@dataclasses.dataclass
@@ -55,8 +55,8 @@ class CommandResult:
5555
backend: "BaseBackend"
5656
exit_status: int
5757
command: bytes
58-
_stdout: Union[str, bytes]
59-
_stderr: Union[str, bytes]
58+
_stdout: str | bytes
59+
_stderr: str | bytes
6060

6161
@property
6262
def succeeded(self) -> bool:
@@ -141,12 +141,12 @@ def __init__(
141141
self,
142142
hostname: str,
143143
sudo: bool = False,
144-
sudo_user: Optional[str] = None,
144+
sudo_user: str | None = None,
145145
*args: Any,
146146
**kwargs: Any,
147147
):
148-
self._encoding: Optional[str] = None
149-
self._host: Optional[testinfra.host.Host] = None
148+
self._encoding: str | None = None
149+
self._host: testinfra.host.Host | None = None
150150
self.hostname = hostname
151151
self.sudo = sudo
152152
self.sudo_user = sudo_user
@@ -207,7 +207,7 @@ def quote(command: str, *args: str) -> str:
207207
return command % tuple(shlex.quote(a) for a in args)
208208
return command
209209

210-
def get_sudo_command(self, command: str, sudo_user: Optional[str]) -> str:
210+
def get_sudo_command(self, command: str, sudo_user: str | None) -> str:
211211
if sudo_user is None:
212212
return self.quote("sudo /bin/sh -c %s", command)
213213
return self.quote("sudo -u %s /bin/sh -c %s", sudo_user, command)
@@ -265,7 +265,7 @@ def parse_hostspec(hostspec: str) -> HostSpec:
265265
return HostSpec(name, port, user, password)
266266

267267
@staticmethod
268-
def parse_containerspec(containerspec: str) -> tuple[str, Optional[str]]:
268+
def parse_containerspec(containerspec: str) -> tuple[str, str | None]:
269269
name = containerspec
270270
user = None
271271
if "@" in name:
@@ -311,7 +311,7 @@ def encode(self, data: str) -> bytes:
311311
return data.encode(self.encoding)
312312

313313
def result(
314-
self, rc: int, cmd: bytes, stdout: Union[str, bytes], stderr: Union[str, bytes]
314+
self, rc: int, cmd: bytes, stdout: str | bytes, stderr: str | bytes
315315
) -> CommandResult:
316316
result = CommandResult(
317317
backend=self,

testinfra/backend/paramiko.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
) from None
2222

2323
import functools
24-
from typing import Any, Optional
24+
from typing import Any
2525

2626
import paramiko.pkey
2727
import paramiko.ssh_exception
@@ -44,8 +44,8 @@ class ParamikoBackend(base.BaseBackend):
4444
def __init__(
4545
self,
4646
hostspec: str,
47-
ssh_config: Optional[str] = None,
48-
ssh_identity_file: Optional[str] = None,
47+
ssh_config: str | None = None,
48+
ssh_identity_file: str | None = None,
4949
timeout: int = 10,
5050
*args: Any,
5151
**kwargs: Any,

testinfra/backend/salt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"You must install salt package to use the salt backend"
1818
) from None
1919

20-
from typing import Any, Optional
20+
from typing import Any
2121

2222
from testinfra.backend import base
2323

@@ -28,7 +28,7 @@ class SaltBackend(base.BaseBackend):
2828

2929
def __init__(self, host: str, *args: Any, **kwargs: Any):
3030
self.host = host
31-
self._client: Optional[salt.client.LocalClient] = None
31+
self._client: salt.client.LocalClient | None = None
3232
super().__init__(self.host, *args, **kwargs)
3333

3434
@property

testinfra/backend/ssh.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# limitations under the License.
1212

1313
import base64
14-
from typing import Any, Optional
14+
from typing import Any
1515

1616
from testinfra.backend import base
1717

@@ -24,12 +24,12 @@ class SshBackend(base.BaseBackend):
2424
def __init__(
2525
self,
2626
hostspec: str,
27-
ssh_config: Optional[str] = None,
28-
ssh_identity_file: Optional[str] = None,
27+
ssh_config: str | None = None,
28+
ssh_identity_file: str | None = None,
2929
timeout: int = 10,
30-
controlpath: Optional[str] = None,
30+
controlpath: str | None = None,
3131
controlpersist: int = 60,
32-
ssh_extra_args: Optional[str] = None,
32+
ssh_extra_args: str | None = None,
3333
*args: Any,
3434
**kwargs: Any,
3535
):

testinfra/backend/winrm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# limitations under the License.
1212

1313
import re
14-
from typing import Any, Optional
14+
from typing import Any
1515

1616
from testinfra.backend import base
1717

@@ -52,8 +52,8 @@ def __init__(
5252
hostspec: str,
5353
no_ssl: bool = False,
5454
no_verify_ssl: bool = False,
55-
read_timeout_sec: Optional[int] = None,
56-
operation_timeout_sec: Optional[int] = None,
55+
read_timeout_sec: int | None = None,
56+
operation_timeout_sec: int | None = None,
5757
*args: Any,
5858
**kwargs: Any,
5959
):

testinfra/modules/socket.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import functools
1414
import socket
15-
from typing import Optional
1615

1716
from testinfra.modules.base import Module
1817

@@ -121,7 +120,7 @@ def is_listening(self):
121120
)
122121

123122
@property
124-
def clients(self) -> list[Optional[tuple[str, int]]]:
123+
def clients(self) -> list[tuple[str, int] | None]:
125124
"""Return a list of clients connected to a listening socket
126125
127126
For tcp and udp sockets a list of pair (address, port) is returned.
@@ -134,7 +133,7 @@ def clients(self) -> list[Optional[tuple[str, int]]]:
134133
[None, None, None]
135134
136135
"""
137-
sockets: list[Optional[tuple[str, int]]] = []
136+
sockets: list[tuple[str, int] | None] = []
138137
for sock in self._iter_sockets(False):
139138
if sock[0] != self.protocol:
140139
continue

testinfra/utils/ansible_runner.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import json
1818
import os
1919
import 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

2323
import testinfra
2424
import testinfra.host
@@ -50,7 +50,7 @@ def get_ansible_config() -> configparser.ConfigParser:
5050

5151

5252
def 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

235233
class 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

Comments
 (0)