From e2026b92f37d3a4b995cd04a1c8cf8f2ebddd428 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:46:46 +0100 Subject: [PATCH 01/12] GH-121970: Use Ruff to check and format the docs tools --- .pre-commit-config.yaml | 4 + Doc/.ruff.toml | 55 ++++++++ Doc/conf.py | 120 +++++++++------- Doc/tools/check-warnings.py | 163 ++++++++++------------ Doc/tools/extensions/glossary_search.py | 7 +- Doc/tools/extensions/lexers/__init__.py | 10 +- Doc/tools/extensions/lexers/asdl_lexer.py | 36 ++--- Doc/tools/extensions/lexers/peg_lexer.py | 72 +++++----- 8 files changed, 266 insertions(+), 201 deletions(-) create mode 100644 Doc/.ruff.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fde9d9149bf62b1..a444c56c1338270 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,6 +2,10 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.3.4 hooks: + - id: ruff + name: Run Ruff on Doc/ + args: [--exit-non-zero-on-fix] + files: ^Doc/ - id: ruff name: Run Ruff on Lib/test/ args: [--exit-non-zero-on-fix] diff --git a/Doc/.ruff.toml b/Doc/.ruff.toml new file mode 100644 index 000000000000000..2af7e6bd9d60503 --- /dev/null +++ b/Doc/.ruff.toml @@ -0,0 +1,55 @@ +target-version = "py312" # align with the version in oldest_supported_sphinx +fix = true +output-format = "full" +extend-exclude = [ + "includes/*", + "tools/extensions/c_annotations.py", + "tools/extensions/escape4chm.py", + "tools/extensions/patchlevel.py", + "tools/extensions/pyspecific.py", +] + +[lint] +preview = true +select = [ + "C4", # flake8-comprehensions + "B", # flake8-bugbear + "E", # pycodestyle + "EM", # flake8-errmsg + "F", # pyflakes + "FA", # flake8-future-annotations + "FLY", # flynt + "FURB",# refurb + "G", # flake8-logging-format + "I", # isort + "LOG", # flake8-logging + "N", # pep8-naming + "PERF",# perflint + "PGH", # pygrep-hooks + "PT", # flake8-pytest-style + "TCH", # flake8-type-checking + "UP", # pyupgrade + "W", # pycodestyle +] + +[lint.per-file-ignores] +"tools/extensions/lexers/peg_lexer.py" =[ + "E501", +] +"tools/extensions/c_annotations.py" =[ + "E501", +] +"tools/extensions/pyspecific.py" =[ + "E501", +] + +[format] +preview = true +quote-style = "single" +exclude = [ + "tools/extensions/lexers/*", + "tools/extensions/c_annotations.py", + "tools/extensions/escape4chm.py", + "tools/extensions/patchlevel.py", + "tools/extensions/pyspecific.py", +] diff --git a/Doc/conf.py b/Doc/conf.py index 5addee0378984a5..7c6201a123d5b5c 100644 --- a/Doc/conf.py +++ b/Doc/conf.py @@ -9,6 +9,7 @@ import os import sys import time + sys.path.append(os.path.abspath('tools/extensions')) sys.path.append(os.path.abspath('includes')) @@ -30,20 +31,20 @@ # Skip if downstream redistributors haven't installed them try: - import notfound.extension + import notfound.extension # NoQA: F401 except ImportError: pass else: extensions.append('notfound.extension') try: - import sphinxext.opengraph + import sphinxext.opengraph # NoQA: F401 except ImportError: pass else: extensions.append('sphinxext.opengraph') -doctest_global_setup = ''' +doctest_global_setup = """ try: import _tkinter except ImportError: @@ -53,7 +54,7 @@ import warnings warnings.simplefilter('error') del warnings -''' +""" manpages_url = 'https://manpages.debian.org/{path}' @@ -63,7 +64,8 @@ # We look for the Include/patchlevel.h file in the current Python source tree # and replace the values accordingly. -import patchlevel +import patchlevel # NoQA: E402 + version, release = patchlevel.get_version_info() rst_epilog = f""" @@ -298,7 +300,8 @@ # Disable Docutils smartquotes for several translations smartquotes_excludes = { - 'languages': ['ja', 'fr', 'zh_TW', 'zh_CN'], 'builders': ['man', 'text'], + 'languages': ['ja', 'fr', 'zh_TW', 'zh_CN'], + 'builders': ['man', 'text'], } # Avoid a warning with Sphinx >= 4.0 @@ -319,30 +322,32 @@ 'collapsiblesidebar': True, 'issues_url': '/bugs.html', 'license_url': '/license.html', - 'root_include_title': False # We use the version switcher instead. + 'root_include_title': False, # We use the version switcher instead. } -if os.getenv("READTHEDOCS"): - html_theme_options["hosted_on"] = 'Read the Docs' +if os.getenv('READTHEDOCS'): + html_theme_options['hosted_on'] = ( + 'Read the Docs' + ) # Override stylesheet fingerprinting for Windows CHM htmlhelp to fix GH-91207 # https://github.com/python/cpython/issues/91207 if any('htmlhelp' in arg for arg in sys.argv): html_style = 'pydoctheme.css' - print("\nWARNING: Windows CHM Help is no longer supported.") - print("It may be removed in the future\n") + print('\nWARNING: Windows CHM Help is no longer supported.') + print('It may be removed in the future\n') # Short title used e.g. for