Skip to content

Commit 3f9d14b

Browse files
seismanweiji14
andauthored
Enable ruff's pydocstyle (D) rules and remove docformatter (#2925)
* Enable ruff's pydocstyle (D) rules and set numpy convention * Fix D406: Section name should end with a newline ('Notes') * Fix D405L: Section name should be properly capitalized ('See also') * Fix D104 Missing docstring in public package * Fix D409: Section underline should match the length of its name * Ignore a D410/D411 violation * Fix D103: Missing docstring in public function * Fix D105: Missing docstring in magic method * Fix a formatting issue * Ignore some D rules ``` $ 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) ``` * Enabel D213 and D410 rules * Fully remove docformatter * Update the contributing guides --------- Co-authored-by: Wei Ji <[email protected]>
1 parent fbfed86 commit 3f9d14b

18 files changed

+64
-39
lines changed

.github/workflows/format-command.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
# Install formatting tools
3333
- name: Install formatting tools
3434
run: |
35-
python -m pip install docformatter ruff
35+
python -m pip install ruff
3636
python -m pip list
3737
sudo apt-get install dos2unix
3838

.github/workflows/style_checks.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ jobs:
3434

3535
- name: Install packages
3636
run: |
37-
python -m pip install docformatter ruff
37+
python -m pip install ruff
3838
python -m pip list
3939
sudo apt-get install dos2unix
4040
41-
- name: Formatting check (docformatter, ruff)
41+
- name: Formatting check (ruff)
4242
run: make check
4343

4444
- name: Ensure files use UNIX line breaks and have 644 permission

Makefile

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ help:
1515
@echo " fulltest run the test suite (including all doctests)"
1616
@echo " doctest run the doctests only"
1717
@echo " test_no_images run the test suite (including all doctests) but skip image comparisons"
18-
@echo " format run docformatter and ruff to automatically format the code"
19-
@echo " check run code style and quality checks (docformatter and ruff)"
18+
@echo " format run ruff to automatically format the code"
19+
@echo " check run ruff to check code style and quality"
2020
@echo " codespell run codespell to check common misspellings"
2121
@echo " typecheck run mypy for static type check"
2222
@echo " clean clean up build and generated files"
@@ -60,12 +60,10 @@ test_no_images: PYTEST_ARGS=-o addopts="--verbose --durations=0 --durations-min=
6060
test_no_images: _runtest
6161

6262
format:
63-
docformatter --in-place $(FORMAT_FILES)
6463
ruff check --fix $(FORMAT_FILES)
6564
ruff format $(FORMAT_FILES)
6665

6766
check:
68-
docformatter --check $(FORMAT_FILES)
6967
ruff check $(FORMAT_FILES)
7068
ruff format --check $(FORMAT_FILES)
7169

doc/contributing.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ arguments and return values.
406406

407407
While the maximum line length for code is automatically set by ruff, docstrings
408408
must be formatted manually. To play nicely with Jupyter and IPython, **keep docstrings
409-
limited to 79 characters** per line.
409+
limited to 88 characters** per line.
410410

411411
### Standards for Example Code
412412

@@ -471,14 +471,10 @@ code, be sure to follow the general guidelines in the
471471

472472
### Code Style
473473

