Skip to content

Add ruff config to pyproject.toml for devs who are interested #25147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,23 @@
max-line-length = 88
select =
# flake8 default
C90, E, F, W,
# docstring-convention=numpy
D100, D101, D102, D103, D104, D105, D106,
D200, D201, D202, D204, D205, D206, D207, D208,
D209, D210, D211, D214, D215,
D300, D301, D302,
D400, D401, D403, D404, D405, D406, D407, D408,
D409, D410, D411, D412, D414,
# matplotlib-specific extra pydocstyle errors
D213,
D, E, F, W,
ignore =
# flake8 default
E121,E123,E126,E226,E24,E704,W503,W504,
# Additional ignores:
E127, E131,
E266,
E305, E306,
E722, E741,
E741,
F841,
# Some new flake8 ignores:
N801, N802, N803, N806, N812,
# pydocstyle
D100, D101, D102, D103, D104, D105, D106, D107,
D200, D202, D203, D204, D205, D207, D212,
D100, D101, D102, D103, D104, D105, D106,
D200, D202, D204, D205,
D301,
D400, D401, D402, D403, D404, D413,
D400, D401, D403, D404
# ignored by pydocstyle numpy docstring convention
D107, D203, D212, D213, D402, D413, D415, D416, D417,

exclude =
.git
Expand All @@ -42,7 +33,6 @@ exclude =
per-file-ignores =
setup.py: E402


lib/matplotlib/__init__.py: E402, F401
lib/matplotlib/_animation_data.py: E501
lib/matplotlib/_api/__init__.py: F401
Expand Down Expand Up @@ -81,9 +71,6 @@ per-file-ignores =
tutorials/text/mathtext.py: E501
tutorials/text/text_intro.py: E402
tutorials/text/text_props.py: E501
tutorials/text/usetex.py: E501
tutorials/toolkits/axes_grid.py: E501
tutorials/toolkits/axisartist.py: E501

examples/animation/frame_grabbing_sgskip.py: E402
examples/images_contours_and_fields/tricontour_demo.py: E201
Expand All @@ -93,7 +80,6 @@ per-file-ignores =
examples/misc/print_stdout_sgskip.py: E402
examples/misc/table_demo.py: E201
examples/style_sheets/bmh.py: E501
examples/style_sheets/plot_solarizedlight2.py: E501
examples/subplots_axes_and_figures/demo_constrained_layout.py: E402
examples/text_labels_and_annotations/custom_legends.py: E402
examples/ticks/date_concise_formatter.py: E402
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def __init__(self, xpad=0., ypad=0., update_func=None):
"""
Parameters
----------

xpad : float, optional
Padding in x-direction.
ypad : float, optional
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, axis):
# constructor docstring, which would otherwise end up interpolated into
# the docstring of Axis.set_scale.
"""
"""
""" # noqa: D419

def set_default_locators_and_formatters(self, axis):
# docstring inherited
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_sigint(target, kwargs):
try:
proc.wait_for('DRAW')
stdout, _ = proc.communicate(timeout=_test_timeout)
except:
except Exception:
proc.kill()
stdout, _ = proc.communicate()
raise
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_other_signal_before_sigint(target, kwargs):
proc.wait_for('SIGUSR1')
os.kill(proc.pid, signal.SIGINT)
stdout, _ = proc.communicate(timeout=_test_timeout)
except:
except Exception:
proc.kill()
stdout, _ = proc.communicate()
raise
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def test_get_font_names():
font = ft2font.FT2Font(path)
prop = ttfFontProperty(font)
ttf_fonts.append(prop.name)
except:
except Exception:
pass
available_fonts = sorted(list(set(ttf_fonts)))
mpl_font_names = sorted(fontManager.get_font_names())
Expand Down
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Text3D(mtext.Text):
----------------
**kwargs
All other parameters are passed on to `~matplotlib.text.Text`.
"""
"""

def __init__(self, x=0, y=0, z=0, text='', zdir='z', **kwargs):
mtext.Text.__init__(self, x, y, text, **kwargs)
Expand Down
115 changes: 114 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,124 @@ requires = [
"setuptools_scm>=7",
]


[tool.isort]
known_mpltoolkits = "mpl_toolkits"
known_pydata = "numpy, matplotlib.pyplot"
known_firstparty = "matplotlib"
sections = "FUTURE,STDLIB,THIRDPARTY,PYDATA,FIRSTPARTY,MPLTOOLKITS,LOCALFOLDER"
no_lines_before = "MPLTOOLKITS"
force_sort_within_sections = true

