{% macro construct(property, source, initial_value=None) %} def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_string() }}: {% if "None" in property.get_type_strings_in_union(json=True) %} if data is None: return data {% endif %} {% if "Unset" in property.get_type_strings_in_union(json=True) %} if isinstance(data, Unset): return data {% endif %} {% set ns = namespace(contains_unmodified_properties = false) %} {% for inner_property in property.inner_properties %} {% import "property_templates/" + inner_property.template as inner_template %} {% if not inner_template.construct %} {% set ns.contains_unmodified_properties = true %} {% continue %} {% endif %} {% if inner_template.check_type_for_construct and (not loop.last or ns.contains_unmodified_properties) %} try: if not {{ inner_template.check_type_for_construct(inner_property, "data") }}: raise TypeError() {{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(8) }} return {{ inner_property.python_name }} except: # noqa: E722 pass {% else %}{# Don't do try/except for the last one nor any properties with no type checking #} {% if inner_template.check_type_for_construct %} if not {{ inner_template.check_type_for_construct(inner_property, "data") }}: raise TypeError() {% endif %} {{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(4) }} return {{ inner_property.python_name }} {% endif %} {% endfor %} {% if ns.contains_unmodified_properties %} return cast({{ property.get_type_string() }}, data) {% endif %} {{ property.python_name }} = _parse_{{ property.python_name }}({{ source }}) {% endmacro %} {% macro transform(property, source, destination, declare_type=True, multipart=False) %} {% if not property.required or property.nullable %} {{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} {% if not property.required %} if isinstance({{ source }}, Unset): {{ destination }} = UNSET {% endif %} {% endif %} {% if property.nullable %} {% if property.required %} if {{ source }} is None: {% else %}{# There's an if UNSET statement before this #} elif {{ source }} is None: {% endif %} {{ destination }} = None {% endif %} {% set ns = namespace(contains_properties_without_transform = false, contains_modified_properties = not property.required) %} {% for inner_property in property.inner_properties %} {% import "property_templates/" + inner_property.template as inner_template %} {% if not inner_template.transform %} {% set ns.contains_properties_without_transform = true %} {% continue %} {% else %} {% set ns.contains_modified_properties = true %} {% endif %} {% if loop.first and property.required and not property.nullable %}{# No if UNSET or if None statement before this #} if isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}): {% elif not loop.last or ns.contains_properties_without_transform %} elif isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}): {% else %} else: {% endif %} {{ inner_template.transform(inner_property, source, destination, declare_type=False, multipart=multipart) | indent(4) }} {% endfor %} {% if ns.contains_properties_without_transform and ns.contains_modified_properties %} else: {{ destination }} = {{ source }} {% elif ns.contains_properties_without_transform %} {{ destination }} = {{ source }} {% endif %} {% endmacro %}