Skip to content

Commit

Permalink
Migrate from autoflake, black, isort, pyupgrade, flake8 and pydocstyl…
Browse files Browse the repository at this point in the history
…e, to ruff

ruff is faster and handle everything we had prior.

isort configuration done based on the indication from
astral-sh/ruff#4670, previousely based on
reorder-python-import (pytest-dev#11896)

flake8-docstrings was a wrapper around pydocstyle (now archived) that
explicitly asks to use ruff in PyCQA/pydocstyle#658.

flake8-typing-import is useful mainly for project that support python 3.7
and the one useful check will be implemented in astral-sh/ruff#2302

We need to keep blacken-doc because ruff does not handle detection
of python code inside .md and .rst. The direct link to the repo is
now used to avoid a redirection.

Manual fixes:
- Lines that became too long
- % formatting that was not done automatically
- type: ignore that were moved around
- noqa of hard to fix issues (UP031 generally)
- fmt: off and fmt: on that is not really identical
  between black and ruff
- autofix re-order in pre-commit from faster to slower

Co-authored-by: Ran Benita <ran@unusedvar.com>
  • Loading branch information
Pierre-Sassoulas and bluetech committed Feb 2, 2024
1 parent 046f647 commit 4588653
Show file tree
Hide file tree
Showing 149 changed files with 799 additions and 971 deletions.
46 changes: 10 additions & 36 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
repos:
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies: [black==24.1.1]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.15"
hooks:
- id: ruff
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand All @@ -20,33 +16,11 @@ repos:
- id: debug-statements
exclude: _pytest/(debugging|hookspec).py
language_version: python3
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.1
hooks:
- id: autoflake
name: autoflake
args: ["--in-place", "--remove-unused-variables", "--remove-all-unused-imports"]
language: python
files: \.py$
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
language_version: python3
additional_dependencies:
- flake8-typing-imports==1.12.0
- flake8-docstrings==1.5.0
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort
args: [--force-single-line, --profile=black]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.16.0
hooks:
- id: pyupgrade
args: [--py38-plus]
- id: blacken-docs
additional_dependencies: [black==24.1.1]
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.5.0
hooks:
Expand Down
3 changes: 0 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
:target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest/main
:alt: pre-commit.ci status

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black

.. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg
:target: https://www.codetriage.com/pytest-dev/pytest

Expand Down
1 change: 1 addition & 0 deletions bench/bench.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys


if __name__ == "__main__":
import cProfile
import pstats
Expand Down
1 change: 1 addition & 0 deletions bench/bench_argcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# FastFilesCompleter 0.7383 1.0760
import timeit


imports = [
"from argcomplete.completers import FilesCompleter as completer",
"from _pytest._argcomplete import FastFilesCompleter as completer",
Expand Down
1 change: 1 addition & 0 deletions bench/skip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest


SKIP = True


Expand Down
1 change: 1 addition & 0 deletions bench/unit_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase # noqa: F401


for i in range(15000):
exec(
f"""
Expand Down
1 change: 1 addition & 0 deletions doc/en/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from _pytest import __version__ as version


if TYPE_CHECKING:
import sphinx.application

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest


mydir = os.path.dirname(__file__)


Expand Down
1 change: 1 addition & 0 deletions doc/en/example/assertion/test_failures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os.path
import shutil


failure_demo = os.path.join(os.path.dirname(__file__), "failure_demo.py")
pytest_plugins = ("pytester",)

Expand Down
21 changes: 9 additions & 12 deletions doc/en/example/multipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest


pythonlist = ["python3.9", "python3.10", "python3.11"]


Expand All @@ -32,14 +33,12 @@ def dumps(self, obj):
dumpfile = self.picklefile.with_name("dump.py")
dumpfile.write_text(
textwrap.dedent(
r"""
rf"""
import pickle
f = open({!r}, 'wb')
s = pickle.dump({!r}, f, protocol=2)
f = open({str(self.picklefile)!r}, 'wb')
s = pickle.dump({obj!r}, f, protocol=2)
f.close()
""".format(
str(self.picklefile), obj
)
"""
)
)
subprocess.run((self.pythonpath, str(dumpfile)), check=True)
Expand All @@ -48,17 +47,15 @@ def load_and_is_true(self, expression):
loadfile = self.picklefile.with_name("load.py")
loadfile.write_text(
textwrap.dedent(
r"""
rf"""
import pickle
f = open({!r}, 'rb')
f = open({str(self.picklefile)!r}, 'rb')
obj = pickle.load(f)
f.close()
res = eval({!r})
res = eval({expression!r})
if not res:
raise SystemExit(1)
""".format(
str(self.picklefile), expression
)
"""
)
)
print(loadfile)
Expand Down
1 change: 1 addition & 0 deletions doc/en/example/xfail_demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest


xfail = pytest.mark.xfail


Expand Down
1 change: 1 addition & 0 deletions extra/get_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import requests


issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues"


Expand Down
52 changes: 52 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,55 @@ target-version = ['py38']
[tool.check-wheel-contents]
# W009: Wheel contains multiple toplevel library entries
ignore = "W009"

[tool.ruff]
src = ["src"]
line-length = 88
select = [
"D", # pydocstyle
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"W", # pycodestyle
]
ignore = [
# pycodestyle ignore
# pytest can do weird low-level things, and we usually know
# what we're doing when we use type(..) is ...
"E721", # Do not compare types, use `isinstance()`
# 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
"D402", # First line should not be the function's signature
"D404", # First word of the docstring should not be "This"
"D415", # First line should end with a period, question mark, or exclamation point
]

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

[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
known-local-folder = ["pytest", "_pytest"]
lines-after-imports = 2
2 changes: 1 addition & 1 deletion scripts/generate-gh-release-notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
Requires Python3.6+.
"""
from pathlib import Path
import re
import sys
from pathlib import Path
from typing import Sequence

import pypandoc
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepare-release-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
`pytest bot <pytestbot@gmail.com>` commit author.
"""
import argparse
import re
from pathlib import Path
import re
from subprocess import check_call
from subprocess import check_output
from subprocess import run
Expand Down
2 changes: 1 addition & 1 deletion scripts/towncrier-draft-to-file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mypy: disallow-untyped-defs
import sys
from subprocess import call
import sys


def main() -> int:
Expand Down
7 changes: 3 additions & 4 deletions scripts/update-plugin-list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

import packaging.version
import platformdirs
import tabulate
import wcwidth
from requests_cache import CachedResponse
from requests_cache import CachedSession
from requests_cache import OriginalResponse
from requests_cache import SQLiteCache
import tabulate
from tqdm import tqdm
import wcwidth


FILE_HEAD = r"""
.. Note this file is autogenerated by scripts/update-plugin-list.py - usually weekly via github action
Expand Down Expand Up @@ -85,7 +86,6 @@ def project_response_with_refresh(
force refresh in case of last serial mismatch
"""

response = session.get(f"https://pypi.org/pypi/{name}/json")
if int(response.headers.get("X-PyPI-Last-Serial", -1)) != last_serial:
response = session.get(f"https://pypi.org/pypi/{name}/json", refresh=True)
Expand Down Expand Up @@ -184,7 +184,6 @@ def version_sort_key(version_string: str) -> Any:

def plugin_definitions(plugins: Iterable[PluginInfo]) -> Iterator[str]:
"""Return RST for the plugin list that fits better on a vertical page."""

for plugin in plugins:
yield dedent(
f"""
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/_argcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
"""

import argparse
from glob import glob
import os
import sys
from glob import glob
from typing import Any
from typing import List
from typing import Optional
Expand Down
1 change: 1 addition & 0 deletions src/_pytest/_code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .source import getrawcode
from .source import Source


__all__ = [
"Code",
"ExceptionInfo",
Expand Down

0 comments on commit 4588653

Please sign in to comment.