-
-
Notifications
You must be signed in to change notification settings - Fork 228
add option to put docstrings on model attributes #1190
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 option to put docstrings on model attributes #1190
Conversation
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.
Could you use a smaller OpenAPI document for the snapshots on this one so there will be fewer changes in the future as we tweak things?
Probably just a single endpoint & model would be enough to demonstrate it
ec3a812
to
0c73307
Compare
@dbanty FYI, this was motivated partly by trying to build some HTML docs from a generated client with Sphinx, and finding that Sphinx really does not like how the current class docstrings are formatted. The format only really works if you view the strings as plain text; if it's interpreted either as Sphinx's default |
@dbanty Btw, I updated the PR description to clarify the behavior of attribute docstrings in Python. |
end_to_end_tests/docstrings-on-attributes-golden-record/my_test_api_client/client.py
Outdated
Show resolved
Hide resolved
80c799d
to
e980228
Compare
e980228
to
7c3427e
Compare
…1190) Adds a `docstrings_on_attributes` option that causes property descriptions to appear in docstrings on the attributes, instead of in the class docstring. This is a desirable behavior when using documentation generators like Sphinx. This is a non-breaking change; if the option is not set, code generation is exactly the same as before (evidence: the existing golden-record directories have no changes). ## Clarification about how docstrings on attributes work Python does not treat all docstrings the same, in terms of availability at runtime. A docstring on a class or method is available as a `__doc__` attribute— but a docstring on an attribute is not; such a string is just thrown away by the interpreter. If class `A` has attribute `b`, then `A.b.__doc__` will never be a thing— you would have to define `b` as a property getter method instead. However, docstrings on attributes are still valid as per [PEP 257](https://peps.python.org/pep-0257/) as a thing that can be surfaced by other tools. Sphinx _will_ use them when generating documentation, and VS Code _will_ show them as a description in the popup if you hover over an attribute.
Adds a
docstrings_on_attributes
option that causes property descriptions to appear in docstrings on the attributes, instead of in the class docstring. This is a desirable behavior when using documentation generators like Sphinx.This is a non-breaking change; if the option is not set, code generation is exactly the same as before (evidence: the existing golden-record directories have no changes).
Clarification about how docstrings on attributes work
Python does not treat all docstrings the same, in terms of availability at runtime. A docstring on a class or method is available as a
__doc__
attribute— but a docstring on an attribute is not; such a string is just thrown away by the interpreter. If classA
has attributeb
, thenA.b.__doc__
will never be a thing— you would have to defineb
as a property getter method instead.However, docstrings on attributes are still valid as per PEP 257 as a thing that can be surfaced by other tools. Sphinx will use them when generating documentation, and VS Code will show them as a description in the popup if you hover over an attribute.