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

Unify the type preprocessing logic in Napoleon #13146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cbarrick
Copy link

@cbarrick cbarrick commented Nov 20, 2024

Subject: Unify the type preprocessing logic in Napoleon

Feature or Bugfix

  • Feature

Purpose

Allow Google-style docstrings to use the optional and default keywords described at https://numpydoc.readthedocs.io/en/latest/format.html#parameters

Detail

Previously, there were two separate type preprocessing functions: _convert_type_spec (used in Google-style docstrings) and _convert_numpy_type_spec (used in Numpy-style docstrings).

The Google version simply applied type-alias translations or wrapped the text in a :py:class: role.

The Numpy version does the same, plus adds special handling for keywords optional and default and delimiter words or, of, and and. This allows one to write in natural language, like Array of int instead of Array[int] or Widget, optional instead of Optional[Widget] or Widget | None. Numpy style is described in full at: https://numpydoc.readthedocs.io/en/latest/format.html#parameters

This commit eliminates the distinction and allows Google-style docstrings to use these preprocessing rules.

More details

The Numpy-style preprocessing code lived between the GoogleDocstring and NumpyDocstring classes. This was kinda an awkward location when the code would become shared by both classes.

This PR is broken into two commits. The first moves the code to a more palatable location. The second migrates GoogleDocstring to use the previously-named _convert_numpy_type_spec.

I think it will be much easier to review the diffs individually rather than the diff for the whole PR.

@cbarrick cbarrick force-pushed the napoleon-preprocess branch 5 times, most recently from 8856704 to 223a7cb Compare November 20, 2024 02:23
This puts all preprocessing code above both docstring classes, rather
than in between. This is in preparation to making both docstring classes
share the same preprocessing.
Previously, there were two type preprocessing functions:
`_convert_type_spec` (used in Google-style docstrings) and
`_convert_numpy_type_spec` (used in Numpy-style docstrings).

The Google version simply applied type-alias translations or wrapped
the text in a `:py:class:` role.

The Numpy version does the same, plus adds special handling for keywords
`optional` and `default` and delimiter words `or`, `of`, and `and`. This
allows one to write in natural language, like `Array of int` instead of
`Array[int]` or `Widget, optional` instead of `Optional[Widget]` or
`Widget | None`. Numpy style is described in full at:
https://numpydoc.readthedocs.io/en/latest/format.html#parameters

This commit eliminates the distinction and allows Google-style
docstrings to use these preprocessing rules.
@cbarrick cbarrick force-pushed the napoleon-preprocess branch from 223a7cb to 8f2a345 Compare November 24, 2024 00:40
@cbarrick
Copy link
Author

Friendly ping!

Any concerns with this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants