Skip to content

Commit 072f8da

Browse files
committed
Update templates construct to allow different initial value, use in union property
1 parent eb45e92 commit 072f8da

File tree

9 files changed

+21
-21
lines changed

9 files changed

+21
-21
lines changed

openapi_python_client/templates/property_templates/date_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = isoparse({{ source }}).date()
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = isoparse(cast(str, {{ source }})).date()
88
{% endif %}

openapi_python_client/templates/property_templates/datetime_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = isoparse({{ source }})
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = isoparse(cast(str, {{ source }}))
88
{% endif %}

openapi_python_client/templates/property_templates/dict_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = {{ source }}
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = {{ source }}
88
{% endif %}

openapi_python_client/templates/property_templates/enum_property.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }})
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }})
88
{% endif %}
@@ -19,9 +19,9 @@ if {{ source }} is not None:
1919
{{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET
2020
if not isinstance({{ source }}, Unset):
2121
{% if property.nullable %}
22-
{{ destination }} = {{ source }}.value if {{ source }} else None
22+
{{ destination }} = {{ source }} if {{ source }} else None
2323
{% else %}
24-
{{ destination }} = {{ source }}.value
24+
{{ destination }} = {{ source }}
2525
{% endif %}
2626
{% endif %}
2727
{% endmacro %}

openapi_python_client/templates/property_templates/file_property.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value=None) %}
22
{{ property.python_name }} = File(
33
payload = BytesIO({{ source }})
44
)

openapi_python_client/templates/property_templates/list_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="[]") %}
22
{% set inner_property = property.inner_property %}
33
{% if inner_property.template %}
44
{% set inner_source = inner_property.python_name + "_data" %}
5-
{{ property.python_name }} = []
5+
{{ property.python_name }} = {{ initial_value }}
66
{% if property.required %}
77
for {{ inner_source }} in ({{ source }}):
88
{% else %}

openapi_python_client/templates/property_templates/model_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value=None) %}
22
{% if property.required %}
33
{{ property.python_name }} = {{ property.reference.class_name }}.from_dict({{ source }})
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = {{ property.reference.class_name }}.from_dict(cast(Dict[str, Any], {{ source }}))
88
{% endif %}

openapi_python_client/templates/property_templates/none_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{% macro construct(property, source) %}
2-
{{ property.python_name }} = None
1+
{% macro construct(property, source, initial_value="None") %}
2+
{{ property.python_name }} = {{ initial_value }}
33
{% endmacro %}
44

55
{% macro transform(property, source, destination, declare_type=True) %}

openapi_python_client/templates/property_templates/union_property.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
{% macro construct(property, source) %}
2-
def _parse_{{ property.python_name }}(data: Dict[str, Any]) -> {{ property.get_type_string() }}:
1+
{% macro construct(property, source, initial_value=None) %}
2+
def _parse_{{ property.python_name }}(data: Any) -> {{ property.get_type_string() }}:
33
{{ property.python_name }}: {{ property.get_type_string() }}
44
{% for inner_property in property.inner_properties %}
55
{% if inner_property.template and not loop.last %}
66
try:
77
{% from "property_templates/" + inner_property.template import construct %}
8-
{{ construct(inner_property, source) | indent(8) }}
8+
{{ construct(inner_property, "data", initial_value="UNSET") | indent(8) }}
99
return {{ property.python_name }}
1010
except: # noqa: E722
1111
pass
1212
{% elif inner_property.template and loop.last %}{# Don't do try/except for the last one #}
1313
{% from "property_templates/" + inner_property.template import construct %}
14-
{{ construct(inner_property, source) | indent(4) }}
14+
{{ construct(inner_property, "data", initial_value="UNSET") | indent(4) }}
1515
return {{ property.python_name }}
1616
{% else %}
1717
return {{ source }}

0 commit comments

Comments
 (0)