Skip to content

Commit 0ab3ddf

Browse files
emanndbanty
andauthored
Apply suggestions from code review
Co-authored-by: Dylan Anthony <43723790+dbanty@users.noreply.github.com>
1 parent 961f6df commit 0ab3ddf

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

openapi_python_client/parser/properties.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ def _validate_default(self, default: Any) -> Any:
4444
""" Check that the default value is valid for the property's type + perform any necessary sanitization """
4545
raise ValidationError
4646

47-
def get_type_string(self, no_optional: bool = False, no_unset: bool = False) -> str:
47+
def get_type_string(self, no_optional: bool = False) -> str:
4848
"""
4949
Get a string representation of type that should be used when declaring this property
5050
5151
Args:
52-
no_optional: Do not include Optional even if the value is optional (needed for isinstance checks)
52+
no_optional: Do not include Optional or Unset even if the value is optional (needed for isinstance checks)
5353
"""
5454
type_string = self._type_string
55-
if not no_optional and self.nullable:
55+
if no_optional:
56+
return type_string
57+
if self.nullable:
5658
type_string = f"Optional[{type_string}]"
57-
if not no_unset and not self.required:
59+
if not self.required:
5860
type_string = f"Union[Unset, {type_string}]"
5961
return type_string
6062

@@ -225,12 +227,14 @@ class ListProperty(Property, Generic[InnerProp]):
225227
inner_property: InnerProp
226228
template: ClassVar[str] = "list_property.pyi"
227229

228-
def get_type_string(self, no_optional: bool = False, no_unset: bool = False) -> str:
230+
def get_type_string(self, no_optional: bool = False) -> str:
229231
""" Get a string representation of type that should be used when declaring this property """
230232
type_string = f"List[{self.inner_property.get_type_string()}]"
231-
if not no_optional and self.nullable:
233+
if no_optional:
234+
return type_string
235+
if self.nullable:
232236
type_string = f"Optional[{type_string}]"
233-
if not no_unset and not self.required:
237+
if not self.required:
234238
type_string = f"Union[Unset, {type_string}]"
235239
return type_string
236240

@@ -258,9 +262,9 @@ class UnionProperty(Property):
258262
inner_properties: List[Property]
259263
template: ClassVar[str] = "union_property.pyi"
260264

261-
def get_type_string(self, no_optional: bool = False, no_unset: bool = False) -> str:
265+
def get_type_string(self, no_optional: bool = False) -> str:
262266
""" Get a string representation of type that should be used when declaring this property """
263-
inner_types = [p.get_type_string(no_unset=True) for p in self.inner_properties]
267+
inner_types = [p.get_type_string(no_optional=True) for p in self.inner_properties]
264268
inner_prop_string = ", ".join(inner_types)
265269
type_string = f"Union[{inner_prop_string}]"
266270
if not no_optional and self.nullable:
@@ -337,12 +341,14 @@ def get_enum(name: str) -> Optional["EnumProperty"]:
337341
""" Get all the EnumProperties that have been registered keyed by class name """
338342
return _existing_enums.get(name)
339343

340-
def get_type_string(self, no_optional: bool = False, no_unset: bool = False) -> str:
344+
def get_type_string(self, no_optional: bool = False) -> str:
341345
""" Get a string representation of type that should be used when declaring this property """
342346
type_string = self.reference.class_name
343-
if not no_optional and self.nullable:
347+
if no_optional:
348+
return type_string
349+
if self.nullable:
344350
type_string = f"Optional[{type_string}]"
345-
if not no_unset and not self.required:
351+
if not self.required:
346352
type_string = f"Union[Unset, {type_string}]"
347353
return type_string
348354

@@ -401,12 +407,14 @@ def template(self) -> str: # type: ignore
401407
return "enum_property.pyi"
402408
return "ref_property.pyi"
403409

404-
def get_type_string(self, no_optional: bool = False, no_unset: bool = False) -> str:
410+
def get_type_string(self, no_optional: bool = False) -> str:
405411
""" Get a string representation of type that should be used when declaring this property """
406412
type_string = self.reference.class_name
407-
if not no_optional and self.nullable:
413+
if no_optional:
414+
return type_string
415+
if self.nullable:
408416
type_string = f"Optional[{type_string}]"
409-
if not no_unset and not self.required:
417+
if not self.required:
410418
type_string = f"Union[Unset, {type_string}]"
411419
return type_string
412420

openapi_python_client/templates/model.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional, Set
1+
from typing import Any, Dict
22

33
import attr
44

openapi_python_client/templates/property_templates/union_property.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ elif {{ source }} is None:
3737
{% endif %}
3838
{% for inner_property in property.inner_properties %}
3939
{% if loop.first and property.required and not property.nullable %}{# No if UNSET or if None statement before this #}
40-
if isinstance({{ source }}, {{ inner_property.get_type_string(no_optional=True, no_unset=True) }}):
40+
if isinstance({{ source }}, {{ inner_property.get_type_string(no_optional=True) }}):
4141
{% elif not loop.last %}
42-
elif isinstance({{ source }}, {{ inner_property.get_type_string(no_optional=True, no_unset=True) }}):
42+
elif isinstance({{ source }}, {{ inner_property.get_type_string(no_optional=True) }}):
4343
{% else %}
4444
else:
4545
{% endif %}

0 commit comments

Comments
 (0)