Skip to content

Commit 8e88fa4

Browse files
Refactor
1 parent 2ad1051 commit 8e88fa4

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,38 +169,43 @@ def __attrs_post_init__(self) -> None:
169169
self, "has_properties_without_templates", any(prop.template is None for prop in self.inner_properties)
170170
)
171171

172-
def _get_inner_prop_string(self, json: bool = False) -> str:
172+
def _get_inner_type_strings(self, json: bool = False) -> List[str]:
173173
inner_types = [p.get_type_string(no_optional=True, json=json) for p in self.inner_properties]
174174
unique_inner_types = list(dict.fromkeys(inner_types))
175-
return ", ".join(unique_inner_types)
175+
return unique_inner_types
176176

177177
def get_base_type_string(self, json: bool = False) -> str:
178-
return f"Union[{self._get_inner_prop_string(json=json)}]"
178+
return f"Union[{', '.join(self._get_inner_type_strings(json=json))}]"
179179

180-
def get_type_string(self, no_optional: bool = False, query_parameter: bool = False, json: bool = False) -> str:
181-
"""
182-
Get a string representation of type that should be used when declaring this property.
183-
184-
This implementation differs slightly from `Property.get_type_string` in order to collapse
185-
nested union types.
186-
"""
187-
type_string = self.get_base_type_string(json=json)
180+
def get_type_strings_in_union(self, no_optional: bool = False, query_parameter: bool = False, json: bool = False) -> List[str]:
181+
type_strings = self._get_inner_type_strings(json=json)
188182
if no_optional:
189-
return type_string
183+
return type_strings
190184
if self.required:
191185
if self.nullable:
192-
return f"Union[None, {self._get_inner_prop_string(json=json)}]"
186+
return ["None"] + type_strings
193187
else:
194-
return type_string
188+
return type_strings
195189
else:
196190
if self.nullable:
197-
return f"Union[Unset, None, {self._get_inner_prop_string(json=json)}]"
191+
return ["Unset", "None"] + type_strings
198192
else:
199193
if query_parameter:
200194
# For query parameters, None has the same meaning as Unset
201-
return f"Union[Unset, None, {self._get_inner_prop_string(json=json)}]"
195+
return ["Unset", "None"] + type_strings
202196
else:
203-
return f"Union[Unset, {self._get_inner_prop_string(json=json)}]"
197+
return ["Unset"] + type_strings
198+
199+
def get_type_string(self, no_optional: bool = False, query_parameter: bool = False, json: bool = False) -> str:
200+
"""
201+
Get a string representation of type that should be used when declaring this property.
202+
203+
This implementation differs slightly from `Property.get_type_string` in order to collapse
204+
nested union types.
205+
"""
206+
return (
207+
f"Union[{', '.join(self.get_type_strings_in_union(no_optional=no_optional, query_parameter=query_parameter, json=json))}]"
208+
)
204209

205210
def get_imports(self, *, prefix: str) -> Set[str]:
206211
"""

openapi_python_client/templates/property_templates/union_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{% macro construct(property, source, initial_value=None) %}
22
def _parse_{{ property.python_name }}(data: {{ property.get_type_string(json=True) }}) -> {{ property.get_type_string() }}:
33
{{ property.python_name }}: {{ property.get_type_string() }}
4-
{% if "None" in property.get_type_string(json=True) %}
4+
{% if "None" in property.get_type_strings_in_union(json=True) %}
55
if data is None:
66
return data
77
{% endif %}
8-
{% if "Unset" in property.get_type_string(json=True) %}
8+
{% if "Unset" in property.get_type_strings_in_union(json=True) %}
99
if isinstance(data, Unset):
1010
return data
1111
{% endif %}

0 commit comments

Comments
 (0)