Skip to content

Commit bbd84d1

Browse files
seismanweiji14
andauthored
Add docformatter to format plain text in docstrings (#642)
* Add docformatter to format docstrings following a subset of PEP 257 * Add --make-summary-multi-line * Install docformatter to make /format work * Sort dependencies alphabetically in environment.yml and requirements-dev.txt * Format all docstrings Co-authored-by: Wei Ji <[email protected]>
1 parent 9a5858f commit bbd84d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+789
-417
lines changed

.github/workflows/ci_tests.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ jobs:
3232

3333
- name: Install packages
3434
run: |
35-
pip install black blackdoc flake8 pylint isort
35+
pip install black blackdoc docformatter flake8 pylint isort
3636
sudo apt-get install dos2unix
3737
38-
- name: Formatting check (black and flake8)
38+
- name: Formatting check (black, blackdoc, docformatter, flake8 and isort)
3939
run: make check
4040

4141
- name: Linting (pylint)

.github/workflows/format-command.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
# Install formatting tools
2727
- name: Install formatting tools
2828
run: |
29-
pip install black blackdoc flake8 isort
29+
pip install black blackdoc docformatter flake8 isort
3030
sudo apt-get install dos2unix
3131
3232
# Run "make format" and commit the change to the PR branch

CONTRIBUTING.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ We use some tools:
249249

250250
- [Black](https://github.com/ambv/black)
251251
- [blackdoc](https://github.com/keewis/blackdoc)
252+
- [docformatter](https://github.com/myint/docformatter)
252253
- [isort](https://pycqa.github.io/isort/)
253254

254255
to format the code so we don't have to think about it.
255-
Black loosely follows the [PEP8](http://pep8.org) guide but with a few differences.
256+
Black and blackdoc loosely follows the [PEP8](http://pep8.org) guide but with a few differences.
256257
Regardless, you won't have to worry about formatting the code yourself.
257258
Before committing, run it to automatically format your code:
258259

@@ -273,7 +274,7 @@ common errors.
273274
The [`Makefile`](Makefile) contains rules for running both checks:
274275

275276
```bash
276-
make check # Runs flake8, black, blackdoc and isort (in check mode)
277+
make check # Runs black, blackdoc, docformatter, flake8 and isort (in check mode)
277278
make lint # Runs pylint, which is a bit slower
278279
```
279280

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ PYTEST_COV_ARGS=--cov=$(PROJECT) --cov-config=../pyproject.toml \
66
--pyargs ${PYTEST_EXTRA}
77
BLACK_FILES=$(PROJECT) setup.py doc/conf.py examples
88
BLACKDOC_OPTIONS=--line-length 79
9+
DOCFORMATTER_FILES=$(PROJECT) setup.py doc/conf.py examples
10+
DOCFORMATTER_OPTIONS=--recursive --pre-summary-newline --make-summary-multi-line --wrap-summaries 79 --wrap-descriptions 79
911
FLAKE8_FILES=$(PROJECT) setup.py doc/conf.py
1012
LINT_FILES=$(PROJECT) setup.py doc/conf.py
1113

@@ -14,8 +16,8 @@ help:
1416
@echo ""
1517
@echo " install install in editable mode"
1618
@echo " test run the test suite (including doctests) and report coverage"
17-
@echo " format run black and blackdoc to automatically format the code"
18-
@echo " check run code style and quality checks (black, blackdoc, isort and flake8)"
19+
@echo " format run black, blackdoc, docformatter and isort to automatically format the code"
20+
@echo " check run code style and quality checks (black, blackdoc, docformatter, flake8 and isort)"
1921
@echo " lint run pylint for a deeper (and slower) quality check"
2022
@echo " clean clean up build and generated files"
2123
@echo " distclean clean up build and generated files, including project metadata files"
@@ -37,11 +39,13 @@ test:
3739

3840
format:
3941
isort .
42+
docformatter --in-place $(DOCFORMATTER_OPTIONS) $(DOCFORMATTER_FILES)
4043
black $(BLACK_FILES)
4144
blackdoc $(BLACKDOC_OPTIONS) $(BLACK_FILES)
4245

4346
check:
4447
isort . --check
48+
docformatter --check $(DOCFORMATTER_OPTIONS) $(DOCFORMATTER_FILES)
4549
black --check $(BLACK_FILES)
4650
blackdoc --check $(BLACKDOC_OPTIONS) $(BLACK_FILES)
4751
flake8 $(FLAKE8_FILES)

environment.yml

+11-10
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ dependencies:
1111
- xarray
1212
- netCDF4
1313
- packaging
14-
- ipython
15-
- matplotlib
16-
- jupyter
17-
- pytest>=6.0
18-
- pytest-cov
19-
- pytest-mpl
20-
- coverage[toml]
2114
- black
2215
- blackdoc
16+
- coverage[toml]
17+
- docformatter
18+
- flake8
19+
- ipython
2320
- isort>=5
21+
- jupyter
22+
- matplotlib
23+
- nbsphinx
2424
- pylint
25-
- flake8
25+
- pytest-cov
26+
- pytest-mpl
27+
- pytest>=6.0
2628
- sphinx
27-
- sphinx_rtd_theme=0.4.3
2829
- sphinx-gallery
29-
- nbsphinx
30+
- sphinx_rtd_theme=0.4.3

pygmt/__init__.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def show_versions():
6767
import sys
6868

6969
def _get_module_version(modname):
70-
"""Get version information of a Python module."""
70+
"""
71+
Get version information of a Python module.
72+
"""
7173
try:
7274
if modname in sys.modules:
7375
module = sys.modules[modname]
@@ -82,7 +84,9 @@ def _get_module_version(modname):
8284
return None
8385

8486
def _get_ghostscript_version():
85-
"""Get ghostscript version."""
87+
"""
88+
Get ghostscript version.
89+
"""
8690
os_name = sys.platform
8791
if os_name.startswith("linux") or os_name == "darwin":
8892
cmds = ["gs"]
@@ -102,7 +106,9 @@ def _get_ghostscript_version():
102106
return None
103107

104108
def _get_gmt_version():
105-
"""Get GMT version."""
109+
"""
110+
Get GMT version.
111+
"""
106112
try:
107113
version = subprocess.check_output(
108114
["gmt", "--version"], universal_newlines=True
@@ -164,7 +170,6 @@ def test(doctest=True, verbose=True, coverage=False, figures=True):
164170
AssertionError
165171
If pytest returns a non-zero error code indicating that some tests have
166172
failed.
167-
168173
"""
169174
import pytest
170175

pygmt/base_plotting.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
Base class with plot generating commands.
3+
34
Does not define any special non-GMT methods (savefig, show, etc).
45
"""
56
import contextlib
@@ -52,7 +53,6 @@ def _preprocess(self, **kwargs): # pylint: disable=no-self-use
5253
>>> base = BasePlotting()
5354
>>> base._preprocess(resolution="low")
5455
{'resolution': 'low'}
55-
5656
"""
5757
return kwargs
5858

@@ -266,7 +266,6 @@ def colorbar(self, **kwargs):
266266
{XY}
267267
{p}
268268
{t}
269-
270269
"""
271270
kwargs = self._preprocess(**kwargs)
272271
with Session() as lib:
@@ -295,7 +294,7 @@ def colorbar(self, **kwargs):
295294
@kwargs_to_strings(R="sequence", L="sequence", A="sequence_plus", p="sequence")
296295
def grdcontour(self, grid, **kwargs):
297296
"""
298-
Convert grids or images to contours and plot them on maps
297+
Convert grids or images to contours and plot them on maps.
299298
300299
Takes a grid file name or an xarray.DataArray object as input.
301300
@@ -511,7 +510,6 @@ def grdimage(self, grid, **kwargs):
511510
{p}
512511
{t}
513512
{x}
514-
515513
"""
516514
kwargs = self._preprocess(**kwargs)
517515
kind = data_kind(grid, None, None)
@@ -624,7 +622,6 @@ def grdview(self, grid, **kwargs):
624622
{XY}
625623
{p}
626624
{t}
627-
628625
"""
629626
kwargs = self._preprocess(**kwargs)
630627
kind = data_kind(grid, None, None)
@@ -827,7 +824,6 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
827824
{t}
828825
*transparency* can also be a 1d array to set varying transparency
829826
for symbols.
830-
831827
"""
832828
kwargs = self._preprocess(**kwargs)
833829

@@ -900,7 +896,7 @@ def plot3d(
900896
self, x=None, y=None, z=None, data=None, sizes=None, direction=None, **kwargs
901897
):
902898
"""
903-
Plot lines, polygons, and symbols in 3-D
899+
Plot lines, polygons, and symbols in 3-D.
904900
905901
Takes a matrix, (x,y,z) triplets, or a file name as input and plots
906902
lines, polygons, or symbols at those locations in 3-D.
@@ -1010,7 +1006,6 @@ def plot3d(
10101006
{t}
10111007
*transparency* can also be a 1d array to set varying transparency
10121008
for symbols.
1013-
10141009
"""
10151010
kwargs = self._preprocess(**kwargs)
10161011

@@ -1132,7 +1127,6 @@ def contour(self, x=None, y=None, z=None, data=None, **kwargs):
11321127
{XY}
11331128
{p}
11341129
{t}
1135-
11361130
"""
11371131
kwargs = self._preprocess(**kwargs)
11381132

@@ -1208,7 +1202,6 @@ def basemap(self, **kwargs):
12081202
{XY}
12091203
{p}
12101204
{t}
1211-
12121205
"""
12131206
kwargs = self._preprocess(**kwargs)
12141207
if not ("B" in kwargs or "L" in kwargs or "Td" in kwargs or "Tm" in kwargs):
@@ -1267,7 +1260,6 @@ def logo(self, **kwargs):
12671260
{V}
12681261
{XY}
12691262
{t}
1270-
12711263
"""
12721264
kwargs = self._preprocess(**kwargs)
12731265
with Session() as lib:
@@ -1715,17 +1707,20 @@ def meca(
17151707
# pylint: disable=too-many-statements
17161708

17171709
def set_pointer(data_pointers, spec):
1718-
"""Set optional parameter pointers based on DataFrame or dict, if
1719-
those parameters are present in the DataFrame or dict."""
1710+
"""
1711+
Set optional parameter pointers based on DataFrame or dict, if
1712+
those parameters are present in the DataFrame or dict.
1713+
"""
17201714
for param in list(data_pointers.keys()):
17211715
if param in spec:
17221716
# set pointer based on param name
17231717
data_pointers[param] = spec[param]
17241718

17251719
def update_pointers(data_pointers):
1726-
"""Updates variables based on the location of data, as the
1727-
following data can be passed as parameters or it can be
1728-
contained in `spec`."""
1720+
"""
1721+
Updates variables based on the location of data, as the following
1722+
data can be passed as parameters or it can be contained in `spec`.
1723+
"""
17291724
# update all pointers
17301725
longitude = data_pointers["longitude"]
17311726
latitude = data_pointers["latitude"]

pygmt/clib/conversion.py

-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def dataarray_to_matrix(grid):
7979
[-150.5, -78.5, -80.5, -48.5]
8080
>>> print(inc)
8181
[2.0, 2.0]
82-
8382
"""
8483
if len(grid.dims) != 2:
8584
raise GMTInvalidInput(
@@ -158,7 +157,6 @@ def vectors_to_arrays(vectors):
158157
>>> data = [[1, 2], (3, 4), range(5, 7)]
159158
>>> all(isinstance(i, np.ndarray) for i in vectors_to_arrays(data))
160159
True
161-
162160
"""
163161
arrays = [as_c_contiguous(np.asarray(i)) for i in vectors]
164162
return arrays
@@ -200,7 +198,6 @@ def as_c_contiguous(array):
200198
True
201199
>>> as_c_contiguous(x).flags.c_contiguous
202200
True
203-
204201
"""
205202
if not array.flags.c_contiguous:
206203
return array.copy(order="C")
@@ -238,7 +235,6 @@ def kwargs_to_ctypes_array(argument, kwargs, dtype):
238235
... )
239236
>>> print(should_be_none)
240237
None
241-
242238
"""
243239
if argument in kwargs:
244240
return dtype(*kwargs[argument])

pygmt/clib/loading.py

-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def load_libgmt():
3030
GMTCLibNotFoundError
3131
If there was any problem loading the library (couldn't find it or
3232
couldn't access the functions).
33-
3433
"""
3534
lib_fullnames = clib_full_names()
3635
error = True
@@ -64,7 +63,6 @@ def clib_names(os_name):
6463
-------
6564
libnames : list of str
6665
List of possible names of GMT's shared library.
67-
6866
"""
6967
if os_name.startswith("linux"):
7068
libnames = ["libgmt.so"]
@@ -93,7 +91,6 @@ def clib_full_names(env=None):
9391
-------
9492
lib_fullnames: list of str
9593
List of possible full names of GMT's shared library.
96-
9794
"""
9895
if env is None:
9996
env = os.environ
@@ -127,7 +124,6 @@ def check_libgmt(libgmt):
127124
Raises
128125
------
129126
GMTCLibError
130-
131127
"""
132128
# Check if a few of the functions we need are in the library
133129
functions = ["Create_Session", "Get_Enum", "Call_Module", "Destroy_Session"]

0 commit comments

Comments
 (0)