[tool.ruff]
exclude = [
".git",
"build",
"doc/gallery",
"doc/tutorials",
"tools/gh_api.py",
".tox",
".eggs",
]
ignore = [
"D100",
"D101",
"D102",
"D103",
"D104",
"D105",
"D106",
Comment on lines +29 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused, these are in ignore, but also select. Do we want to just remove them altogether if we aren't going to use them? (ignore takes precedent)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, but that is how they are in the flake8 config, sooo???

My best guess is that that was originally put that way because those are rules we would like to enforce, but have too many failures to individually enumerate them yet.

I ran flake8-to-ruff (a tool distributed separately on pypi by the ruff devs)
The only rule selection difference I made explicitly was to ignore D90 (mccabe cyclomatic complexity) because that didn't actually do anything on flake8 (the config value was null).

Outside of that the handful of rules that are not currently supported by ruff are added to "external" (There may be cause to add to ignores later as well, didn't look too closely at those because they can't be configured as ignores yet anyway)

It did not respect order of the per file ignores so I re-sorted them because that made it much easier to navigate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I tried it out, and selecting "D" was largely simply fine...

Looking back at the flake8 config, it contains some comments which indicate that the select was all rules for the numpy convention. But then ignored are all the rules we break regularly.

I already did configure it to use numpy style docstrings, which automatically disables several of the ignored rules (without having to specify) Thus selecting all "D" rules and only ignoring has the same effect, and cleans up the config quite a bit.

Additionally, the following rules from the ignore list had only a single occurrence, so I may as well fix that occurrence and enforce the rule:

  • D207: Docstring is under-indented
    • just a single missing space in the closing """ for one docstring
  • D403: First word of the first line should be properly capitalized
    • No occurrences, if no objection to enforcing this going forward, may as well enable
  • E722: do not use bare except, specify exception instead
    • single occurence in tests, added a noqa comment (could alternatively add to per file ignore if we wish)

Additionally, selecting all pydocstyle (D) enabled the previously unselected D419: Docstring is empty.

There was a single occurrence of an empty docstring, in scale.py:LinearScale, which has a comment stating that the method is there explicitly to prevent inherited docstring.

  • noqa comment added, enforcing the rule otherwise

The N error codes (enforcing naming conventions) that were included in the ignore list were also never selected (and selecting "N" was rather noisy even with the ignores).
These have been removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think incrementally adding linter checks when appropriate is probably going to be easier, so this sounds fine to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I will state that the speed of ruff made this a lot better to test which rules could be enforced now... doing that with flake8 would have taken much much longer)

"D200",
"D202",
"D204",
"D205",
"D301",
"D400",
"D401",
"D403",
"D404",
"E741",
"F841",
]
line-length = 88
select = [
"D",
"E",
"F",
"W",
]

# The following error codes are not supported by ruff v0.0.240
# They are planned and should be selected once implemented
# even if they are deselected by default.
# These are primarily whitespace/corrected by autoformatters (which we don't use).
# See https://github.com/charliermarsh/ruff/issues/2402 for status on implementation
external = [
"E122",
"E201",
"E202",
"E203",
"E221",
"E251",
"E261",
"E272",
"E302",
"E703",
]

target-version = "py39"

[tool.ruff.pydocstyle]
convention = "numpy"

[tool.ruff.per-file-ignores]
"setup.py" = ["E402"]

"doc/conf.py" = ["E402"]
"examples/animation/frame_grabbing_sgskip.py" = ["E402"]
"examples/lines_bars_and_markers/marker_reference.py" = ["E402"]
"examples/misc/print_stdout_sgskip.py" = ["E402"]
"examples/style_sheets/bmh.py" = ["E501"]
"examples/subplots_axes_and_figures/demo_constrained_layout.py" = ["E402"]
"examples/text_labels_and_annotations/custom_legends.py" = ["E402"]
"examples/ticks/date_concise_formatter.py" = ["E402"]
"examples/ticks/date_formatters_locators.py" = ["F401"]
"examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py" = ["E402"]
"examples/user_interfaces/embedding_in_gtk3_sgskip.py" = ["E402"]
"examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py" = ["E402"]
"examples/user_interfaces/embedding_in_gtk4_sgskip.py" = ["E402"]
"examples/user_interfaces/gtk3_spreadsheet_sgskip.py" = ["E402"]
"examples/user_interfaces/gtk4_spreadsheet_sgskip.py" = ["E402"]
"examples/user_interfaces/mpl_with_glade3_sgskip.py" = ["E402"]
"examples/user_interfaces/pylab_with_gtk3_sgskip.py" = ["E402"]
"examples/user_interfaces/pylab_with_gtk4_sgskip.py" = ["E402"]
"examples/userdemo/pgf_preamble_sgskip.py" = ["E402"]

"lib/matplotlib/__init__.py" = ["E402", "F401"]
"lib/matplotlib/_animation_data.py" = ["E501"]
"lib/matplotlib/_api/__init__.py" = ["F401"]
"lib/matplotlib/axes/__init__.py" = ["F401", "F403"]
"lib/matplotlib/backends/backend_template.py" = ["F401"]
"lib/matplotlib/font_manager.py" = ["E501"]
"lib/matplotlib/image.py" = ["F401", "F403"]
"lib/matplotlib/pylab.py" = ["F401", "F403"]
"lib/matplotlib/pyplot.py" = ["F401", "F811"]
"lib/matplotlib/tests/test_mathtext.py" = ["E501"]
"lib/mpl_toolkits/axisartist/__init__.py" = ["F401"]
"lib/pylab.py" = ["F401", "F403"]

"tutorials/advanced/path_tutorial.py" = ["E402"]
"tutorials/advanced/patheffects_guide.py" = ["E402"]
"tutorials/advanced/transforms_tutorial.py" = ["E402", "E501"]
"tutorials/colors/colormaps.py" = ["E501"]
"tutorials/colors/colors.py" = ["E402"]
"tutorials/intermediate/artists.py" = ["E402"]
"tutorials/intermediate/constrainedlayout_guide.py" = ["E402"]
"tutorials/intermediate/legend_guide.py" = ["E402"]
"tutorials/intermediate/tight_layout_guide.py" = ["E402"]
"tutorials/introductory/animation_tutorial.py" = ["E501"]
"tutorials/introductory/images.py" = ["E501"]
"tutorials/introductory/pyplot.py" = ["E402", "E501"]
"tutorials/text/annotations.py" = ["E402", "E501"]
"tutorials/text/mathtext.py" = ["E501"]
"tutorials/text/text_intro.py" = ["E402"]
"tutorials/text/text_props.py" = ["E501"]