-
Notifications
You must be signed in to change notification settings - Fork 38
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
Extract and dynamically re-inject base client sphinx param doc into client classes, for better sphinx and runtime doc #1131
Open
sirosen
wants to merge
1
commit into
globus:main
Choose a base branch
from
sirosen:better-client-param-doc
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 all commits
Commits
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
5 changes: 5 additions & 0 deletions
5
changelog.d/20250118_122417_sirosen_better_client_param_doc.rst
This file contains 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,5 @@ | ||
Added | ||
~~~~~ | ||
|
||
- Most client classes now have their ``__doc__`` attribute modified at runtime | ||
to provide better ``help()`` and sphinx documentation. (:pr:`NUMBER`) |
This file contains 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
|
||
import collections | ||
import collections.abc | ||
import functools | ||
import hashlib | ||
import os | ||
import platform | ||
import sys | ||
import textwrap | ||
import typing as t | ||
import uuid | ||
from base64 import b64encode | ||
|
@@ -276,3 +278,87 @@ def classproperty(func: t.Callable[[T], R]) -> _classproperty[T, R]: | |
def classproperty(func: t.Callable[[T], R]) -> _classproperty[T, R]: | ||
# type cast to convert instance method to class method | ||
return _classproperty(t.cast(t.Callable[[t.Type[T]], R], func)) | ||
|
||
|
||
@functools.lru_cache(maxsize=None) | ||
def read_sphinx_params(docstring: str) -> tuple[str, ...]: | ||
""" | ||
Given a docstring, extract the `:param:` declarations into a tuple of strings. | ||
|
||
:param docstring: The ``__doc__`` to parse, as it appeared on the original object | ||
|
||
Params start with `:param ...` and end | ||
- at the end of the string | ||
- at the next param | ||
- when a non-indented, non-param line is found | ||
|
||
Whitespace lines within a param doc are supported. | ||
|
||
All produced param strings are dedented. | ||
""" | ||
docstring = textwrap.dedent(docstring) | ||
|
||
result: list[str] = [] | ||
current: list[str] = [] | ||
for line in docstring.splitlines(): | ||
if not current: | ||
if line.startswith(":param"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surely sphinx loads these as a part of its docstring parsing these. Do we have any mechanism to tap into that in a plugin instead of doing it ourselves? |
||
current = [line] | ||
else: | ||
continue | ||
else: | ||
# a new param -- flush the current one and restart | ||
if line.startswith(":param"): | ||
result.append("\n".join(current).strip()) | ||
current = [line] | ||
# a continuation line for the current param (indented) | ||
# or a blank line -- it *could* be a continuation of param doc (include it) | ||
elif line != line.lstrip() or not line: | ||
current.append(line) | ||
# otherwise this is a populated line, not indented, and without a `:param` | ||
# start -- stop looking for more param doc | ||
else: | ||
break | ||
if current: | ||
result.append("\n".join(current).strip()) | ||
|
||
return tuple(result) | ||
|
||
|
||
def inject_sphinx_params(doc_params: tuple[str, ...], target: t.Any) -> None: | ||
""" | ||
Given a tuple of sphinx doc params, and a target object with a `__doc__` attribute, | ||
inject the params to replace the marker comment 'globus-sdk-inject-doc-params'. | ||
|
||
:param doc_params: Params, as parsed by ``read_sphinx_params`` | ||
:param target: The object where `__doc__` is being updated | ||
(usually a class or function) | ||
""" | ||
target_doc_lines = target.__doc__.splitlines() | ||
|
||
result = [] | ||
for line in target_doc_lines: | ||
stripped_line = line.strip() | ||
if stripped_line == ".. globus-sdk-inject-doc-params": | ||
indent = len(line) - len(stripped_line) | ||
result.append(textwrap.indent("\n".join(doc_params), " " * indent)) | ||
else: | ||
result.append(line) | ||
|
||
target.__doc__ = "\n".join(result) | ||
|
||
|
||
def inject_sphinx_params_of(source: t.Any) -> t.Callable[[T], T]: | ||
""" | ||
Read params from a source object, and inject them into a destination object. | ||
|
||
:param source: An object whose ``__doc__`` attribute is being parsed for | ||
parameter docs | ||
""" | ||
params = read_sphinx_params(source.__doc__) | ||
|
||
def apply(target: T) -> T: | ||
inject_sphinx_params(params, target) | ||
return target | ||
|
||
return apply |
This file contains 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
Oops, something went wrong.
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.
Is there a way to do this without the annotation by looking at the client class's mro instead of annotating?