Skip to content

Commit 80c799d

Browse files
committed
also move docstrings to attributes in client classes
1 parent 457f963 commit 80c799d

File tree

5 files changed

+55
-38
lines changed
  • end_to_end_tests
    • docstrings-on-attributes-golden-record/my_test_api_client
    • golden-record/my_test_api_client
    • literal-enums-golden-record/my_enum_api_client
    • test-3-1-golden-record/test_3_1_features_client
  • openapi_python_client/templates

5 files changed

+55
-38
lines changed

end_to_end_tests/docstrings-on-attributes-golden-record/my_test_api_client/client.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ class Client:
2727
2828
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
2929
30-
31-
Attributes:
32-
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
33-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
34-
argument to the constructor.
3530
"""
3631

3732
raise_on_unexpected_status: bool = field(default=False, kw_only=True)
33+
"""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."""
3834
_base_url: str = field(alias="base_url")
3935
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
4036
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
@@ -154,17 +150,10 @@ class AuthenticatedClient:
154150
155151
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
156152
157-
158-
Attributes:
159-
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
160-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
161-
argument to the constructor.
162-
token: The token to use for authentication
163-
prefix: The prefix to use for the Authorization header
164-
auth_header_name: The name of the Authorization header
165153
"""
166154

167155
raise_on_unexpected_status: bool = field(default=False, kw_only=True)
156+
"""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."""
168157
_base_url: str = field(alias="base_url")
169158
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
170159
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
@@ -176,8 +165,11 @@ class AuthenticatedClient:
176165
_async_client: Optional[httpx.AsyncClient] = field(default=None, init=False)
177166

178167
token: str
168+
"""The token to use for authentication"""
179169
prefix: str = "Bearer"
170+
"""The prefix to use for the Authorization header"""
180171
auth_header_name: str = "Authorization"
172+
"""The name of the Authorization header"""
181173

182174
def with_headers(self, headers: dict[str, str]) -> "AuthenticatedClient":
183175
"""Get a new client matching this one with additional headers"""

end_to_end_tests/golden-record/my_test_api_client/client.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Client:
3030
3131
Attributes:
3232
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
33-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
34-
argument to the constructor.
33+
status code that was not documented in the source OpenAPI document. Can also be provided as a
34+
keyword argument to the constructor.
3535
"""
3636

3737
raise_on_unexpected_status: bool = field(default=False, kw_only=True)
@@ -157,8 +157,8 @@ class AuthenticatedClient:
157157
158158
Attributes:
159159
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
160-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
161-
argument to the constructor.
160+
status code that was not documented in the source OpenAPI document. Can also be provided as a
161+
keyword argument to the constructor.
162162
token: The token to use for authentication
163163
prefix: The prefix to use for the Authorization header
164164
auth_header_name: The name of the Authorization header

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/client.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Client:
3030
3131
Attributes:
3232
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
33-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
34-
argument to the constructor.
33+
status code that was not documented in the source OpenAPI document. Can also be provided as a
34+
keyword argument to the constructor.
3535
"""
3636

3737
raise_on_unexpected_status: bool = field(default=False, kw_only=True)
@@ -157,8 +157,8 @@ class AuthenticatedClient:
157157
158158
Attributes:
159159
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
160-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
161-
argument to the constructor.
160+
status code that was not documented in the source OpenAPI document. Can also be provided as a
161+
keyword argument to the constructor.
162162
token: The token to use for authentication
163163
prefix: The prefix to use for the Authorization header
164164
auth_header_name: The name of the Authorization header

