@@ -51,9 +51,16 @@ def get_type_string(self, no_optional: bool = False) -> str:
51
51
Args:
52
52
no_optional: Do not include Optional even if the value is optional (needed for isinstance checks)
53
53
"""
54
- if no_optional or (self .required and not self .nullable ):
55
- return self ._type_string
56
- return f"Optional[{ self ._type_string } ]"
54
+ return type_string (no_optional , self , self ._type_string )
55
+
56
+ def get_query_string (self , no_optional : bool = False ) -> str :
57
+ """
58
+ Get a string representation of type that should be used when the property is represented in a query string.
59
+
60
+ Args:
61
+ no_optional: Do not include Optional even if the value is optional (needed for isinstance checks)
62
+ """
63
+ return query_string (no_optional , self , self ._type_string )
57
64
58
65
def get_imports (self , * , prefix : str ) -> Set [str ]:
59
66
"""
@@ -63,24 +70,42 @@ def get_imports(self, *, prefix: str) -> Set[str]:
63
70
prefix: A prefix to put before any relative (local) module names. This should be the number of . to get
64
71
back to the root of the generated client.
65
72
"""
66
- if self .nullable or not self .required :
73
+ if self .required and self .nullable :
67
74
return {"from typing import Optional" }
75
+ elif not self .required :
76
+ return {"from typing import Union" ,
77
+ f"from { prefix } types import Unset" ,
78
+ f"from { prefix } types import UNSET" }
68
79
return set ()
69
80
70
81
def to_string (self ) -> str :
71
82
""" How this should be declared in a dataclass """
72
83
if self .default :
73
84
default = self .default
74
85
elif not self .required :
75
- default = "None "
86
+ default = "UNSET "
76
87
else :
77
88
default = None
78
89
79
90
if default is not None :
80
- return f"{ self .python_name } : { self .get_type_string ()} = { self . default } "
91
+ return f"{ self .python_name } : { self .get_type_string ()} = { default } "
81
92
else :
82
93
return f"{ self .python_name } : { self .get_type_string ()} "
83
94
95
+ def to_query_string (self ) -> str :
96
+ """ How this should be declared in a query string """
97
+ if self .default :
98
+ default = self .default
99
+ elif not self .required :
100
+ default = "None"
101
+ else :
102
+ default = None
103
+
104
+ if default is not None :
105
+ return f"{ self .python_name } : { self .get_query_string ()} = { default } "
106
+ else :
107
+ return f"{ self .python_name } : { self .get_query_string ()} "
108
+
84
109
85
110
@dataclass
86
111
class StringProperty (Property ):
@@ -222,9 +247,13 @@ class ListProperty(Property, Generic[InnerProp]):
222
247
223
248
def get_type_string (self , no_optional : bool = False ) -> str :
224
249
""" Get a string representation of type that should be used when declaring this property """
225
- if no_optional or (self .required and not self .nullable ):
226
- return f"List[{ self .inner_property .get_type_string ()} ]"
227
- return f"Optional[List[{ self .inner_property .get_type_string ()} ]]"
250
+ return type_string (no_optional , self , f"List[{ self .inner_property .get_type_string ()} ]" )
251
+
252
+ def get_query_string (self , no_optional : bool = False ) -> str :
253
+ """
254
+ Get a string representation of type that should be used when the property is represented in a query string.
255
+ """
256
+ return query_string (no_optional , self , f"List[{ self .inner_property .get_query_string ()} ]" )
228
257
229
258
def get_imports (self , * , prefix : str ) -> Set [str ]:
230
259
"""
@@ -254,9 +283,15 @@ def get_type_string(self, no_optional: bool = False) -> str:
254
283
""" Get a string representation of type that should be used when declaring this property """
255
284
inner_types = [p .get_type_string () for p in self .inner_properties ]
256
285
inner_prop_string = ", " .join (inner_types )
257
- if no_optional or (self .required and not self .nullable ):
258
- return f"Union[{ inner_prop_string } ]"
259
- return f"Optional[Union[{ inner_prop_string } ]]"
286
+ return type_string (no_optional , self , inner_prop_string , is_outer_union = True )
287
+
288
+ def get_query_string (self , no_optional : bool = False ) -> str :
289
+ """
290
+ Get a string representation of type that should be used when the property is represented in a query string.
291
+ """
292
+ inner_types = [p .get_query_string () for p in self .inner_properties ]
293
+ inner_prop_string = ", " .join (inner_types )
294
+ return query_string (no_optional , self , inner_prop_string , is_outer_union = True )
260
295
261
296
def get_imports (self , * , prefix : str ) -> Set [str ]:
262
297
"""
@@ -328,10 +363,13 @@ def get_enum(name: str) -> Optional["EnumProperty"]:
328
363
329
364
def get_type_string (self , no_optional : bool = False ) -> str :
330
365
""" Get a string representation of type that should be used when declaring this property """
366
+ return type_string (no_optional , self , self .reference .class_name )
331
367
332
- if no_optional or (self .required and not self .nullable ):
333
- return self .reference .class_name
334
- return f"Optional[{ self .reference .class_name } ]"
368
+ def get_query_string (self , no_optional : bool = False ) -> str :
369
+ """
370
+ Get a string representation of type that should be used when the property is represented in a query string.
371
+ """
372
+ return query_string (no_optional , self , self .reference .class_name )
335
373
336
374
def get_imports (self , * , prefix : str ) -> Set [str ]:
337
375
"""
@@ -390,9 +428,13 @@ def template(self) -> str: # type: ignore
390
428
391
429
def get_type_string (self , no_optional : bool = False ) -> str :
392
430
""" Get a string representation of type that should be used when declaring this property """
393
- if no_optional or (self .required and not self .nullable ):
394
- return self .reference .class_name
395
- return f"Optional[{ self .reference .class_name } ]"
431
+ return type_string (no_optional , self , self .reference .class_name )
432
+
433
+ def get_query_string (self , no_optional : bool = False ) -> str :
434
+ """
435
+ Get a string representation of type that should be used when the property is represented in a query string.
436
+ """
437
+ return query_string (no_optional , self , self .reference .class_name )
396
438
397
439
def get_imports (self , * , prefix : str ) -> Set [str ]:
398
440
"""
@@ -575,3 +617,27 @@ def property_from_data(
575
617
return _property_from_data (name = name , required = required , data = data )
576
618
except ValidationError :
577
619
return PropertyError (detail = "Failed to validate default value" , data = data )
620
+
621
+
622
+ def type_string (no_optional : bool , prop : Property , inner_type_string : str , is_outer_union : bool = False ) -> str :
623
+ if no_optional or (prop .required and not prop .nullable ):
624
+ if is_outer_union :
625
+ return f"Union[{ inner_type_string } ]"
626
+ return inner_type_string
627
+ elif prop .nullable and prop .required :
628
+ if is_outer_union :
629
+ return f"Optional[Union[{ inner_type_string } ]]"
630
+ return f"Optional[{ inner_type_string } ]"
631
+ elif not prop .nullable and not prop .required :
632
+ return f"Union[Unset, None, { inner_type_string } ]"
633
+ return f"Union[Unset, { inner_type_string } ]"
634
+
635
+
636
+ def query_string (no_optional : bool , prop : Property , inner_type_string : str , is_outer_union : bool = False ) -> str :
637
+ if no_optional or (prop .required and not prop .nullable ):
638
+ if is_outer_union :
639
+ return f"Union[{ inner_type_string } ]"
640
+ return inner_type_string
641
+ if is_outer_union :
642
+ return f"Optional[Union[{ inner_type_string } ]]"
643
+ return f"Optional[{ inner_type_string } ]"
0 commit comments