Skip to content

Commit 08fd630

Browse files
author
Constantinos Symeonides
committed
fix: Declaring types is necessary
1 parent 1b5133c commit 08fd630

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

Diff for: end_to_end_tests/golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from io import BytesIO
3-
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast
3+
from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar, Union, cast
44

55
import attr
66

@@ -76,18 +76,18 @@ def to_multipart(self) -> Dict[str, Any]:
7676

7777
some_object = (None, json.dumps(self.some_object.to_dict()), "application/json")
7878

79-
some_optional_file = UNSET
79+
some_optional_file: Union[Unset, FileJsonType] = UNSET
8080
if not isinstance(self.some_optional_file, Unset):
8181
some_optional_file = self.some_optional_file.to_tuple()
8282

8383
some_string = self.some_string
8484
some_number = self.some_number
85-
some_array = UNSET
85+
some_array: Union[Unset, Tuple[None, str, str]] = UNSET
8686
if not isinstance(self.some_array, Unset):
87-
some_array = self.some_array
88-
some_array = (None, json.dumps(some_array), "application/json")
87+
_temp_some_array = self.some_array
88+
some_array = (None, json.dumps(_temp_some_array), "application/json")
8989

90-
some_optional_object = UNSET
90+
some_optional_object: Union[Unset, Tuple[None, str, str]] = UNSET
9191
if not isinstance(self.some_optional_object, Unset):
9292
some_optional_object = (None, json.dumps(self.some_optional_object.to_dict()), "application/json")
9393

Diff for: openapi_python_client/templates/model.py.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class {{ class_name }}:
4747
{% for property in model.required_properties + model.optional_properties %}
4848
{% if property.template %}
4949
{% from "property_templates/" + property.template import transform %}
50-
{{ transform(property, "self." + property.python_name, property.python_name, declare_type=not multipart, stringify=multipart) }}
50+
{{ transform(property, "self." + property.python_name, property.python_name, stringify=multipart) }}
5151
{% else %}
5252
{{ property.python_name }} = self.{{ property.python_name }}
5353
{% endif %}
@@ -58,7 +58,7 @@ field_dict: Dict[str, Any] = {}
5858
{% if model.additional_properties.template %}
5959
{% from "property_templates/" + model.additional_properties.template import transform %}
6060
for prop_name, prop in self.additional_properties.items():
61-
{{ transform(model.additional_properties, "prop", "field_dict[prop_name]", declare_type=not multipart, stringify=multipart) | indent(4) }}
61+
{{ transform(model.additional_properties, "prop", "field_dict[prop_name]", stringify=multipart) | indent(4) }}
6262
{% else %}
6363
field_dict.update(self.additional_properties)
6464
{% endif %}

Diff for: openapi_python_client/templates/property_templates/list_property.py.jinja

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ for {{ inner_source }} in (_{{ property.python_name }} or []):
1919

2020
{% macro _transform(property, source, destination, stringify) %}
2121
{% set inner_property = property.inner_property %}
22+
{% if stringify %}
23+
{% set stringified_destination = destination %}
24+
{% set destination = "_temp_" + destination %}
25+
{% endif %}
2226
{% if inner_property.template %}
2327
{% set inner_source = inner_property.python_name + "_data" %}
2428
{{ destination }} = []
@@ -30,14 +34,18 @@ for {{ inner_source }} in {{ source }}:
3034
{{ destination }} = {{ source }}
3135
{% endif %}
3236
{% if stringify %}
33-
{{ destination }} = (None, json.dumps({{ destination }}), 'application/json')
37+
{{ stringified_destination }} = (None, json.dumps({{ destination }}), 'application/json')
3438
{% endif %}
3539
{% endmacro %}
3640

3741
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %}
3842

3943
{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
4044
{% set inner_property = property.inner_property %}
45+
{% set type_string = property.get_type_string(json=True) %}
46+
{% if stringify %}
47+
{% set type_string = "Union[Unset, Tuple[None, str, str]]" %}
48+
{% endif %}
4149
{% if property.required %}
4250
{% if property.nullable %}
4351
if {{ source }} is None:
@@ -48,7 +56,7 @@ else:
4856
{{ _transform(property, source, destination, stringify) }}
4957
{% endif %}
5058
{% else %}
51-
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
59+
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
5260
if not isinstance({{ source }}, Unset):
5361
{% if property.nullable %}
5462
if {{ source }} is None:

Diff for: openapi_python_client/templates/property_templates/model_property.py.jinja

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
{% macro transform(property, source, destination, declare_type=True, stringify=False, transform_method="to_dict") %}
1414
{% set transformed = source + "." + transform_method + "()" %}
15+
{% set type_string = property.get_type_string(json=True) %}
1516
{% if stringify %}
16-
{% set transformed = "(None, json.dumps(" + transformed + "), 'application/json')" %}
17+
{% set transformed = "(None, json.dumps(" + transformed + "), 'application/json')" %}
18+
{% set type_string = "Union[Unset, Tuple[None, str, str]]" %}
1719
{% endif %}
1820
{% if property.required %}
1921
{% if property.nullable %}
@@ -22,7 +24,7 @@
2224
{{ destination }} = {{ transformed }}
2325
{% endif %}
2426
{% else %}
25-
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
27+
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
2628
if not isinstance({{ source }}, Unset):
2729
{% if property.nullable %}
2830
{{ destination }} = {{ transformed }} if {{ source }} else None

0 commit comments

Comments
 (0)