-
Notifications
You must be signed in to change notification settings - Fork 1
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
eng: add option to put docstrings on model attributes BNCH-114718 #225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,31 @@ from attrs import define, field, evolve | |
import httpx | ||
|
||
|
||
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. The maintainer pointed out on the upstream PR that there was a similar docstring issue in the generated client classes. So I updated this template in the upstream PR & also in this one. As with my other changes, these result in exactly the same code if you do not have the new option enabled - the proof of that is that I didn't need to make any changes in the "golden record" code under 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. You can see what the generated client class looks like when the new option is set here - because, in the upstream PR, I did include the new feature in the "golden record"-based tests (since there are no functional tests on that branch). |
||
{% set attrs_info = { | ||
"raise_on_unexpected_status": namespace( | ||
type="bool", | ||
default="field(default=False, kw_only=True)", | ||
docstring="Whether or not to raise an errors.UnexpectedStatus if the API returns a status code" | ||
" that was not documented in the source OpenAPI document. Can also be provided as a keyword" | ||
" argument to the constructor." | ||
), | ||
"token": namespace(type="str", default="", docstring="The token to use for authentication"), | ||
"prefix": namespace(type="str", default='"Bearer"', docstring="The prefix to use for the Authorization header"), | ||
"auth_header_name": namespace(type="str", default='"Authorization"', docstring="The name of the Authorization header"), | ||
} %} | ||
|
||
{% macro attr_in_class_docstring(name) %} | ||
{{ name }}: {{ attrs_info[name].docstring }} | ||
{%- endmacro %} | ||
|
||
{% macro declare_attr(name) %} | ||
{% set attr = attrs_info[name] %} | ||
{{ name }}: {{ attr.type }}{% if attr.default %} = {{ attr.default }}{% endif %} | ||
{% if attr.docstring and config.docstrings_on_attributes +%} | ||
"""{{ attr.docstring }}""" | ||
{%- endif %} | ||
{% endmacro %} | ||
|
||
@define | ||
class Client: | ||
"""A class for keeping track of data related to the API | ||
|
@@ -29,14 +54,14 @@ class Client: | |
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor. | ||
{% endmacro %} | ||
{{ httpx_args_docstring() }} | ||
{% if not config.docstrings_on_attributes %} | ||
|
||
Attributes: | ||
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a | ||
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword | ||
argument to the constructor. | ||
{{ attr_in_class_docstring("raise_on_unexpected_status") | wordwrap(101) | indent(12) }} | ||
{% endif %} | ||
""" | ||
{% macro attributes() %} | ||
raise_on_unexpected_status: bool = field(default=False, kw_only=True) | ||
{{ declare_attr("raise_on_unexpected_status") | indent(4) }} | ||
_base_url: str = field(alias="base_url") | ||
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies") | ||
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers") | ||
|
@@ -147,20 +172,20 @@ class AuthenticatedClient: | |
"""A Client which has been authenticated for use on secured endpoints | ||
|
||
{{ httpx_args_docstring() }} | ||
{% if not config.docstrings_on_attributes %} | ||
|
||
Attributes: | ||
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a | ||
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword | ||
argument to the constructor. | ||
token: The token to use for authentication | ||
prefix: The prefix to use for the Authorization header | ||
auth_header_name: The name of the Authorization header | ||
{{ attr_in_class_docstring("raise_on_unexpected_status") | wordwrap(101) | indent(12) }} | ||
{{ attr_in_class_docstring("token") | indent(8) }} | ||
{{ attr_in_class_docstring("prefix") | indent(8) }} | ||
{{ attr_in_class_docstring("auth_header_name") | indent(8) }} | ||
{% endif %} | ||
""" | ||
|
||
{{ attributes() }} | ||
token: str | ||
prefix: str = "Bearer" | ||
auth_header_name: str = "Authorization" | ||
{{ declare_attr("token") | indent(4) }} | ||
{{ declare_attr("prefix") | indent(4) }} | ||
{{ declare_attr("auth_header_name") | indent(4) }} | ||
|
||
{{ builders("AuthenticatedClient") }} | ||
{{ httpx_stuff("AuthenticatedClient", "self._headers[self.auth_header_name] = f\"{self.prefix} {self.token}\" if self.prefix else self.token") }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
{% macro safe_docstring(content) %} | ||
{% macro safe_docstring(content, omit_if_empty=False) %} | ||
{# This macro returns the provided content as a docstring, set to a raw string if it contains a backslash #} | ||
{% if (not omit_if_empty) or (content | trim) %} | ||
{% if '\\' in content -%} | ||
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. The reason I added the |
||
r""" {{ content }} """ | ||
{%- else -%} | ||
""" {{ content }} """ | ||
{%- endif -%} | ||
{% endif %} | ||
{% endmacro %} |
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 makes the openapi-python-client config options available as a
config
variable in the Jinja templates.