Skip to content

Commit

Permalink
Merge pull request #1051 from bluetech/ruff
Browse files Browse the repository at this point in the history
Use ruff instead of black, flake8, autoflake, pyupgrade
  • Loading branch information
bluetech committed Apr 3, 2024
2 parents 8f7bd68 + 816c9dc commit b6f5d2b
Show file tree
Hide file tree
Showing 26 changed files with 265 additions and 215 deletions.
28 changes: 6 additions & 22 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
repos:
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
- id: autoflake
args: ["--in-place", "--remove-unused-variables", "--remove-all-unused-imports"]
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
args: [--safe, --quiet, --target-version, py35]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.3.5"
hooks:
- id: ruff
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
Expand All @@ -17,19 +13,7 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: debug-statements
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.1
hooks:
- id: pyupgrade
args: [--py3-plus]
- repo: local
hooks:
- id: rst
Expand Down
70 changes: 66 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,72 @@ include-package-data = false
[tool.setuptools_scm]
write_to = "src/xdist/_version.py"

[tool.flake8]
# Ignore any errors related to formatting, let black worry/fix them.
ignore = ["E501", "W503", "E203"]
max-line-length = 100
[tool.ruff]
src = ["src"]

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint]
select = [
"B", # bugbear
"D", # pydocstyle
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"PYI", # flake8-pyi
"UP", # pyupgrade
"RUF", # ruff
"W", # pycodestyle
"T10", # flake8-debugger
"PIE", # flake8-pie
"PGH", # pygrep-hooks
"PLE", # pylint error
"PLW", # pylint warning
"PLR1714", # Consider merging multiple comparisons
]
ignore = [
# bugbear ignore
"B011", # Do not `assert False` (`python -O` removes these calls)
"B028", # No explicit `stacklevel` keyword argument found
# pydocstyle ignore
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"D105", # Missing docstring in magic method
"D106", # Missing docstring in public nested class
"D107", # Missing docstring in `__init__`
"D209", # Multi-line docstring closing quotes should be on a separate line
"D205", # 1 blank line required between summary line and description
"D400", # First line should end with a period
"D401", # First line of docstring should be in imperative mood
# ruff ignore
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
# pylint ignore
"PLW0603", # Using the global statement
"PLW0120", # remove the else and dedent its contents
"PLW2901", # for loop variable overwritten by assignment target
"PLR5501", # Use `elif` instead of `else` then `if`
]

[tool.ruff.lint.pycodestyle]
# In order to be able to format for 88 char in ruff format
max-line-length = 120

[tool.ruff.lint.pydocstyle]
convention = "pep257"

[tool.ruff.lint.isort]
force-single-line = true
combine-as-imports = true
force-sort-within-sections = true
order-by-type = false
lines-after-imports = 2

[tool.ruff.lint.per-file-ignores]
"src/xdist/_version.py" = ["I001"]

[tool.mypy]
mypy_path = ["src"]
Expand Down
11 changes: 5 additions & 6 deletions src/xdist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from xdist.plugin import (
is_xdist_worker,
is_xdist_master,
get_xdist_worker_id,
is_xdist_controller,
)
from xdist._version import version as __version__
from xdist.plugin import get_xdist_worker_id
from xdist.plugin import is_xdist_controller
from xdist.plugin import is_xdist_master
from xdist.plugin import is_xdist_worker


