Skip to content

Commit 50f7a4e

Browse files
committed
Use ruff instead of flake8
And fix some error along the way
1 parent e37c8c9 commit 50f7a4e

25 files changed

+161
-161
lines changed

.flake8

Lines changed: 0 additions & 10 deletions
This file was deleted.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dist
1414
AUTHORS
1515
ChangeLog
1616
coverage.xml
17-
flake8.report
1817
junit*.xml
1918
doc/build
2019
.cache

.pre-commit-config.yaml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ repos:
2525
- id: check-merge-conflict
2626
- id: debug-statements
2727
language_version: python3
28+
- repo: local
29+
hooks:
30+
- id: ruff-check
31+
name: ruff-check
32+
entry: ruff check
33+
language: system
34+
types: [python]
2835
- repo: https://github.com/PyCQA/isort
2936
rev: 5.12.0
3037
hooks:
@@ -37,18 +44,6 @@ repos:
3744
hooks:
3845
- id: black
3946
language_version: python3
40-
- repo: https://github.com/pycqa/flake8.git
41-
rev: 6.0.0
42-
hooks:
43-
- id: flake8
44-
language_version: python3
45-
additional_dependencies:
46-
- flake8-bugbear
47-
- flake8-comprehensions
48-
- flake8-debugger
49-
- flake8-logging-format
50-
- flake8-pep3101
51-
- flake8-print
5247
- repo: https://github.com/pre-commit/mirrors-mypy
5348
rev: v1.4.1
5449
hooks:

CONTRIBUTING.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ To run only some selected tests::
3030
Code style
3131
==========
3232

33-
Your code must pass without errors under `flake8
34-
<https://flake8.readthedocs.io>`_ with the extension `hacking
35-
<https://docs.openstack.org/hacking/latest/>`_::
33+
Your code must pass without errors under `ruff <https://docs.astral.sh/ruff/>`_
3634

37-
38-
pip install hacking
39-
flake8 testinfra
35+
pip install ruff
36+
ruff check

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ recursive-include doc *.py *.rst *.svg
55
include doc/source/_templates/piwik.html
66
include doc/Makefile
77
include tox.ini
8-
include .flake8
98
include mypy.ini
109
include Makefile
1110
include *.yaml
@@ -15,6 +14,7 @@ include ansible.cfg
1514
include dev-requirements.txt
1615
include test-requirements.txt
1716
include LICENSE
17+
include *.toml
1818
exclude .editorconfig
1919
exclude .gitignore
2020
prune doc/build

ruff.toml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
target-version = "py39"
2+
3+
[lint]
4+
select = [
5+
# pycodestyle
6+
"E",
7+
# Pyflakes
8+
"F",
9+
# pyupgrade
10+
"UP",
11+
# flake8-bugbear
12+
"B",
13+
# flake8-debugger
14+
"T10",
15+
# flake8-logging
16+
"G",
17+
# flake8-comprehension
18+
"C4",
19+
# flake8-simplify
20+
"SIM",
21+
# flake8-print
22+
"T20",
23+
# individual rules
24+
"RUF100", # unused-noqa
25+
# imports
26+
"I",
27+
]
28+
29+
# For error codes, see https://docs.astral.sh/ruff/rules/#error-e
30+
ignore = [
31+
# argument is shadowing a Python builtin
32+
"A001",
33+
"A002",
34+
"A005",
35+
# attribute overriding builtin name
36+
"A003",
37+
# [bugbear] Do not use mutable data structures for argument defaults (I choose by myself)
38+
"B006",
39+
# [bugbear] Do not perform function calls in argument defaults (conflict with fastapi dependency system)
40+
"B008",
41+
# [bugbear] Function definition does not bind loop variable
42+
"B023",
43+
# closing bracket does not match indentation of opening bracket's line (disagree with emacs python mode)
44+
# "E123",
45+
# continuation line over-indented for hanging indent line (disagree with emacs python mode)
46+
# "E126",
47+
# whitespace before ':'
48+
"E203",
49+
# missing whitespace around arithmetic operator (disagree when in parameter)
50+
"E226",
51+
# line too long
52+
"E501",
53+
# multiple statements on one line (def) (not compatible with black)
54+
# "E704",
55+
# do not assign lambda expression (this is inconvenient)
56+
"E731",
57+
# Logging statement uses exception in arguments (seems legit to display only str representation of exc)
58+
# "G200",
59+
# [string-format] format string does contain unindexed parameters (don't care of py2.6)
60+
# "P101",
61+
# [string-format] docstring does contain unindexed parameters
62+
# "P102",
63+
# [string-format] other string does contain unindexed parameters
64+
# "P103",
65+
# Line break occurred before a binary operator (to keep operators aligned)
66+
# "W503",
67+
]
68+
69+
[lint.isort]
70+
known-first-party = ["testinfra"]
71+
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]

