@@ -44,17 +44,19 @@ def _validate_default(self, default: Any) -> Any:
44
44
""" Check that the default value is valid for the property's type + perform any necessary sanitization """
45
45
raise ValidationError
46
46
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 :
48
48
"""
49
49
Get a string representation of type that should be used when declaring this property
50
50
51
51
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)
53
53
"""
54
54
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 :
56
58
type_string = f"Optional[{ type_string } ]"
57
- if not no_unset and not self .required :
59
+ if not self .required :
58
60
type_string = f"Union[Unset, { type_string } ]"
59
61
return type_string
60
62
@@ -225,12 +227,14 @@ class ListProperty(Property, Generic[InnerProp]):
225
227
inner_property : InnerProp
226
228
template : ClassVar [str ] = "list_property.pyi"
227
229
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 :
229
231
""" Get a string representation of type that should be used when declaring this property """
230
232
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 :
232
236
type_string = f"Optional[{ type_string } ]"
233
- if not no_unset and not self .required :
237
+ if not self .required :
234
238
type_string = f"Union[Unset, { type_string } ]"
235
239
return type_string
236
240
@@ -258,9 +262,9 @@ class UnionProperty(Property):
258
262
inner_properties : List [Property ]
259
263
template : ClassVar [str ] = "union_property.pyi"
260
264
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 :
262
266
""" 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 ]
264
268
inner_prop_string = ", " .join (inner_types )
265
269
type_string = f"Union[{ inner_prop_string } ]"
266
270
if not no_optional and self .nullable :
@@ -337,12 +341,14 @@ def get_enum(name: str) -> Optional["EnumProperty"]:
337
341
""" Get all the EnumProperties that have been registered keyed by class name """
338
342
return _existing_enums .get (name )
339
343
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 :
341
345
""" Get a string representation of type that should be used when declaring this property """
342
346
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 :
344
350
type_string = f"Optional[{ type_string } ]"
345
- if not no_unset and not self .required :
351
+ if not self .required :
346
352
type_string = f"Union[Unset, { type_string } ]"
347
353
return type_string
348
354
@@ -401,12 +407,14 @@ def template(self) -> str: # type: ignore
401
407
return "enum_property.pyi"
402
408
return "ref_property.pyi"
403
409
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 :
405
411
""" Get a string representation of type that should be used when declaring this property """
406
412
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 :
408
416
type_string = f"Optional[{ type_string } ]"
409
- if not no_unset and not self .required :
417
+ if not self .required :
410
418
type_string = f"Union[Unset, { type_string } ]"
411
419
return type_string
412
420
0 commit comments