eng: add option to put docstrings on model attributes BNCH-114718#225
Uh oh!
There was an error while loading. Please reload this page.
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.
This PR is for the Benchling fork. There is an equivalent upstream PR, openapi-generators#1190 - see the description there for more details.
The implementation here is exactly equivalent to the upstream PR, so will not change if that one is accepted. There is a difference in the test code: this PR uses the "functional tests" mechanism that exists on our fork, rather than the "golden record" type of test which is the only way to test it upstream.
Why we need this
The tool's current behavior is to make a docstring for each model class that combines the schema-level description with a list of all the properties and their types and descriptions. It ends up looking like this:
That's nice enough if you're just looking directly at the source code— but it's pretty bad if you plan to use a documentation tool like Sphinx, as we do. The use of line breaks and indents in the "Attributes:" section confuses these tools, if they try to interpret it as any kind of markup— it's not valid in Sphinx's
.rstformat, and it's not valid in Markdown either. So you end up seeing something like this:Plus, as you can see in the code example, putting the type signatures of the attributes into the docstring is redundant: they already have type hints in the code, which documentation tools and IDEs already know how to parse.
So, by using the new option in this PR, we can make the code look like this instead:
This renders nicely in Sphinx, and the attribute descriptions also show up if you hover over an attribute in VS Code.
(The way we used to deal with this in our old SDK was somewhat different: we had heavily customized the generator's template for model classes, so that instead of just declaring attributes and letting
attrsauto-generate everything, we were creating custom getter methods and putting docstrings on those. We'd like to avoid doing anything like that in the future; making custom versions of complex generator templates made our code very hard to maintain.)