test/conftest.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ def setup_ansible_config(tmpdir, name, host, user, port, key):
7878
vault_password_file.write("polichinelle\n")
7979
ansible_cfg = tmpdir.join("ansible.cfg")
8080
ansible_cfg.write(
81-
82-
"[defaults]\n"
83-
f"vault_password_file={str(vault_password_file)}\n"
84-
"host_key_checking=False\n\n"
85-
"[ssh_connection]\n"
86-
"pipelining=True\n"
87-
81+
"[defaults]\n"
82+
f"vault_password_file={str(vault_password_file)}\n"
83+
"host_key_checking=False\n\n"
84+
"[ssh_connection]\n"
85+
"pipelining=True\n"
8886
)
8987

9088

@@ -156,7 +154,8 @@ def host(request, tmpdir_factory):
156154
hostname = spec.name
157155
tmpdir = tmpdir_factory.mktemp(str(id(request)))
158156
key = tmpdir.join("ssh_key")
159-
key.write(open(os.path.join(BASETESTDIR, "ssh_key")).read())
157+
with open(os.path.join(BASETESTDIR, "ssh_key")) as f:
158+
key.write(f.read())
160159
key.chmod(384) # octal 600
161160
if kw["connection"] == "ansible":
162161
setup_ansible_config(
@@ -168,26 +167,21 @@ def host(request, tmpdir_factory):
168167
else:
169168
ssh_config = tmpdir.join("ssh_config")
170169
ssh_config.write(
171-
172-
f"Host {hostname}\n"
173-
f" Hostname {docker_host}\n"
174-
f" Port {port}\n"
175-
" UserKnownHostsFile /dev/null\n"
176-
" StrictHostKeyChecking no\n"
177-
f" IdentityFile {str(key)}\n"
178-
" IdentitiesOnly yes\n"
179-
" LogLevel FATAL\n"
180-
170+
f"Host {hostname}\n"
171+
f" Hostname {docker_host}\n"
172+
f" Port {port}\n"
173+
" UserKnownHostsFile /dev/null\n"
174+
" StrictHostKeyChecking no\n"
175+
f" IdentityFile {str(key)}\n"
176+
" IdentitiesOnly yes\n"
177+
" LogLevel FATAL\n"
181178
)
182179
kw["ssh_config"] = str(ssh_config)
183180

184181
# Wait ssh to be up
185182
service = testinfra.get_host(docker_id, connection="docker").service
186183

187-
if image == "rockylinux9":
188-
service_name = "sshd"
189-
else:
190-
service_name = "ssh"
184+
service_name = "sshd" if image == "rockylinux9" else "ssh"
191185

192186
while not service(service_name).is_running:
193187
time.sleep(0.5)

test/test_backends.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,16 @@ def test_sudo(host):
125125
def test_ansible_get_hosts():
126126
with tempfile.NamedTemporaryFile() as f:
127127
f.write(
128-
129-
b"ungrp\n"
130-
b"[g1]\n"
131-
b"debian\n"
132-
b"[g2]\n"
133-
b"rockylinux\n"
134-
b"[g3:children]\n"
135-
b"g1\n"
136-
b"g2\n"
137-
b"[g4:children]\n"
138-
b"g3"
139-
128+
b"ungrp\n"
129+
b"[g1]\n"
130+
b"debian\n"
131+
b"[g2]\n"
132+
b"rockylinux\n"
133+
b"[g3:children]\n"
134+
b"g1\n"
135+
b"g2\n"
136+
b"[g4:children]\n"
137+
b"g3"
140138
)
141139
f.flush()
142140

@@ -158,16 +156,14 @@ def get_hosts(spec):
158156
def test_ansible_get_variables():
159157
with tempfile.NamedTemporaryFile() as f:
160158
f.write(
161-
162-
b"debian a=b c=d\n"
163-
b"rockylinux e=f\n"
164-
b"[all:vars]\n"
165-
b"a=a\n"
166-
b"[g]\n"
167-
b"debian\n"
168-
b"[g:vars]\n"
169-
b"x=z\n"
170-
159+
b"debian a=b c=d\n"
160+
b"rockylinux e=f\n"
161+
b"[all:vars]\n"
162+
b"a=a\n"
163+
b"[g]\n"
164+
b"debian\n"
165+
b"[g:vars]\n"
166+
b"x=z\n"
171167
)
172168
f.flush()
173169

test/test_modules.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_package(host, docker_image):
5050
}[docker_image]
5151
if release is None:
5252
with pytest.raises(NotImplementedError):
53-
ssh.release
53+
ssh.release # noqa: B018
5454
else:
5555
assert release in ssh.release
5656