__all__ = [
"__version__",
Expand Down
5 changes: 3 additions & 2 deletions src/xdist/_path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from itertools import chain
import os
from pathlib import Path
from typing import Callable, Iterator
from typing import Callable
from typing import Iterator


def visit_path(
Expand Down
39 changes: 18 additions & 21 deletions src/xdist/dsession.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
from __future__ import annotations

from enum import auto
from enum import Enum
from queue import Empty
from queue import Queue
import sys
from enum import Enum, auto
from typing import Sequence

import pytest

from xdist.remote import Producer
from xdist.scheduler import EachScheduling
from xdist.scheduler import LoadFileScheduling
from xdist.scheduler import LoadGroupScheduling
from xdist.scheduler import LoadScheduling
from xdist.scheduler import LoadScopeScheduling
from xdist.scheduler import WorkStealingScheduling
from xdist.workermanage import NodeManager
from xdist.scheduler import (
EachScheduling,
LoadScheduling,
LoadScopeScheduling,
LoadFileScheduling,
LoadGroupScheduling,
WorkStealingScheduling,
)


from queue import Empty, Queue


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


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

@property
def session_finished(self):
"""Return True if the distributed session has finished
"""Return True if the distributed session has finished.
This means all nodes have executed all test items. This is
used by pytest_runtestloop to break out of its loop.
Expand Down Expand Up @@ -231,9 +230,7 @@ def worker_errordown(self, node, error):
)
if maximum_reached:
if self._max_worker_restart == 0:
msg = "worker {} crashed and worker restarting disabled".format(
node.gateway.id
)
msg = f"worker {node.gateway.id} crashed and worker restarting disabled"
else:
msg = "maximum crashed workers reached: %d" % self._max_worker_restart
self._summary_report = msg
Expand All @@ -251,7 +248,7 @@ def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep("=", f"xdist: {self._summary_report}")

def worker_collectionfinish(self, node, ids):
"""worker has finished test collection.
"""Worker has finished test collection.
This adds the collection for this node to the scheduler. If
the scheduler indicates collection is finished (i.e. all
Expand Down Expand Up @@ -464,7 +461,7 @@ def pytest_xdist_newgateway(self, gateway) -> None:
rinfo = gateway._rinfo()
different_interpreter = rinfo.executable != sys.executable
if different_interpreter:
version = "%s.%s.%s" % rinfo.version_info[:3]
version = "{}.{}.{}".format(*rinfo.version_info[:3])
self.rewrite(
f"[{gateway.id}] {rinfo.platform} Python {version} cwd: {rinfo.cwd}",
newline=True,
Expand All @@ -491,7 +488,7 @@ def pytest_testnodedown(self, node, error) -> None:


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


def get_workers_status_line(
status_and_items: Sequence[tuple[WorkerStatus, int]]
status_and_items: Sequence[tuple[WorkerStatus, int]],
) -> str:
"""
Return the line to display during worker setup/collection based on the
Expand Down
21 changes: 11 additions & 10 deletions src/xdist/looponfail.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
"""
Implement -f aka looponfailing for pytest.
Implement -f aka looponfailing for pytest.
NOTE that we try to avoid loading and depending on application modules
within the controlling process (the one that starts repeatedly test
processes) otherwise changes to source code can crash
the controlling process which should best never happen.
NOTE that we try to avoid loading and depending on application modules
within the controlling process (the one that starts repeatedly test
processes) otherwise changes to source code can crash
the controlling process which should best never happen.
"""

import os
from pathlib import Path
from typing import Dict, Sequence

import pytest
import sys
import time
import execnet
from typing import Dict
from typing import Sequence

from _pytest._io import TerminalWriter
import execnet
import pytest

from xdist._path import visit_path

Expand Down Expand Up @@ -253,7 +254,7 @@ def waitonchange(self, checkinterval=1.0):
return
time.sleep(checkinterval)

def check(self, removepycfiles: bool = True) -> bool: # noqa, too complex
def check(self, removepycfiles: bool = True) -> bool:
changed = False
newstat: Dict[Path, os.stat_result] = {}
for rootdir in self.rootdirlist:
Expand Down
16 changes: 8 additions & 8 deletions src/xdist/newhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

@pytest.hookspec()
def pytest_xdist_setupnodes(config, specs):
"""called before any remote node is set up."""
"""Called before any remote node is set up."""


@pytest.hookspec()
def pytest_xdist_newgateway(gateway):
"""called on new raw gateway creation."""
"""Called on new raw gateway creation."""


@pytest.hookspec(
Expand All @@ -31,7 +31,7 @@ def pytest_xdist_newgateway(gateway):
)
)
def pytest_xdist_rsyncstart(source, gateways):
"""called before rsyncing a directory to remote gateways takes place."""
"""Called before rsyncing a directory to remote gateways takes place."""


@pytest.hookspec(
Expand All @@ -40,17 +40,17 @@ def pytest_xdist_rsyncstart(source, gateways):
)
)
def pytest_xdist_rsyncfinish(source, gateways):
"""called after rsyncing a directory to remote gateways takes place."""
"""Called after rsyncing a directory to remote gateways takes place."""


@pytest.hookspec(firstresult=True)
def pytest_xdist_getremotemodule():
"""called when creating remote node"""
"""Called when creating remote node."""


@pytest.hookspec()
def pytest_configure_node(node):
"""configure node information before it gets instantiated."""
"""Configure node information before it gets instantiated."""


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

@pytest.hookspec()
def pytest_xdist_node_collection_finished(node, ids):
"""called by the controller node when a worker node finishes collecting."""
"""Called by the controller node when a worker node finishes collecting."""


@pytest.hookspec(firstresult=True)
def pytest_xdist_make_scheduler(config, log):
"""return a node scheduler implementation"""
"""Return a node scheduler implementation."""


@pytest.hookspec(firstresult=True)
Expand Down
8 changes: 4 additions & 4 deletions src/xdist/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import uuid
import sys
import uuid
import warnings

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


# -------------------------------------------------------------------------
Expand All @@ -305,15 +305,15 @@ def pytest_cmdline_main(config):


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


def is_xdist_controller(request_or_session) -> bool:
"""Return `True` if this is the xdist controller, `False` otherwise
"""Return `True` if this is the xdist controller, `False` otherwise.
Note: this method also returns `False` when distribution has not been
activated at all.
Expand Down
Loading

0 comments on commit b6f5d2b

Please sign in to comment.