Skip to content

Commit 6b31e6c

Browse files
committed
refactor: set response status code type directly in response type def
1 parent eca78b1 commit 6b31e6c

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

openapi_python_client/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ def from_sources(
124124
file_encoding=file_encoding,
125125
overwrite=overwrite,
126126
output_path=output_path,
127-
allow_int_response_codes=config_file.allow_int_response_codes
127+
allow_int_response_codes=config_file.allow_int_response_codes,
128128
)
129129
return config

openapi_python_client/parser/responses.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from .errors import ParseError, PropertyError
1515
from .properties import AnyProperty, Property, Schemas, property_from_data
1616

17+
_STATUS_CODES = set(status.value for status in HTTPStatus)
18+
1719

1820
@define
1921
class Responses:
@@ -60,7 +62,7 @@ def __init__(self, *, pattern: str, code_range: tuple[int, int] | None):
6062
self.pattern = pattern
6163
self.range = code_range
6264
self.is_official = self.range is None or all(
63-
code in HTTPStatus for code in range(self.range[0], self.range[1] + 1)
65+
code in _STATUS_CODES for code in range(self.range[0], self.range[1] + 1)
6466
)
6567

6668
@staticmethod

openapi_python_client/templates/endpoint_module.py.jinja

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ from ... import errors
1111
{{ relative }}
1212
{% endfor %}
1313

14-
{% set all_standard_response_codes = endpoint.responses|map(attribute="status_code")|map(attribute="is_official")|all %}
15-
{% if config.allow_int_response_codes and not all_standard_response_codes %}
16-
HTTPStatus = int
14+
{% if config.allow_int_response_codes and not endpoint.responses|map(attribute="status_code")|map(attribute="is_official")|all %}
15+
{% set http_status_type = "int" %}
1716
{% else %}
1817
import http
19-
HTTPStatus = http.HTTPStatus
18+
{% set http_status_type = "http.HTTPStatus" %}
2019
{% endif %}
2120

2221
{% from "endpoint_macros.py.jinja" import header_params, cookie_params, query_params,
@@ -99,9 +98,9 @@ def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Res
9998
{% endif %}
10099

101100

102-
def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[{{ return_string }}, HTTPStatus]:
101+
def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[{{ return_string }}, {{ http_status_type }}]:
103102
return Response(
104-
status_code=HTTPStatus(response.status_code),
103+
status_code={{ http_status_type }}(response.status_code),
105104
content=response.content,
106105
headers=response.headers,
107106
parsed=_parse_response(client=client, response=response),
@@ -110,7 +109,7 @@ def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Res
110109

111110
def sync_detailed(
112111
{{ arguments(endpoint) | indent(4) }}
113-
) -> Response[{{ return_string }}, HTTPStatus]:
112+
) -> Response[{{ return_string }}, {{ http_status_type }}]:
114113
{{ docstring(endpoint, return_string, is_detailed=true) | indent(4) }}
115114

116115
kwargs = _get_kwargs(
@@ -136,7 +135,7 @@ def sync(
136135

137136
async def asyncio_detailed(
138137
{{ arguments(endpoint) | indent(4) }}
139-
) -> Response[{{ return_string }}, HTTPStatus]:
138+
) -> Response[{{ return_string }}, {{ http_status_type }}]:
140139
{{ docstring(endpoint, return_string, is_detailed=true) | indent(4) }}
141140

142141
kwargs = _get_kwargs(

0 commit comments

Comments
 (0)