@@ -66,7 +66,7 @@ def test_held_package(host):
6666
def test_rpmdb_corrupted(host):
6767
host.check_output("dd if=/dev/zero of=/var/lib/rpm/rpmdb.sqlite bs=1024 count=1")
6868
with pytest.raises(RuntimeError) as excinfo:
69-
host.package("zsh").is_installed
69+
host.package("zsh").is_installed # noqa: B018
7070
assert (
7171
"Could not check if RPM package 'zsh' is installed. error: sqlite failure:"
7272
) in str(excinfo.value)
@@ -82,7 +82,7 @@ def test_non_default_package_tool(host):
8282
@pytest.mark.destructive
8383
def test_uninstalled_package_version(host):
8484
with pytest.raises(AssertionError) as excinfo:
85-
host.package("zsh").version
85+
host.package("zsh").version # noqa: B018
8686
assert (
8787
"The package zsh is not installed, dpkg-query output: unknown ok not-installed"
8888
in str(excinfo.value)
@@ -91,7 +91,7 @@ def test_uninstalled_package_version(host):
9191
host.check_output("apt-get -y remove sudo")
9292
assert not host.package("sudo").is_installed
9393
with pytest.raises(AssertionError) as excinfo:
94-
host.package("sudo").version
94+
host.package("sudo").version # noqa: B018
9595
assert (
9696
"The package sudo is not installed, dpkg-query output: "
9797
"deinstall ok config-files 1.9."
@@ -114,11 +114,7 @@ def test_systeminfo(host, docker_image):
114114

115115
@all_images
116116
def test_ssh_service(host, docker_image):
117-
if docker_image == "rockylinux9":
118-
name = "sshd"
119-
else:
120-
name = "ssh"
121-
117+
name = "sshd" if docker_image == "rockylinux9" else "ssh"
122118
ssh = host.service(name)
123119
# wait at max 10 seconds for ssh is running
124120
for _ in range(10):
@@ -196,7 +192,7 @@ def test_socket(host):
196192

197193
assert not host.socket("tcp://4242").is_listening
198194

199-
if not host.backend.get_connection_type() == "docker":
195+
if host.backend.get_connection_type() != "docker":
200196
# FIXME
201197
for spec in (
202198
"tcp://22",
@@ -324,7 +320,7 @@ def test_file(host):
324320
assert link.linked_to == "/d/f"
325321
assert link.linked_to == f
326322
assert f == host.file("/d/f")
327-
assert not d == f
323+
assert d != f
328324

329325
host.check_output("ln /d/f /d/h")
330326
hardlink = host.file("/d/h")
@@ -334,7 +330,7 @@ def test_file(host):
334330
assert isinstance(f.inode, int)
335331
assert hardlink.inode == f.inode
336332
assert f == host.file("/d/f")
337-
assert not d == f
333+
assert d != f
338334

339335
host.check_output("rm -f /d/p && mkfifo /d/p")
340336
assert host.file("/d/p").is_pipe
@@ -516,7 +512,7 @@ def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
516512
host.run("service supervisor stop")
517513
assert not host.service("supervisor").is_running
518514
with pytest.raises(RuntimeError) as excinfo:
519-
host.supervisor(
515+
host.supervisor( # noqa: B018
520516
"tail",
521517
supervisorctl_path=supervisorctl_path,
522518
supervisorctl_conf=supervisorctl_conf,
@@ -551,9 +547,9 @@ def test_sudo_from_root(host):
551547

552548
def test_sudo_fail_from_root(host):
553549
assert host.user().name == "root"
554-
with pytest.raises(AssertionError) as exc:
555-
with host.sudo("unprivileged"):
556-
assert host.user().name == "unprivileged"
550+
with host.sudo("unprivileged"):
551+
assert host.user().name == "unprivileged"
552+
with pytest.raises(AssertionError) as exc:
557553
host.check_output("ls /root/invalid")
558554
assert str(exc.value).startswith("Unexpected exit code")
559555
with host.sudo():

testinfra/backend/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def get_backend_class(connection: str) -> type["testinfra.backend.base.BaseBacke
4040
try:
4141
classpath = BACKENDS[connection]
4242
except KeyError:
43-
raise RuntimeError(f"Unknown connection type '{connection}'")
43+
raise RuntimeError(f"Unknown connection type '{connection}'") from None
4444
module, name = classpath.rsplit(".", 1)
4545
return getattr(importlib.import_module(module), name) # type: ignore[no-any-return]
4646

@@ -109,9 +109,6 @@ def get_backends(
109109
key = (name, frozenset(kw.items()))
110110
if key in backends:
111111
continue
112-
if connection == "local":
113-
backend = klass(**kw)
114-
else:
115-
backend = klass(name, **kw)
112+
backend = klass(**kw) if connection == "local" else klass(name, **kw)
116113
backends[key] = backend
117114
return list(backends.values())

0 commit comments

Comments
 (0)