Skip to content
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

Enable ruff's pydocstyle (D) rules and remove docformatter #2925

Merged
merged 20 commits into from
Jan 4, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Dec 27, 2023

Better to review the commits one by one to understand the changes.

In b9c4d42, we enable ruff's pydocstyle (D) rules and set the docstring convention to "numpy".

After setting the "numpy" convention, we have to fix/ignore some violations (done in fa93c37, 092075a, 608f38d, 0f9cb0b, d9116bd, f22bfa0, 1280a1c).

After the above fixes, we still have hundreds of violations against 6 rules, as shown below:

$ ruff check pygmt doc/conf.py examples --statistics
645	D200	[*] One-line docstring should fit on one line
312	D205	[ ] 1 blank line required between summary line and description
123	D400	[ ] First line should end with a period
 30	D401	[ ] First line of docstring should be in imperative mood: "A mock GMT API function that always returns a given value."
 30	D412	[*] No blank lines allowed between a section header and its content ("Examples")
 12	D202	[*] No blank lines allowed after function docstring (found 1)
  • D200: This is against our current style, so it must be disabled.
  • D205: This rule makes sense. It seems PEP 257 recommends that the summary line should be one line, but we actually have some multi-line summary, which causes lots of violations. We have to disable the rule now and may enable it in the future.
  • D400: Makes sense but too many violations. Disable it now to avoid too many changes in this PR.
  • D401: Not always make sense, so disabled.
  • D412: Makes sense, but have 30 violations. Disabled to make the changes in this PR small, but I'm also OK with enabling it.
  • D202: Makes sense but have 12 violations. Disabled to make the changes in this PR small, but I'm also OK with enabling it.

In commit 3170401, these 6 rules are disabled.

As the documentation says (https://docs.astral.sh/ruff/faq/#does-ruff-support-numpy-or-google-style-docstrings):

The NumPy convention includes all D errors apart from: D107, D203, D212, D213, D402, D413, D415, D416, and D417.

Among these rules, D213 and D410 make sense to the project and are enabled in commit 5d983e6.

Then docformatter can be fully replaced by ruff and is removed in commit 435290f. The contributing guides are updated in 6e04543.

Please also note that, in the old settings, docformatter wraps docstrings at 79 characters. If we change the docformatting settings to 88, then the summary lines can be automatically rewrapped to 88 characters, as mentioned in #962 (comment). However, ruff's pydocstyle rules can't wrap long lines and only raise warnings. So, ruff is not as powerful as docformatter. In other words, we may want to change docformatter line-length settings to 88 (as done in #962 (comment)) and let docformatter rewrap lines to 88 characters before completely removing it (Done in #2926).

Closes #962.

```
$ ruff check pygmt doc/conf.py examples --statistics
645	D200	[*] One-line docstring should fit on one line
312	D205	[ ] 1 blank line required between summary line and description
123	D400	[ ] First line should end with a period
 30	D401	[ ] First line of docstring should be in imperative mood: "A mock GMT API function that always returns a given value."
 30	D412	[*] No blank lines allowed between a section header and its content ("Examples")
 12	D202	[*] No blank lines allowed after function docstring (found 1)
```
@seisman seisman added the maintenance Boring but important stuff for the core devs label Dec 27, 2023
@seisman seisman added this to the 0.11.0 milestone Dec 27, 2023
@seisman seisman marked this pull request as ready for review January 2, 2024 01:16
@seisman seisman added the needs review This PR has higher priority and needs review. label Jan 2, 2024
@seisman seisman requested a review from a team January 2, 2024 15:46
@@ -445,7 +445,7 @@ def fmt_docstring(module_func):
- J = projection
- R = region
<BLANKLINE>
"""
""" # noqa: D410,D411
Copy link
Member

Choose a reason for hiding this comment

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

Since D410 (a blank line after section headings) is enabled in 5d983e6, this noqa: D410, D411 can be removed (and you'll need to run formatting after).

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this line is required. If removing noqa: D410, D411, the codes will be formatted with the following diff:

diff --git a/pygmt/helpers/decorators.py b/pygmt/helpers/decorators.py
index 57bd0f900..b44dd7fec 100644
--- a/pygmt/helpers/decorators.py
+++ b/pygmt/helpers/decorators.py
@@ -425,6 +425,7 @@ def fmt_docstring(module_func):
     <BLANKLINE>
     My nice module.
     <BLANKLINE>
+
     Parameters
     ----------
     data : str, numpy.ndarray, pandas.DataFrame, xarray.Dataset, or geo...
@@ -445,7 +446,7 @@ def fmt_docstring(module_func):
     - J = projection
     - R = region
     <BLANKLINE>
-    """  # noqa: D410,D411
+    """
     filler_text = {}

     if hasattr(module_func, "aliases"):

Then the doctest fails, because only one blank line is expected, but two blank lines are given (one is <BLANKLINE>, another one is the newly added blank line).

Copy link
Member

@weiji14 weiji14 Jan 4, 2024

Choose a reason for hiding this comment

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

We could just remove the <BLANKLINE> from L427 no? Oh wait, doesn't seem to work, hold on.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, looks like we'll need to keep it, since doctest takes an empty line to mean the end of the expected output according to https://docs.python.org/3/library/doctest.html#how-are-docstring-examples-recognized

pygmt/src/config.py Outdated Show resolved Hide resolved
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

Thanks again @seisman for all the work simplifying the code formatters to just ruff! Now make format runs in less than a second 🚀

@seisman seisman merged commit 3f9d14b into main Jan 4, 2024
19 checks passed
@seisman seisman deleted the ruff/pydocstyle branch January 4, 2024 01:10
@seisman seisman removed the needs review This PR has higher priority and needs review. label Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change the line length limit of docstrings to 88 characters
2 participants