From 48a778cdb193048f19d29cacc76a71ee4a64254b Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:51:02 +0100 Subject: [PATCH] Adopt Ruff --- .flake8 | 3 -- .pre-commit-config.yaml | 37 ++------------- .ruff.toml | 84 ++++++++++++++++++++++++++++++++++ sphinx_autobuild/__main__.py | 1 - sphinx_autobuild/build.py | 1 - sphinx_autobuild/filter.py | 2 +- sphinx_autobuild/middleware.py | 6 ++- sphinx_autobuild/server.py | 5 +- 8 files changed, 97 insertions(+), 42 deletions(-) delete mode 100644 .flake8 create mode 100644 .ruff.toml diff --git a/.flake8 b/.flake8 deleted file mode 100644 index e4e5eb3..0000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -max-line-length = 88 -extend-ignore = E203, W503 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a436890..c941efb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,26 +1,9 @@ repos: - - repo: https://github.com/asottile/pyupgrade - rev: v3.15.2 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.7 hooks: - - id: pyupgrade - args: [--py39-plus] - - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.4.0 - hooks: - - id: black - - - repo: https://github.com/PyCQA/isort - rev: 5.13.2 - hooks: - - id: isort - files: \.py$ - args: ["--profile", "black"] - - - repo: https://github.com/PyCQA/flake8 - rev: 7.0.0 - hooks: - - id: flake8 + - id: ruff + - id: ruff-format - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 @@ -34,15 +17,3 @@ repos: - id: end-of-file-fixer - id: forbid-new-submodules - id: trailing-whitespace - - - repo: https://github.com/PyCQA/pydocstyle - rev: 6.3.0 - hooks: - - id: pydocstyle - files: src/.*\.py$ - - - repo: https://github.com/asottile/blacken-docs - rev: 1.16.0 - hooks: - - id: blacken-docs - additional_dependencies: [black==24.4.0] diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 0000000..e60e99e --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,84 @@ +target-version = "py39" # Pin Ruff to Python 3.9 +line-length = 88 +output-format = "full" +extend-exclude = [ + "noxfile.py", +] + +[lint] +preview = true +ignore = [ + "D100", + "D101", + "D102", + "D103", + "D105", + "D107", + "E203", +# "W503", # unimplemented in Ruff (as of 2024-04-16) +] +select = [ + # flake8-builtins ('A') + "A", + # flake8-unused-arguments ('ARG') + "ARG", + # flake8-async ('ASYNC') + "ASYNC", + # flake8-bugbear ('B') + "B", + # flake8-comprehensions ('C4') + "C4", + # mccabe ('C90') + "C901", + # pydocstyle ('D') + "D", + # pycodestyle ('E') + "E", + # pyflakes ('F') + "F", + # isort ('I') + "I", + # pep8-naming ('N') + "N", + # perflint ('PERF') + "PERF", + # pygrep-hooks ('PGH') + "PGH", + # flake8-pytest-style ('PT') + "PT", + # flake8-quotes ('Q') + "Q", + # flake8-return ('RET') + "RET", + # flake8-raise ('RSE') + "RSE", + # flake8-simplify ('SIM') + "SIM", + # pyupgrade ('UP') + "UP", + # pycodestyle ('W') + "W", +] + +[lint.pydocstyle] +convention = "pep257" + +[lint.per-file-ignores] +"tests/*" = [ + "E501", + "S101", # whitelist ``assert`` for tests + "T201", # whitelist ``print`` for tests +] + +[lint.flake8-quotes] +inline-quotes = "double" + +[lint.isort] +forced-separate = [ + "tests", +] + +[format] +preview = true +quote-style = "double" +docstring-code-format = true diff --git a/sphinx_autobuild/__main__.py b/sphinx_autobuild/__main__.py index d8767d7..a78b6b9 100644 --- a/sphinx_autobuild/__main__.py +++ b/sphinx_autobuild/__main__.py @@ -123,7 +123,6 @@ def _get_sphinx_build_parser(): def _get_parser(): """Get the application's argument parser.""" - parser = argparse.ArgumentParser(allow_abbrev=False) parser.add_argument( "--version", action="version", version=f"sphinx-autobuild {__version__}" diff --git a/sphinx_autobuild/build.py b/sphinx_autobuild/build.py index e918a76..0859dc3 100644 --- a/sphinx_autobuild/build.py +++ b/sphinx_autobuild/build.py @@ -16,7 +16,6 @@ def __init__(self, sphinx_args, *, url_host, pre_build_commands): def __call__(self, *, rebuild: bool = True): """Generate the documentation using ``sphinx``.""" - if rebuild: show(context="Detected change. Rebuilding...") diff --git a/sphinx_autobuild/filter.py b/sphinx_autobuild/filter.py index 8abbfdd..456786f 100644 --- a/sphinx_autobuild/filter.py +++ b/sphinx_autobuild/filter.py @@ -27,7 +27,7 @@ def __call__(self, path): return True # Any regular expression matches. - for regex in self.regex_based_patterns: + for regex in self.regex_based_patterns: # NoQA: SIM110 if regex.search(path): return True diff --git a/sphinx_autobuild/middleware.py b/sphinx_autobuild/middleware.py index d39587a..2652a0c 100644 --- a/sphinx_autobuild/middleware.py +++ b/sphinx_autobuild/middleware.py @@ -1,7 +1,11 @@ from __future__ import annotations +from typing import TYPE_CHECKING + from starlette.datastructures import MutableHeaders -from starlette.types import ASGIApp, Message, Receive, Scope, Send + +if TYPE_CHECKING: + from starlette.types import ASGIApp, Message, Receive, Scope, Send def web_socket_script(ws_url: str) -> str: diff --git a/sphinx_autobuild/server.py b/sphinx_autobuild/server.py index def7737..5f0233c 100644 --- a/sphinx_autobuild/server.py +++ b/sphinx_autobuild/server.py @@ -4,15 +4,16 @@ import os from concurrent.futures import ProcessPoolExecutor from contextlib import AbstractAsyncContextManager, asynccontextmanager +from typing import TYPE_CHECKING import watchfiles -from starlette.types import Receive, Scope, Send from starlette.websockets import WebSocket -TYPE_CHECKING = False if TYPE_CHECKING: from collections.abc import Callable + from starlette.types import Receive, Scope, Send + from sphinx_autobuild.filter import IgnoreFilter