474-
We use some tools to format the code so we don't have to think about it:
475-
476-
- [docformatter](https://github.com/myint/docformatter)
477-
- [ruff](https://docs.astral.sh/ruff)
478-
479-
These tools loosely follow the [PEP8](http://pep8.org) guide but with a few
480-
differences. Regardless, you won't have to worry about formatting the code yourself.
481-
Before committing, run it to automatically format your code:
474+
We use the [ruff](https://docs.astral.sh/ruff) tool to format the code, so we
475+
don't have to think about it. It loosely follow the [PEP8](http://pep8.org) guide
476+
but with a few differences. Regardless, you won't have to worry about formatting
477+
the code yourself. Before committing, run it to automatically format your code:
482478

483479
```bash
484480
make format
@@ -511,7 +507,7 @@ The [`Makefile`](https://github.com/GenericMappingTools/pygmt/blob/main/Makefile
511507
contains rules for running the linter checks:
512508

513509
```bash
514-
make check # Runs docformatter and ruff (in check mode)
510+
make check # Runs ruff in check mode
515511
```
516512

517513
### Testing your Code

environment.yml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ dependencies:
2424
- pip
2525
# Dev dependencies (style checks)
2626
- codespell
27-
- docformatter>=1.7.2
2827
- ruff>=0.1.9
2928
# Dev dependencies (unit testing)
3029
- matplotlib

examples/gallery/3d_plots/grdview_surface.py

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
# Define an interesting function of two variables, see:
2424
# https://en.wikipedia.org/wiki/Ackley_function
2525
def ackley(x, y):
26+
"""
27+
Ackley function.
28+
"""
2629
return (
2730
-20 * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2)))
2831
- np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))

examples/gallery/lines/vector_styles.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
55
The :meth:`pygmt.Figure.plot` method can plot Cartesian, circular, and
66
geographic vectors. The ``style`` parameter controls vector attributes.
7-
See also
8-
:doc:`Vector attributes example </gallery/lines/vector_heads_tails>`.
9-
7+
See also :doc:`Vector attributes example </gallery/lines/vector_heads_tails>`.
108
"""
119

1210
# %%

examples/tutorials/advanced/working_with_panel.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@
7373
)
7474

7575

76-
# Define a function for plotting the single slices
7776
@pn.depends(central_lon=slider_lon)
7877
def view(central_lon):
78+
"""
79+
Define a function for plotting the single slices.
80+
"""
7981
# Create a new instance or object of the pygmt.Figure() class
8082
fig = pygmt.Figure()
8183
fig.coast(
@@ -112,9 +114,11 @@ def view(central_lon):
112114
)
113115

114116

115-
# Define a function for plotting the single slices
116117
@pn.depends(central_lon=slider_lon)
117118
def view(central_lon):
119+
"""
120+
Define a function for plotting the single slices.
121+
"""
118122
# Create a new instance or object of the pygmt.Figure() class
119123
fig = pygmt.Figure()
120124
# Set up a colormap for the elevation in meters

pygmt/figure.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def __init__(self):
8888
self._activate_figure()
8989

9090
def __del__(self):
91-
# Clean up the temporary directory that stores the previews
91+
"""
92+
Clean up the temporary directory that stores the previews.
93+
"""
9294
if hasattr(self, "_preview_dir"):
9395
self._preview_dir.cleanup()
9496

pygmt/helpers/decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def fmt_docstring(module_func):
445445
- J = projection
446446
- R = region
447447
<BLANKLINE>
448-
"""
448+
""" # noqa: D410,D411
449449
filler_text = {}
450450

451451
if hasattr(module_func, "aliases"):

pygmt/helpers/tempfile.py

+6
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ def __init__(self, prefix="pygmt-", suffix=".txt"):
6262
self.name = tmpfile.name
6363

6464
def __enter__(self):
65+
"""
66+
Do nothing but return the object.
67+
"""
6568
return self
6669

6770
def __exit__(self, *args):
71+
"""
72+
Remove the temporary file.
73+
"""
6874
if os.path.exists(self.name):
6975
os.remove(self.name)
7076

pygmt/helpers/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def args_in_kwargs(args, kwargs):
529529
short-form aliases of the parameters.
530530
531531
Returns
532-
--------
532+
-------
533533
bool
534534
If one of the required arguments is in ``kwargs``.
535535

pygmt/src/config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,15 @@ def __init__(self, **kwargs):
203203
lib.call_module(module="set", args=arg_str)
204204

205205
def __enter__(self):
206+
"""
207+
Do nothing but return the object.
208+
"""
206209
return self
207210

208211
def __exit__(self, exc_type, exc_value, traceback):
209-
# revert to initial values
212+
"""
213+
Revert GMT configurations to initial values.
214+
"""
210215
arg_str = " ".join(
211216
[f'{key}="{value}"' for key, value in self.old_defaults.items()]
212217
)

pygmt/src/grdhisteq.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _grdhisteq(grid, output_type, **kwargs):
102102
``outgrid`` or ``outfile``)
103103
104104
See Also
105-
-------
105+
--------
106106
:func:`pygmt.grd2cpt`
107107
"""
108108

@@ -194,7 +194,7 @@ def equalize_grid(
194194
>>> grid = pygmt.grdhisteq.equalize_grid(grid=grid, gaussian=True)
195195
196196
See Also
197-
-------
197+
--------
198198
:func:`pygmt.grd2cpt`
199199
200200
Note
@@ -306,7 +306,7 @@ def compute_bins(
306306
4 705.0 2275.5
307307
308308
See Also
309-
-------
309+
--------
310310
:func:`pygmt.grd2cpt`
311311
312312
Note

pygmt/src/grdtrack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs):
209209
- **+c**\ *fact* : Compute envelope on stacked profile as
210210
±\ *fact* \*\ *deviation* [Default fact value is 2].
211211
212-
Notes:
212+
Here are some notes:
213213
214214
1. Deviations depend on *method* and are st.dev (**a**), L1 scale,
215215
i.e., 1.4826 \* median absolute deviation (MAD) (for **m** and

pygmt/tests/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
PyGMT test suite.
3+
"""

pygmt/tests/test_clib_loading.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def __init__(self, name):
2424
self._name = name
2525

2626
def __str__(self):
27+
"""
28+
String representation of the object.
29+
"""
2730
return self._name
2831

2932

pyproject.toml

+16-8
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ ignore-words-list = "astroid,oints,reenable,tripel,trough"
7575
[tool.coverage.run]
7676
omit = ["*/tests/*", "*pygmt/__init__.py"]
7777

78-
[tool.docformatter]
79-
black = true
80-
recursive = true
81-
pre-summary-newline = true
82-
make-summary-multi-line = true
83-
wrap-summaries = 88
84-
wrap-descriptions = 88
85-
8678
[tool.mypy]
8779
exclude = ["pygmt/tests/"]
8880
ignore_missing_imports = true
@@ -103,6 +95,7 @@ select = [
10395
"B", # flake8-bugbear
10496
"BLE", # flake8-blind-except
10597
"C4", # flake8-comprehensions
98+
"D", # pydocstyle
10699
"E", # pycodestyle
107100
"EXE", # flake8-executable
108101
"F", # pyflakes
@@ -131,7 +124,17 @@ select = [
131124
"W", # pycodestyle warnings
132125
"YTT", # flake8-2020
133126
]
127+
extend-select = [
128+
"D213", # Summary lines should be positioned on the second physical line of the docstring.
129+
"D410", # A blank line after section headings.
130+
]
134131
ignore = [
132+
"D200", # One-line docstring should fit on one line
133+
"D202", # No blank lines allowed after function docstring
134+
"D205", # 1 blank line required between summary line and description
135+
"D400", # First line should end with a period
136+
"D401", # First line of docstring should be in imperative mood
137+
"D412", # No blank lines allowed between a section header and its content
135138
"E501", # Avoid enforcing line-length violations
136139
"ISC001", # Single-line-implicit-string-concatenation, conflict with formatter
137140
"PD901", # Allow using the generic variable name `df` for DataFrames
@@ -153,6 +156,11 @@ known-third-party = ["pygmt"]
153156
[tool.ruff.lint.pycodestyle]
154157
max-doc-length = 88
155158

159+
[tool.ruff.lint.pydocstyle]
160+
# See https://docs.astral.sh/ruff/faq/#does-ruff-support-numpy-or-google-style-docstrings
161+
# for the enabled/disabled rules for the "numpy" convention.
162+
convention = "numpy"
163+
156164
[tool.ruff.lint.pylint]
157165
max-args=10
158166

0 commit comments

Comments
 (0)