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

Add docformatter to format plain text in docstrings #642

Merged
merged 10 commits into from
Jan 21, 2021

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 4, 2020

Description of proposed changes

Add docformatter to format plaintext in docstrings, as mentioned in #497.

docformatter follows PEP 257, which is compatible with numpydoc style.

EDIT: There may be some incompatibility. The new documentation will look messy if --pre-summary-newline is not used.

Preview the documentation: https://pygmt-git-docstring-formatter.gmt.vercel.app/

Fixes #497.

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Makefile Outdated Show resolved Hide resolved
@seisman seisman marked this pull request as draft October 4, 2020 21:26
@seisman seisman force-pushed the docstring-formatter branch from bfbe503 to 3589c88 Compare October 4, 2020 21:57
pygmt/_version.py Outdated Show resolved Hide resolved
pygmt/tests/test_grdcut.py Outdated Show resolved Hide resolved
@seisman seisman marked this pull request as ready for review October 4, 2020 22:32
@seisman seisman added the maintenance Boring but important stuff for the core devs label Oct 4, 2020
@seisman seisman added this to the 0.2.1 milestone Oct 4, 2020
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.

Mixed feelings about using docformatter here. There are some aspects of PEP 257 that are nice, while others seem strange (though it might just require some getting used to). As you mentioned too at #642 (comment), the version of docformatter (1.3.1) from PyPI is somewhat out of date (11 Nov 2019) and there's features in the master branch which would be nice to have!

Also a bit wary about adding yet another lint tool into our toolbox since it might put off new contributors. The intention is good (making it easier to style code), but we'll need to remind ourselves to put code before formatting (as mentioned in https://github.com/GenericMappingTools/pygmt/blob/master/MAINTENANCE.md#reviewing-and-merging-pull-requests).

Comment on lines 337 to +347
Callback function that the GMT C API will use to print log and
error messages. We'll capture the messages and print them to stderr
so that they will show up on the Jupyter notebook.
error messages.

We'll capture the messages and print them to stderr so that they
will show up on the Jupyter notebook.
Copy link
Member

Choose a reason for hiding this comment

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

So docformatter splits things up here, the first sentence is a 'summary', and the other sentences become a 'description'. Interesting.

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 think "summary" can be only one sentence, so the "summary" and "description" is separated at the first "period".

Comment on lines 23 to 22
"""
Called by sphinx-gallery to save the figures generated after running
code.
"""
"""Called by sphinx-gallery to save the figures generated after running
code."""
Copy link
Member

Choose a reason for hiding this comment

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

This looks funny too, why doesn't docformatter put a newline here? Seems to be raised before at PyCQA/docformatter#9.

Copy link
Member

Choose a reason for hiding this comment

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

Once PyCQA/docformatter#49 is merged (and a docformatter release is made), then we can rerender things and hopefully get a smaller diff.

Copy link
Member

Choose a reason for hiding this comment

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

Docformatter 1.4 has been released at https://pypi.org/project/docformatter/1.4/ so we can try to finish up this PR.

@seisman
Copy link
Member Author

seisman commented Oct 4, 2020

As you mentioned too at #642 (comment), the version of docformatter (1.3.1) from PyPI is somewhat out of date (11 Nov 2019) and there's features in the master branch which would be nice to have!

Perhaps we can ask if they could have a new release.

Also a bit wary about adding yet another lint tool into our toolbox since it might put off new contributors.

It seems straightforward to support slash commands like /format using slash-command-dispatch (see a demo at peter-evans/slash-command-dispatch#28). Then contributors and even core developer won't need to run make format locally.

@weiji14
Copy link
Member

weiji14 commented Oct 5, 2020

Perhaps we can ask if they could have a new release.

It's been asked at PyCQA/docformatter#55.

It seems straightforward to support slash commands like /format using slash-command-dispatch (see a demo at peter-evans/slash-command-dispatch#28). Then contributors and even core developer won't need to run make format locally.

Ah that does look neat!

@seisman
Copy link
Member Author

seisman commented Oct 5, 2020

Perhaps we can ask if they could have a new release.

It's been asked at myint/docformatter#55.

Let's keep this PR open for a while.

@seisman seisman marked this pull request as draft October 5, 2020 02:18
@seisman seisman modified the milestones: 0.2.1, 0.3.0 Nov 6, 2020
@seisman seisman mentioned this pull request Nov 6, 2020
@seisman seisman force-pushed the docstring-formatter branch from 096ac84 to 2557e8f Compare November 23, 2020 03:33
@seisman seisman force-pushed the docstring-formatter branch from a7b6f3d to d9822c2 Compare December 30, 2020 02:57
@seisman seisman force-pushed the docstring-formatter branch from d9822c2 to 0fcfb24 Compare December 30, 2020 02:59
Makefile Outdated
@@ -6,6 +6,8 @@ PYTEST_COV_ARGS=--cov=$(PROJECT) --cov-config=../pyproject.toml \
--pyargs ${PYTEST_EXTRA}
BLACK_FILES=$(PROJECT) setup.py doc/conf.py examples
BLACKDOC_OPTIONS=--line-length 79
DOCFORMATTER_FILES=$(PROJECT) setup.py doc/conf.py examples
DOCFORMATTER_OPTIONS=--recursive --pre-summary-newline --wrap-summaries 79 --wrap-descriptions 79
Copy link
Member

@weiji14 weiji14 Jan 20, 2021

Choose a reason for hiding this comment

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

Suggested change
DOCFORMATTER_OPTIONS=--recursive --pre-summary-newline --wrap-summaries 79 --wrap-descriptions 79
DOCFORMATTER_OPTIONS=--recursive --pre-summary-newline --make-summary-multi-line --wrap-summaries 79 --wrap-descriptions 79

Do we want to force --make-summary-multi-line (see https://github.com/myint/docformatter/tree/v1.4#options)? This will turn every description (even short ones) into:

"""
This is a description.
"""

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it looks cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just find that PEP 257 says:

Unless the entire docstring fits on a line, place the closing quotes on a line by themselves.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, seems like docformatter didn't implement that one-line docstring PEP 257 guideline correctly? I think it's ok to just force multi-line though, as long as there's an automated solution.

@weiji14
Copy link
Member

weiji14 commented Jan 21, 2021

/format

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.

Just one nitpick on alphabetically ordering, otherwise should be good to merge once the Style Checks tests pass.

Edit: /format command at #642 (comment) didn't work because we need to install docformatter at this line too:

pip install black blackdoc flake8 isort

.github/workflows/ci_tests.yaml Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
environment.yml Show resolved Hide resolved
requirements-dev.txt Show resolved Hide resolved
@seisman
Copy link
Member Author

seisman commented Jan 21, 2021

/format

@weiji14
Copy link
Member

weiji14 commented Jan 21, 2021

/format

Might need to do it manually locally for now, the Github Action is using the workflow yaml file from the master branch.

pygmt/tests/test_figure.py Outdated Show resolved Hide resolved
@weiji14 weiji14 marked this pull request as ready for review January 21, 2021 08:11
@weiji14 weiji14 merged commit bbd84d1 into master Jan 21, 2021
@weiji14 weiji14 deleted the docstring-formatter branch January 21, 2021 08:34
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 2022
…ols#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]>
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.

Add blackdoc and docformatter to wrap docstrings to 79 chars
2 participants