Unify the type preprocessing logic in Napoleon #13146
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Subject: Unify the type preprocessing logic in Napoleon
Feature or Bugfix
Purpose
Allow Google-style docstrings to use the
optional
anddefault
keywords described at https://numpydoc.readthedocs.io/en/latest/format.html#parametersDetail
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
anddefault
and delimiter wordsor
,of
, andand
. This allows one to write in natural language, likeArray of int
instead ofArray[int]
orWidget, optional
instead ofOptional[Widget]
orWidget | None
. Numpy style is described in full at: https://numpydoc.readthedocs.io/en/latest/format.html#parametersThis 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.