Skip to content

Commit b6f5d2b

Browse files
authored
Merge pull request pytest-dev#1051 from bluetech/ruff
Use ruff instead of black, flake8, autoflake, pyupgrade
2 parents 8f7bd68 + 816c9dc commit b6f5d2b

26 files changed

+265
-215
lines changed

.pre-commit-config.yaml

+6-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
repos:
2-
- repo: https://github.com/PyCQA/autoflake
3-
rev: v2.3.1
4-
hooks:
5-
- id: autoflake
6-
args: ["--in-place", "--remove-unused-variables", "--remove-all-unused-imports"]
7-
- repo: https://github.com/psf/black
8-
rev: 24.3.0
9-
hooks:
10-
- id: black
11-
args: [--safe, --quiet, --target-version, py35]
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: "v0.3.5"
4+
hooks:
5+
- id: ruff
6+
args: ["--fix"]
7+
- id: ruff-format
128
- repo: https://github.com/asottile/blacken-docs
139
rev: 1.16.0
1410
hooks:
@@ -17,19 +13,7 @@ repos:
1713
- repo: https://github.com/pre-commit/pre-commit-hooks
1814
rev: v4.5.0
1915
hooks:
20-
- id: trailing-whitespace
21-
- id: end-of-file-fixer
2216
- id: check-yaml
23-
- id: debug-statements
24-
- repo: https://github.com/PyCQA/flake8
25-
rev: 7.0.0
26-
hooks:
27-
- id: flake8
28-
- repo: https://github.com/asottile/pyupgrade
29-
rev: v3.15.1
30-
hooks:
31-
- id: pyupgrade
32-
args: [--py3-plus]
3317
- repo: local
3418
hooks:
3519
- id: rst

pyproject.toml

+66-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,72 @@ include-package-data = false
6767
[tool.setuptools_scm]
6868
write_to = "src/xdist/_version.py"
6969

70-
[tool.flake8]
71-
# Ignore any errors related to formatting, let black worry/fix them.
72-
ignore = ["E501", "W503", "E203"]
73-
max-line-length = 100
70+
[tool.ruff]
71+
src = ["src"]
72+
73+
[tool.ruff.format]
74+
docstring-code-format = true
75+
76+
[tool.ruff.lint]
77+
select = [
78+
"B", # bugbear
79+
"D", # pydocstyle
80+
"E", # pycodestyle
81+
"F", # pyflakes
82+
"I", # isort
83+
"PYI", # flake8-pyi
84+
"UP", # pyupgrade
85+
"RUF", # ruff
86+
"W", # pycodestyle
87+
"T10", # flake8-debugger
88+
"PIE", # flake8-pie
89+
"PGH", # pygrep-hooks
90+
"PLE", # pylint error
91+
"PLW", # pylint warning
92+
"PLR1714", # Consider merging multiple comparisons
93+
]
94+
ignore = [
95+
# bugbear ignore
96+
"B011", # Do not `assert False` (`python -O` removes these calls)
97+
"B028", # No explicit `stacklevel` keyword argument found
98+
# pydocstyle ignore
99+
"D100", # Missing docstring in public module
100+
"D101", # Missing docstring in public class
101+
"D102", # Missing docstring in public method
102+
"D103", # Missing docstring in public function
103+
"D104", # Missing docstring in public package
104+
"D105", # Missing docstring in magic method
105+
"D106", # Missing docstring in public nested class
106+
"D107", # Missing docstring in `__init__`
107+
"D209", # Multi-line docstring closing quotes should be on a separate line
108+
"D205", # 1 blank line required between summary line and description
109+
"D400", # First line should end with a period
110+
"D401", # First line of docstring should be in imperative mood
111+
# ruff ignore
112+
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
113+
# pylint ignore
114+
"PLW0603", # Using the global statement
115+
"PLW0120", # remove the else and dedent its contents
116+
"PLW2901", # for loop variable overwritten by assignment target
117+
"PLR5501", # Use `elif` instead of `else` then `if`
118+
]
119+
120+
[tool.ruff.lint.pycodestyle]
121+
# In order to be able to format for 88 char in ruff format
122+
max-line-length = 120
123+
124+
[tool.ruff.lint.pydocstyle]
125+
convention = "pep257"
126+
127+
[tool.ruff.lint.isort]
128+
force-single-line = true
129+
combine-as-imports = true
130+
force-sort-within-sections = true
131+
order-by-type = false
132+
lines-after-imports = 2
133+
134+
[tool.ruff.lint.per-file-ignores]
135+
"src/xdist/_version.py" = ["I001"]
74136

75137
[tool.mypy]
76138
mypy_path = ["src"]

src/xdist/__init__.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from xdist.plugin import (
2-
is_xdist_worker,
3-
is_xdist_master,
4-
get_xdist_worker_id,
5-
is_xdist_controller,
6-
)
71
from xdist._version import version as __version__
2+
from xdist.plugin import get_xdist_worker_id
3+
from xdist.plugin import is_xdist_controller
4+
from xdist.plugin import is_xdist_master
5+
from xdist.plugin import is_xdist_worker
6+
87

98
__all__ = [
109
"__version__",

src/xdist/_path.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import os
21
from itertools import chain
2+
import os
33
from pathlib import Path
4-
from typing import Callable, Iterator
4+
from typing import Callable
5+
from typing import Iterator
56

67

78
def visit_path(

src/xdist/dsession.py

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
from __future__ import annotations
2+
3+
from enum import auto
4+
from enum import Enum
5+
from queue import Empty
6+
from queue import Queue
27
import sys
3-
from enum import Enum, auto
48
from typing import Sequence
59

610
import pytest
711

812
from xdist.remote import Producer
13+
from xdist.scheduler import EachScheduling
14+
from xdist.scheduler import LoadFileScheduling
15+
from xdist.scheduler import LoadGroupScheduling
16+
from xdist.scheduler import LoadScheduling
17+
from xdist.scheduler import LoadScopeScheduling
18+
from xdist.scheduler import WorkStealingScheduling
919
from xdist.workermanage import NodeManager
10-
from xdist.scheduler import (
11-
EachScheduling,
12-
LoadScheduling,
13-
LoadScopeScheduling,
14-
LoadFileScheduling,
15-
LoadGroupScheduling,
16-
WorkStealingScheduling,
17-
)
18-
19-
20-
from queue import Empty, Queue
2120

2221

2322
class Interrupted(KeyboardInterrupt):
2423
"""signals an immediate interruption."""
2524

2625

2726
class DSession:
28-
"""A pytest plugin which runs a distributed test session
27+
"""A pytest plugin which runs a distributed test session.
2928
3029
At the beginning of the test session this creates a NodeManager
3130
instance which creates and starts all nodes. Nodes then emit
@@ -61,7 +60,7 @@ def __init__(self, config):
6160

6261
@property
6362
def session_finished(self):
64-
"""Return True if the distributed session has finished
63+
"""Return True if the distributed session has finished.
6564
6665
This means all nodes have executed all test items. This is
6766
used by pytest_runtestloop to break out of its loop.
@@ -231,9 +230,7 @@ def worker_errordown(self, node, error):
231230
)
232231
if maximum_reached:
233232
if self._max_worker_restart == 0:
234-
msg = "worker {} crashed and worker restarting disabled".format(
235-
node.gateway.id
236-
)
233+
msg = f"worker {node.gateway.id} crashed and worker restarting disabled"
237234
else:
238235
msg = "maximum crashed workers reached: %d" % self._max_worker_restart
239236
self._summary_report = msg
@@ -251,7 +248,7 @@ def pytest_terminal_summary(self, terminalreporter):
251248
terminalreporter.write_sep("=", f"xdist: {self._summary_report}")
252249

253250
def worker_collectionfinish(self, node, ids):
254-
"""worker has finished test collection.
251+
"""Worker has finished test collection.
255252
256253
This adds the collection for this node to the scheduler. If
257254
the scheduler indicates collection is finished (i.e. all
@@ -464,7 +461,7 @@ def pytest_xdist_newgateway(self, gateway) -> None:
464461
rinfo = gateway._rinfo()
465462
different_interpreter = rinfo.executable != sys.executable
466463
if different_interpreter:
467-
version = "%s.%s.%s" % rinfo.version_info[:3]
464+
version = "{}.{}.{}".format(*rinfo.version_info[:3])
468465
self.rewrite(
469466
f"[{gateway.id}] {rinfo.platform} Python {version} cwd: {rinfo.cwd}",
470467
newline=True,
@@ -491,7 +488,7 @@ def pytest_testnodedown(self, node, error) -> None:
491488

492489

493490
def get_default_max_worker_restart(config):
494-
"""gets the default value of --max-worker-restart option if it is not provided.
491+
"""Gets the default value of --max-worker-restart option if it is not provided.
495492
496493
Use a reasonable default to avoid workers from restarting endlessly due to crashing collections (#226).
497494
"""
@@ -505,7 +502,7 @@ def get_default_max_worker_restart(config):
505502

506503

507504
def get_workers_status_line(
508-
status_and_items: Sequence[tuple[WorkerStatus, int]]
505+
status_and_items: Sequence[tuple[WorkerStatus, int]],
509506
) -> str:
510507
"""
511508
Return the line to display during worker setup/collection based on the

src/xdist/looponfail.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
"""
2-
Implement -f aka looponfailing for pytest.
2+
Implement -f aka looponfailing for pytest.
33
4-
NOTE that we try to avoid loading and depending on application modules
5-
within the controlling process (the one that starts repeatedly test
6-
processes) otherwise changes to source code can crash
7-
the controlling process which should best never happen.
4+
NOTE that we try to avoid loading and depending on application modules
5+
within the controlling process (the one that starts repeatedly test
6+
processes) otherwise changes to source code can crash
7+
the controlling process which should best never happen.
88
"""
99

1010
import os
1111
from pathlib import Path
12-
from typing import Dict, Sequence
13-
14-
import pytest
1512
import sys
1613
import time
17-
import execnet
14+
from typing import Dict
15+
from typing import Sequence
16+
1817
from _pytest._io import TerminalWriter
18+
import execnet
19+
import pytest
1920

2021
from xdist._path import visit_path
2122

@@ -253,7 +254,7 @@ def waitonchange(self, checkinterval=1.0):
253254
return
254255
time.sleep(checkinterval)
255256

256-
def check(self, removepycfiles: bool = True) -> bool: # noqa, too complex
257+
def check(self, removepycfiles: bool = True) -> bool:
257258
changed = False
258259
newstat: Dict[Path, os.stat_result] = {}
259260
for rootdir in self.rootdirlist:

src/xdist/newhooks.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
@pytest.hookspec()
1919
def pytest_xdist_setupnodes(config, specs):
20-
"""called before any remote node is set up."""
20+
"""Called before any remote node is set up."""
2121

2222

2323
@pytest.hookspec()
2424
def pytest_xdist_newgateway(gateway):
25-
"""called on new raw gateway creation."""
25+
"""Called on new raw gateway creation."""
2626

2727

2828
@pytest.hookspec(
@@ -31,7 +31,7 @@ def pytest_xdist_newgateway(gateway):
3131
)
3232
)
3333
def pytest_xdist_rsyncstart(source, gateways):
34-
"""called before rsyncing a directory to remote gateways takes place."""
34+
"""Called before rsyncing a directory to remote gateways takes place."""
3535

3636

3737
@pytest.hookspec(
@@ -40,17 +40,17 @@ def pytest_xdist_rsyncstart(source, gateways):
4040
)
4141
)
4242
def pytest_xdist_rsyncfinish(source, gateways):
43-
"""called after rsyncing a directory to remote gateways takes place."""
43+
"""Called after rsyncing a directory to remote gateways takes place."""
4444

4545

4646
@pytest.hookspec(firstresult=True)
4747
def pytest_xdist_getremotemodule():
48-
"""called when creating remote node"""
48+
"""Called when creating remote node."""
4949

5050

5151
@pytest.hookspec()
5252
def pytest_configure_node(node):
53-
"""configure node information before it gets instantiated."""
53+
"""Configure node information before it gets instantiated."""
5454

5555

5656
@pytest.hookspec()
@@ -65,12 +65,12 @@ def pytest_testnodedown(node, error):
6565

6666
@pytest.hookspec()
6767
def pytest_xdist_node_collection_finished(node, ids):
68-
"""called by the controller node when a worker node finishes collecting."""
68+
"""Called by the controller node when a worker node finishes collecting."""
6969

7070

7171
@pytest.hookspec(firstresult=True)
7272
def pytest_xdist_make_scheduler(config, log):
73-
"""return a node scheduler implementation"""
73+
"""Return a node scheduler implementation."""
7474

7575

7676
@pytest.hookspec(firstresult=True)

src/xdist/plugin.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
import uuid
32
import sys
3+
import uuid
44
import warnings
55

66
import pytest
@@ -296,7 +296,7 @@ def pytest_cmdline_main(config):
296296
if not val("collectonly") and _is_distribution_mode(config) and usepdb:
297297
raise pytest.UsageError(
298298
"--pdb is incompatible with distributing tests; try using -n0 or -nauto."
299-
) # noqa: E501
299+
)
300300

301301

302302
# -------------------------------------------------------------------------
@@ -305,15 +305,15 @@ def pytest_cmdline_main(config):
305305

306306

307307
def is_xdist_worker(request_or_session) -> bool:
308-
"""Return `True` if this is an xdist worker, `False` otherwise
308+
"""Return `True` if this is an xdist worker, `False` otherwise.
309309
310310
:param request_or_session: the `pytest` `request` or `session` object
311311
"""
312312
return hasattr(request_or_session.config, "workerinput")
313313

314314

315315
def is_xdist_controller(request_or_session) -> bool:
316-
"""Return `True` if this is the xdist controller, `False` otherwise
316+
"""Return `True` if this is the xdist controller, `False` otherwise.
317317
318318
Note: this method also returns `False` when distribution has not been
319319
activated at all.

0 commit comments

Comments
 (0)