From c01db9f0d29a0097e30480d481070d34a6cb4131 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 17 Mar 2025 17:29:17 +0000 Subject: [PATCH 1/2] Use ruff instead of flake8 to check PEP8 --- .flake8 | 91 --------------------------- .github/workflows/reviewdog.yml | 14 ++--- .pre-commit-config.yaml | 15 +++-- doc/devel/coding_guide.rst | 26 +++++--- doc/install/dependencies.rst | 3 - environment.yml | 3 +- pyproject.toml | 19 ++++++ requirements/dev/dev-requirements.txt | 2 +- requirements/testing/flake8.txt | 9 --- 9 files changed, 51 insertions(+), 131 deletions(-) delete mode 100644 .flake8 delete mode 100644 requirements/testing/flake8.txt diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 7297d72b5841..000000000000 --- a/.flake8 +++ /dev/null @@ -1,91 +0,0 @@ -[flake8] -max-line-length = 88 -select = - # flake8 default - D, E, F, W, -ignore = - # flake8 default - E121,E123,E126,E226,E24,E704,W503,W504, - # Additional ignores: - E127, E131, - E266, - E305, E306, - E741, - F841, - # pydocstyle - D100, D101, D102, D103, D104, D105, D106, - D200, D202, D204, D205, - D301, - D400, D401, D403, D404 - # ignored by pydocstyle numpy docstring convention - D107, D203, D212, D402, D413, D415, D416, D417, - # while D213 is ignored by the numpy docstring convention, it's left out above, - # because we want to enforce it. - -exclude = - .git - build - doc/gallery - doc/tutorials - # External files. - tools/gh_api.py - .tox - .eggs - -per-file-ignores = - lib/matplotlib/_cm.py: E202, E203, E302 - lib/matplotlib/_mathtext.py: E221 - lib/matplotlib/_mathtext_data.py: E203 - lib/matplotlib/backends/backend_template.py: F401 - lib/matplotlib/mathtext.py: E221 - lib/matplotlib/pylab.py: F401, F403 - lib/matplotlib/pyplot.py: F811 - lib/matplotlib/tests/test_mathtext.py: E501 - lib/matplotlib/transforms.py: E201, E202 - lib/matplotlib/tri/_triinterpolate.py: E201, E221 - lib/mpl_toolkits/axes_grid1/axes_size.py: E272 - lib/mpl_toolkits/axisartist/angle_helper.py: E221 - lib/mpl_toolkits/mplot3d/proj3d.py: E201 - - doc/conf.py: E402 - galleries/users_explain/quick_start.py: E402 - galleries/users_explain/artists/paths.py: E402 - galleries/users_explain/artists/patheffects_guide.py: E402 - galleries/users_explain/artists/transforms_tutorial.py: E402, E501 - galleries/users_explain/colors/colormaps.py: E501 - galleries/users_explain/colors/colors.py: E402 - galleries/tutorials/artists.py: E402 - galleries/users_explain/axes/constrainedlayout_guide.py: E402 - galleries/users_explain/axes/legend_guide.py: E402 - galleries/users_explain/axes/tight_layout_guide.py: E402 - galleries/users_explain/animations/animations.py: E501 - galleries/tutorials/images.py: E501 - galleries/tutorials/pyplot.py: E402, E501 - galleries/users_explain/text/annotations.py: E402, E501 - galleries/users_explain/text/mathtext.py: E501 - galleries/users_explain/text/text_intro.py: E402 - galleries/users_explain/text/text_props.py: E501 - - galleries/examples/animation/frame_grabbing_sgskip.py: E402 - galleries/examples/images_contours_and_fields/tricontour_demo.py: E201 - galleries/examples/images_contours_and_fields/tripcolor_demo.py: E201 - galleries/examples/images_contours_and_fields/triplot_demo.py: E201 - galleries/examples/lines_bars_and_markers/marker_reference.py: E402 - galleries/examples/misc/print_stdout_sgskip.py: E402 - galleries/examples/misc/table_demo.py: E201 - galleries/examples/style_sheets/bmh.py: E501 - galleries/examples/subplots_axes_and_figures/demo_constrained_layout.py: E402 - galleries/examples/text_labels_and_annotations/custom_legends.py: E402 - galleries/examples/ticks/date_concise_formatter.py: E402 - galleries/examples/ticks/date_formatters_locators.py: F401 - galleries/examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py: E402 - galleries/examples/user_interfaces/embedding_in_gtk3_sgskip.py: E402 - galleries/examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py: E402 - galleries/examples/user_interfaces/embedding_in_gtk4_sgskip.py: E402 - galleries/examples/user_interfaces/gtk3_spreadsheet_sgskip.py: E402 - galleries/examples/user_interfaces/gtk4_spreadsheet_sgskip.py: E402 - galleries/examples/user_interfaces/mpl_with_glade3_sgskip.py: E402 - galleries/examples/user_interfaces/pylab_with_gtk3_sgskip.py: E402 - galleries/examples/user_interfaces/pylab_with_gtk4_sgskip.py: E402 - galleries/examples/userdemo/pgf_preamble_sgskip.py: E402 -force-check = True diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 18ff88d972df..d8b30822966d 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -6,8 +6,8 @@ permissions: contents: read jobs: - flake8: - name: flake8 + ruff: + name: ruff runs-on: ubuntu-latest permissions: checks: write @@ -21,19 +21,19 @@ jobs: with: python-version: '3.10' - - name: Install flake8 - run: pip3 install -r requirements/testing/flake8.txt + - name: Install ruff + run: pip3 install ruff - name: Set up reviewdog uses: reviewdog/action-setup@e04ffabe3898a0af8d0fb1af00c188831c4b5893 # v1.3.9 - - name: Run flake8 + - name: Run ruff env: REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -o pipefail - flake8 --docstring-convention=all | \ - reviewdog -f=pep8 -name=flake8 \ + ruff check --output-format rdjson | \ + reviewdog -f=rdjson \ -tee -reporter=github-check -filter-mode nofilter mypy: name: mypy diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0dc717a56e9e..55a33c52c7f8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,15 +41,14 @@ repos: args: ["--config-file=pyproject.toml", "lib/matplotlib"] files: lib/matplotlib # Only run when files in lib/matplotlib are changed. pass_filenames: false - - repo: https://github.com/pycqa/flake8 - rev: 7.1.1 + + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.11.0 hooks: - - id: flake8 - additional_dependencies: - - pydocstyle>5.1.0 - - flake8-docstrings>1.4.0 - - flake8-force - args: ["--docstring-convention=all"] + # Run the linter. + - id: ruff + args: [--fix, --show-fixes] - repo: https://github.com/codespell-project/codespell rev: v2.3.0 hooks: diff --git a/doc/devel/coding_guide.rst b/doc/devel/coding_guide.rst index 36802de49bd0..2b156cedca05 100644 --- a/doc/devel/coding_guide.rst +++ b/doc/devel/coding_guide.rst @@ -15,22 +15,28 @@ consistency, and maintainability of the code base. .. _code-style: -PEP8, as enforced by flake8 -=========================== +PEP8, as enforced by ruff +========================= -Formatting should follow the recommendations of PEP8_, as enforced by flake8_. +Formatting should follow the recommendations of PEP8_, as enforced by ruff_. Matplotlib modifies PEP8 to extend the maximum line length to 88 -characters. You can check flake8 compliance from the command line with :: +characters. You can check PEP8 compliance from the command line with :: + + python -m pip install ruff + ruff check /path/to/module.py + +or your editor may provide integration with it. To check all files, +and fix any errors in-place (where possible) run :: + + ruff check --fix - python -m pip install flake8 - flake8 /path/to/module.py -or your editor may provide integration with it. Note that Matplotlib intentionally -does not use the black_ auto-formatter (1__), in particular due to its inability -to understand the semantics of mathematical expressions (2__, 3__). +Matplotlib intentionally does not use the black_ auto-formatter (1__), +in particular due to its inability to understand the semantics of +mathematical expressions (2__, 3__). .. _PEP8: https://www.python.org/dev/peps/pep-0008/ -.. _flake8: https://flake8.pycqa.org/ +.. _ruff: https://docs.astral.sh/ruff/ .. _black: https://black.readthedocs.io/ .. __: https://github.com/matplotlib/matplotlib/issues/18796 .. __: https://github.com/psf/black/issues/148 diff --git a/doc/install/dependencies.rst b/doc/install/dependencies.rst index 1e1837512409..2bcab755afa2 100644 --- a/doc/install/dependencies.rst +++ b/doc/install/dependencies.rst @@ -346,7 +346,6 @@ otherwise they must be installed manually: - pikepdf_ used in some tests for the pgf and pdf backends - psutil_ used in testing the interactive backends - pytest-cov_ (>= 2.3.1) to collect coverage information -- pytest-flake8_ to test coding standards using flake8_ - pytest-timeout_ to limit runtime in case of stuck tests - pytest-xdist_ to run tests in parallel - pytest-xvfb_ to run tests without windows popping up (Linux) @@ -373,7 +372,6 @@ them will be skipped by pytest. .. _Ghostscript: https://ghostscript.com/ .. _Inkscape: https://inkscape.org .. _WenQuanYi Zen Hei: http://wenq.org/en/ -.. _flake8: https://pypi.org/project/flake8/ .. _nbconvert: https://pypi.org/project/nbconvert/ .. _nbformat: https://pypi.org/project/nbformat/ .. _pandas: https://pypi.org/project/pandas/ @@ -381,7 +379,6 @@ them will be skipped by pytest. .. _psutil: https://pypi.org/project/psutil/ .. _pytz: https://fonts.google.com/noto/use#faq .. _pytest-cov: https://pytest-cov.readthedocs.io/en/latest/ -.. _pytest-flake8: https://pypi.org/project/pytest-flake8/ .. _pytest-timeout: https://pypi.org/project/pytest-timeout/ .. _pytest-xdist: https://pypi.org/project/pytest-xdist/ .. _pytest-xvfb: https://pypi.org/project/pytest-xvfb/ diff --git a/environment.yml b/environment.yml index 9710c6dec4c4..24aab818ef84 100644 --- a/environment.yml +++ b/environment.yml @@ -55,8 +55,6 @@ dependencies: # testing - black<24 - coverage - - flake8>=3.8,<7.2 - - flake8-docstrings>=1.4.0 - gtk4 - ipykernel - nbconvert[execute]!=6.0.0,!=6.0.1,!=7.3.0,!=7.3.1 @@ -72,4 +70,5 @@ dependencies: - pytest-xdist - tornado - pytz + - ruff - tox diff --git a/pyproject.toml b/pyproject.toml index 32abba6aabd5..aa18fa8ebb3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,6 +99,10 @@ exclude = [ "tools/gh_api.py", ".tox", ".eggs", + # TODO: fix .pyi files + "*.pyi", + # TODO: fix .ipynb files + "*.ipynb" ] line-length = 88 target-version = "py310" @@ -112,15 +116,28 @@ ignore = [ "D104", "D105", "D106", + "D107", "D200", "D202", + "D203", "D204", "D205", + "D212", "D301", "D400", "D401", + "D402", "D403", "D404", + "D413", + "D415", + "D416", + "D417", + "E24", + "E266", + "E305", + "E306", + "E721", "E741", "F841", ] @@ -180,6 +197,7 @@ convention = "numpy" "galleries/examples/user_interfaces/pylab_with_gtk4_sgskip.py" = ["E402"] "galleries/examples/userdemo/pgf_preamble_sgskip.py" = ["E402"] +"lib/matplotlib/__init__.py" = ["F822"] "lib/matplotlib/_cm.py" = ["E202", "E203"] "lib/matplotlib/_mathtext.py" = ["E221"] "lib/matplotlib/_mathtext_data.py" = ["E203"] @@ -194,6 +212,7 @@ convention = "numpy" "lib/mpl_toolkits/mplot3d/proj3d.py" = ["E201"] "galleries/users_explain/artists/paths.py" = ["E402"] +"galleries/users_explain/quick_start.py" = ["E402"] "galleries/users_explain/artists/patheffects_guide.py" = ["E402"] "galleries/users_explain/artists/transforms_tutorial.py" = ["E402", "E501"] "galleries/users_explain/colors/colormaps.py" = ["E501"] diff --git a/requirements/dev/dev-requirements.txt b/requirements/dev/dev-requirements.txt index e5cbc1091bb2..3208949ba0e8 100644 --- a/requirements/dev/dev-requirements.txt +++ b/requirements/dev/dev-requirements.txt @@ -2,4 +2,4 @@ -r ../doc/doc-requirements.txt -r ../testing/all.txt -r ../testing/extra.txt --r ../testing/flake8.txt +ruff diff --git a/requirements/testing/flake8.txt b/requirements/testing/flake8.txt deleted file mode 100644 index cb34fee3f385..000000000000 --- a/requirements/testing/flake8.txt +++ /dev/null @@ -1,9 +0,0 @@ -# Extra pip requirements for the GitHub Actions flake8 build - -flake8>=3.8,<7.2 -# versions less than 5.1.0 raise on some interp'd docstrings -pydocstyle>=5.1.0 -# 1.4.0 adds docstring-convention=all -flake8-docstrings>=1.4.0 -# fix bug where flake8 aborts checking on syntax error -flake8-force From d53121dc3055183fa42e1988962dcdb04589962f Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 17 Mar 2025 17:33:10 +0000 Subject: [PATCH 2/2] Make fix --- lib/mpl_toolkits/mplot3d/tests/test_axes3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py index b57dd689cf0e..79c7baba9bd1 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py @@ -2021,7 +2021,7 @@ def test_rotate(style): c = np.sqrt(3)/2 expectations = { - ('azel', 0, 1, 0): (0, -45, 0), + ('azel', 0, 1, 0): (0, -45, 0), ('azel', 0, 0, 1): (-45, 0, 0), ('azel', 0, 0.5, c): (-38.971143, -22.5, 0), ('azel', 0, 2, 0): (0, -90, 0),