end_to_end_tests/test-3-1-golden-record/test_3_1_features_client/client.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Client:
3030
3131
Attributes:
3232
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
33-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
34-
argument to the constructor.
33+
status code that was not documented in the source OpenAPI document. Can also be provided as a
34+
keyword argument to the constructor.
3535
"""
3636

3737
raise_on_unexpected_status: bool = field(default=False, kw_only=True)
@@ -157,8 +157,8 @@ class AuthenticatedClient:
157157
158158
Attributes:
159159
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
160-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
161-
argument to the constructor.
160+
status code that was not documented in the source OpenAPI document. Can also be provided as a
161+
keyword argument to the constructor.
162162
token: The token to use for authentication
163163
prefix: The prefix to use for the Authorization header
164164
auth_header_name: The name of the Authorization header

openapi_python_client/templates/client.py.jinja

+38-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ from attrs import define, field, evolve
55
import httpx
66

77

8+
{% set attrs_info = {
9+
"raise_on_unexpected_status": namespace(
10+
type="bool",
11+
default="field(default=False, kw_only=True)",
12+
docstring="Whether or not to raise an errors.UnexpectedStatus if the API returns a status code"
13+
" that was not documented in the source OpenAPI document. Can also be provided as a keyword"
14+
" argument to the constructor."
15+
),
16+
"token": namespace(type="str", default="", docstring="The token to use for authentication"),
17+
"prefix": namespace(type="str", default='"Bearer"', docstring="The prefix to use for the Authorization header"),
18+
"auth_header_name": namespace(type="str", default='"Authorization"', docstring="The name of the Authorization header"),
19+
} %}
20+
21+
{% macro attr_in_class_docstring(name) %}
22+
{{ name }}: {{ attrs_info[name].docstring }}
23+
{%- endmacro %}
24+
25+
{% macro declare_attr(name) %}
26+
{% set attr = attrs_info[name] %}
27+
{{ name }}: {{ attr.type }}{% if attr.default %} = {{ attr.default }}{% endif %}
28+
{% if attr.docstring and config.docstrings_on_attributes +%}
29+
"""{{ attr.docstring }}"""
30+
{%- endif %}
31+
{% endmacro %}
32+
833
@define
934
class Client:
1035
"""A class for keeping track of data related to the API
@@ -29,14 +54,14 @@ class Client:
2954
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
3055
{% endmacro %}
3156
{{ httpx_args_docstring() }}
57+
{% if not config.docstrings_on_attributes %}
3258

3359
Attributes:
34-
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
35-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
36-
argument to the constructor.
60+
{{ attr_in_class_docstring("raise_on_unexpected_status") | wordwrap(100) | indent(12) }}
61+
{% endif %}
3762
"""
3863
{% macro attributes() %}
39-
raise_on_unexpected_status: bool = field(default=False, kw_only=True)
64+
{{ declare_attr("raise_on_unexpected_status") | indent(4) }}
4065
_base_url: str = field(alias="base_url")
4166
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
4267
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
@@ -147,20 +172,20 @@ class AuthenticatedClient:
147172
"""A Client which has been authenticated for use on secured endpoints
148173

149174
{{ httpx_args_docstring() }}
175+
{% if not config.docstrings_on_attributes %}
150176

151177
Attributes:
152-
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
153-
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
154-
argument to the constructor.
155-
token: The token to use for authentication
156-
prefix: The prefix to use for the Authorization header
157-
auth_header_name: The name of the Authorization header
178+
{{ attr_in_class_docstring("raise_on_unexpected_status") | wordwrap(100) | indent(12) }}
179+
{{ attr_in_class_docstring("token") | indent(8) }}
180+
{{ attr_in_class_docstring("prefix") | indent(8) }}
181+
{{ attr_in_class_docstring("auth_header_name") | indent(8) }}
182+
{% endif %}
158183
"""
159184

160185
{{ attributes() }}
161-
token: str
162-
prefix: str = "Bearer"
163-
auth_header_name: str = "Authorization"
186+
{{ declare_attr("token") | indent(4) }}
187+
{{ declare_attr("prefix") | indent(4) }}
188+
{{ declare_attr("auth_header_name") | indent(4) }}
164189

165190
{{ builders("AuthenticatedClient") }}
166191
{{ httpx_stuff("AuthenticatedClient", "self._headers[self.auth_header_name] = f\"{self.prefix} {self.token}\" if self.prefix else self.token") }}

0 commit comments

Comments
 (0)