Skip to content

Commit 6c38462

Browse files
committed
fix: Remove need for unnecessary if True when constructing unions of Any properties.
1 parent 7ae9ad1 commit 6c38462

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

end_to_end_tests/golden-record/my_test_api_client/models/a_model.py

-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ def _parse_one_of_models(data: object) -> Union[Any, FreeFormModel, ModelWithUni
230230
return one_of_models_type_1
231231
except: # noqa: E722
232232
pass
233-
if not True:
234-
raise TypeError()
235233
one_of_models_type_2 = data
236234

237235
return one_of_models_type_2

openapi_python_client/templates/property_templates/any_property.py.jinja

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{{ property.python_name }} = {{ source }}
33
{% endmacro %}
44

5-
{% macro check_type_for_construct(property, source) %}True{% endmacro %}
6-
75
{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
86
{{ destination }} = {{ source }}
97
{% endmacro %}

openapi_python_client/templates/property_templates/union_property.py.jinja

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_stri
99
return data
1010
{% endif %}
1111
{% for inner_property in property.inner_properties_with_template() %}
12-
{% if not loop.last or property.has_properties_without_templates %}
12+
{% import "property_templates/" + inner_property.template as inner_template %}
13+
{% if inner_template.check_type_for_construct and (not loop.last or property.has_properties_without_templates) %}
1314
try:
14-
{% from "property_templates/" + inner_property.template import construct, check_type_for_construct %}
15-
if not {{ check_type_for_construct(inner_property, "data") }}:
15+
if not {{ inner_template.check_type_for_construct(inner_property, "data") }}:
1616
raise TypeError()
17-
{{ construct(inner_property, "data", initial_value="UNSET") | indent(8) }}
17+
{{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(8) }}
1818
return {{ inner_property.python_name }}
1919
except: # noqa: E722
2020
pass
21-
{% else %}{# Don't do try/except for the last one #}
22-
{% from "property_templates/" + inner_property.template import construct, check_type_for_construct %}
23-
if not {{ check_type_for_construct(inner_property, "data") }}:
21+
{% else %}{# Don't do try/except for the last one nor any properties with no type checking #}
22+
{% if inner_template.check_type_for_construct %}
23+
if not {{ inner_template.check_type_for_construct(inner_property, "data") }}:
2424
raise TypeError()
25-
{{ construct(inner_property, "data", initial_value="UNSET") | indent(4) }}
25+
{% endif %}
26+
{{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(4) }}
2627
return {{ inner_property.python_name }}
2728
{% endif %}
2829
{% endfor %}
@@ -34,9 +35,6 @@ def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_stri
3435
{{ property.python_name }} = _parse_{{ property.python_name }}({{ source }})
3536
{% endmacro %}
3637

37-
{# For now we assume there will be no unions of unions #}
38-
{% macro check_type_for_construct(property, source) %}True{% endmacro %}
39-
4038
{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
4139
{% if not property.required or property.nullable %}
4240
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %}

0 commit comments

Comments
 (0)