-
Notifications
You must be signed in to change notification settings - Fork 24
DM-54027: add docs on writing docs with type annotations #737
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
Open
TallJimbo
wants to merge
5
commits into
main
Choose a base branch
from
tickets/DM-54027
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ef07e6
Make the note on Sphinx Python name resolution more prominent.
TallJimbo bb20124
Add section on docs with type annotations.
TallJimbo 1f76c5d
Tuple returns should use types, not labels.
TallJimbo 8112aed
Add recommendations on documenting generics.
TallJimbo 98613b8
Fix some unnecessary line breaks.
TallJimbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| .. _stack-documentation-code-with-type-annotations: | ||
|
|
||
| ################################ | ||
| Documenting code with type hints | ||
| ################################ | ||
|
|
||
| Many DM packages (especially the middleware suite) use type hints for static analysis, which often duplicates the type information included in docstrings. | ||
| Documentation built with `Documenteer 2.x`_ can often leave this information out, because the `sphinx-autodoc-typehints`_ extension (included automatically) will parse the annotations and include type information in the docs automatically. | ||
|
|
||
| .. note:: | ||
| `pipelines.lsst.io`_ is currently still built with Documenteer 1.x, but is expected to transition soon. | ||
| While some :ref:`package doc builds <build-package-docs>` have already been upgraded in anticipation of this transition, their documentation content needs to remain compatible with Documenteer 1.x for now. | ||
|
|
||
| Function arguments | ||
| ------------------ | ||
|
|
||
| To document the parameters to a function or method declared with type hints, | ||
| use regular numpydoc style without the colon or the type information that follows it:: | ||
|
|
||
| def run_thing(self, x: int, *args: int, name: str = "", **kwargs: str) -> None: | ||
| """Run the thing. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x | ||
| X coordinate. | ||
| *args | ||
| Some other coordinates. | ||
| name | ||
| The name of the thing. | ||
| **kwargs | ||
| Names of other things. | ||
| """ | ||
|
|
||
| Note that ``, optional`` is also unnecessary, as are defaults; default values are automatically pulled from the real function signature. | ||
|
|
||
| Function return values | ||
| ---------------------- | ||
|
|
||
| Return types work automatically when they are not documented at all:: | ||
|
|
||
| def return_it() -> str: | ||
| """Return the thing.""" | ||
| return "" | ||
|
|
||
| This is a reasonable approach when there is nothing else to document about the returned object. | ||
| When the returned object does merit additional documentation, the type does unfortunately need to be written out (duplicating the annotation), but the returned object should not be named:: | ||
|
|
||
| def return_it() -> str: | ||
| """Return the thing. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| The thing. | ||
| """ | ||
| return "" | ||
|
|
||
| A simple return type does not need backticks to create a link, but backticks may be needed for more complex types (e.g. generics):: | ||
|
|
||
| from collections.abc import Sequence | ||
|
|
||
| def return_stuff() -> Sequence[str]: | ||
| """Return some stuff. | ||
|
|
||
| Returns | ||
| ------- | ||
| `~collections.abc.Sequence` [`str`] | ||
| The stuff. | ||
| """ | ||
| return [] | ||
|
|
||
| .. note:: | ||
| As always, types in docstrings do *not* respect imports in the file, and instead are resolved using the `Sphinx target-resolution rules`_. | ||
| See :ref:`rst-python-link` for details. | ||
|
|
||
| Functions that return multiple values via a tuple should have their return types documented just like parameters, i.e. with labels and no types:: | ||
|
|
||
| def return_pair() -> tuple[str, int]: | ||
| """Return a pair. | ||
|
|
||
| Returns | ||
| ------- | ||
| name | ||
| The name. | ||
| id | ||
| The ID. | ||
| """ | ||
| return ("", 0) | ||
|
|
||
| Properties and attributes | ||
| ------------------------- | ||
|
|
||
| Annotations on properties and attributes are not applied to documentation automatically. | ||
| Their docstrings should continue to include the types parenthetically:: | ||
|
|
||
| class Thing: | ||
| """A thing.""" | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Name of the thing (`str`).""" | ||
| return "" | ||
|
|
||
| value: int = 0 | ||
| """Value of the thing (`int`).""" | ||
|
|
||
| .. note:: | ||
| Attributes without default values (or some sort of ``= RHS``) are not | ||
| included in documentation *at all*, except for those on `~dataclasses.dataclass` types. | ||
| Important instance attributes that cannot have a class-level default value should be made into properties so they can be documented. | ||
|
|
||
|
|
||
| .. _`Documenteer 2.x`: https://documenteer.lsst.io | ||
| .. _`sphinx-autodoc-typehints`: https://pypi.org/project/sphinx-autodoc-typehints/ | ||
| .. _`pipelines.lsst.io`: https://pipelines.lsst.io | ||
| .. _`Sphinx target-resolution rules`: <https://www.sphinx-doc.org/en/master/usage/domains/python.html#target-resolution>` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a really significant recommendation to be making about Python code style for something that seems like a limitation in the documentation build system, but I also don't really see that we have a much of a choice; not documenting public attributes doesn't really seem like a viable option, either.