Skip to content

Commit e743c9e

Browse files
fix: Do not cast() unnecessarily
cast(None, None) made mypy fail after that last commit.
1 parent 73d68d5 commit e743c9e

File tree

9 files changed

+31
-16
lines changed

9 files changed

+31
-16
lines changed

end_to_end_tests/golden-record/my_test_api_client/api/tests/callback_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional, Union, cast
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

@@ -35,7 +35,7 @@ def _get_kwargs(
3535

3636
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]:
3737
if response.status_code == HTTPStatus.OK:
38-
response_200 = cast(Any, response.json())
38+
response_200 = response.json()
3939
return response_200
4040
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
4141
response_422 = HTTPValidationError.from_dict(response.json())

end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from http import HTTPStatus
3-
from typing import Any, Dict, List, Optional, Union, cast
3+
from typing import Any, Dict, List, Optional, Union
44

55
import httpx
66
from dateutil.parser import isoparse
@@ -102,7 +102,7 @@ def _get_kwargs(
102102

103103
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]:
104104
if response.status_code == HTTPStatus.OK:
105-
response_200 = cast(Any, response.json())
105+
response_200 = response.json()
106106
return response_200
107107
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
108108
response_422 = HTTPValidationError.from_dict(response.json())

end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional, Union, cast
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

@@ -40,7 +40,7 @@ def _get_kwargs(
4040

4141
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]:
4242
if response.status_code == HTTPStatus.OK:
43-
response_200 = cast(Any, response.json())
43+
response_200 = response.json()
4444
return response_200
4545
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
4646
response_422 = HTTPValidationError.from_dict(response.json())

end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional, Union, cast
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

@@ -35,7 +35,7 @@ def _get_kwargs(
3535

3636
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]:
3737
if response.status_code == HTTPStatus.OK:
38-
response_200 = cast(Any, response.json())
38+
response_200 = response.json()
3939
return response_200
4040
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
4141
response_422 = HTTPValidationError.from_dict(response.json())

end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional, Union, cast
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

@@ -32,10 +32,10 @@ def _get_kwargs(
3232

3333
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, None]]:
3434
if response.status_code == HTTPStatus.OK:
35-
response_200 = cast(Any, response.json())
35+
response_200 = response.json()
3636
return response_200
3737
if response.status_code == HTTPStatus.UNAUTHORIZED:
38-
response_401 = cast(None, None)
38+
response_401 = None
3939
return response_401
4040
if client.raise_on_unexpected_status:
4141
raise errors.UnexpectedStatus(response.status_code, response.content)

end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional, Union, cast
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

@@ -35,7 +35,7 @@ def _get_kwargs(
3535

3636
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]:
3737
if response.status_code == HTTPStatus.OK:
38-
response_200 = cast(Any, response.json())
38+
response_200 = response.json()
3939
return response_200
4040
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
4141
response_422 = HTTPValidationError.from_dict(response.json())

end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_multiple_files_tests_upload_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, List, Optional, Union, cast
2+
from typing import Any, Dict, List, Optional, Union
33

44
import httpx
55

@@ -38,7 +38,7 @@ def _get_kwargs(
3838

3939
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]:
4040
if response.status_code == HTTPStatus.OK:
41-
response_200 = cast(Any, response.json())
41+
response_200 = response.json()
4242
return response_200
4343
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
4444
response_422 = HTTPValidationError.from_dict(response.json())

openapi_python_client/parser/responses.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ class Response:
2020
prop: Property
2121
source: str
2222

23+
def get_typed_source(self) -> str:
24+
"""Get Python statement that parses the response and returns the correct type.
25+
26+
This might include a type cast if necessary (e.g. if we parse JSON
27+
that can only be a string according to the OpenAPI schema)
28+
"""
29+
if self.source == self.prop.get_type_string():
30+
# No cast needed e.g. for `None` and `None`.
31+
return self.source
32+
if self.source == "response.json()" and self.prop.get_type_string() == "Any":
33+
# response.json() is often used for untyped responses and already has
34+
# the `Any` return type annotation.
35+
return self.source
36+
return f"cast({self.prop.get_type_string()}, {self.source})"
37+
2338

2439
def _source_by_content_type(content_type: str) -> Optional[str]:
2540
known_content_types = {

openapi_python_client/templates/endpoint_module.py.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[{{
6767
{% if prop_template.construct %}
6868
{{ prop_template.construct(response.prop, response.source) | indent(8) }}
6969
{% else %}
70-
{{ response.prop.python_name }} = cast({{ response.prop.get_type_string() }}, {{ response.source }})
70+
{{ response.prop.python_name }} = {{ response.get_typed_source() }}
7171
{% endif %}
7272
return {{ response.prop.python_name }}
7373
{% else %}

0 commit comments

Comments
 (0)