@@ -51,16 +51,9 @@ 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
- return type_string (no_optional , self , self ._type_string )
55
-
56
- def get_query_method_arg (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_method_arg (no_optional , self , self ._type_string )
54
+ if no_optional or (self .required and not self .nullable ):
55
+ return self ._type_string
56
+ return f"Optional[{ self ._type_string } ]"
64
57
65
58
def get_imports (self , * , prefix : str ) -> Set [str ]:
66
59
"""
@@ -70,11 +63,9 @@ def get_imports(self, *, prefix: str) -> Set[str]:
70
63
prefix: A prefix to put before any relative (local) module names. This should be the number of . to get
71
64
back to the root of the generated client.
72
65
"""
73
- if self .required and self .nullable :
74
- return {"from typing import Optional" }
75
- elif not self .required :
76
- return {"from typing import Union" ,
77
- f"from { prefix } types import Unset" ,
66
+ if self .nullable or not self .required :
67
+ return {"from typing import Optional" ,
68
+ "from typing import cast" ,
78
69
f"from { prefix } types import UNSET" }
79
70
return set ()
80
71
@@ -83,7 +74,7 @@ def to_string(self) -> str:
83
74
if self .default :
84
75
default = self .default
85
76
elif not self .required :
86
- default = "UNSET"
77
+ default = "cast(None, UNSET) "
87
78
else :
88
79
default = None
89
80
@@ -102,10 +93,9 @@ def to_query_method_arg(self) -> str:
102
93
default = None
103
94
104
95
if default is not None :
105
- return f"{ self .python_name } : { self .get_query_method_arg ()} = { default } "
96
+ return f"{ self .python_name } : { self .get_type_string ()} = { default } "
106
97
else :
107
- return f"{ self .python_name } : { self .get_query_method_arg ()} "
108
-
98
+ return f"{ self .python_name } : { self .get_type_string ()} "
109
99
110
100
@dataclass
111
101
class StringProperty (Property ):
@@ -247,13 +237,9 @@ class ListProperty(Property, Generic[InnerProp]):
247
237
248
238
def get_type_string (self , no_optional : bool = False ) -> str :
249
239
""" Get a string representation of type that should be used when declaring this property """
250
- return type_string (no_optional , self , f"List[{ self .inner_property .get_type_string ()} ]" )
251
-
252
- def get_query_method_arg (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_method_arg (no_optional , self , f"List[{ self .inner_property .get_query_method_arg ()} ]" )
240
+ if no_optional or (self .required and not self .nullable ):
241
+ return f"List[{ self .inner_property .get_type_string ()} ]"
242
+ return f"Optional[List[{ self .inner_property .get_type_string ()} ]]"
257
243
258
244
def get_imports (self , * , prefix : str ) -> Set [str ]:
259
245
"""
@@ -283,15 +269,9 @@ def get_type_string(self, no_optional: bool = False) -> str:
283
269
""" Get a string representation of type that should be used when declaring this property """
284
270
inner_types = [p .get_type_string () for p in self .inner_properties ]
285
271
inner_prop_string = ", " .join (inner_types )
286
- return type_string (no_optional , self , inner_prop_string , is_outer_union = True )
287
-
288
- def get_query_method_arg (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_method_arg () for p in self .inner_properties ]
293
- inner_prop_string = ", " .join (inner_types )
294
- return query_method_arg (no_optional , self , inner_prop_string , is_outer_union = True )
272
+ if no_optional or (self .required and not self .nullable ):
273
+ return f"Union[{ inner_prop_string } ]"
274
+ return f"Optional[Union[{ inner_prop_string } ]]"
295
275
296
276
def get_imports (self , * , prefix : str ) -> Set [str ]:
297
277
"""
@@ -363,13 +343,10 @@ def get_enum(name: str) -> Optional["EnumProperty"]:
363
343
364
344
def get_type_string (self , no_optional : bool = False ) -> str :
365
345
""" 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 )
367
346
368
- def get_query_method_arg (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_method_arg (no_optional , self , self .reference .class_name )
347
+ if no_optional or (self .required and not self .nullable ):
348
+ return self .reference .class_name
349
+ return f"Optional[{ self .reference .class_name } ]"
373
350
374
351
def get_imports (self , * , prefix : str ) -> Set [str ]:
375
352
"""
@@ -428,13 +405,9 @@ def template(self) -> str: # type: ignore
428
405
429
406
def get_type_string (self , no_optional : bool = False ) -> str :
430
407
""" Get a string representation of type that should be used when declaring this property """
431
- return type_string (no_optional , self , self .reference .class_name )
432
-
433
- def get_query_method_arg (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_method_arg (no_optional , self , self .reference .class_name )
408
+ if no_optional or (self .required and not self .nullable ):
409
+ return self .reference .class_name
410
+ return f"Optional[{ self .reference .class_name } ]"
438
411
439
412
def get_imports (self , * , prefix : str ) -> Set [str ]:
440
413
"""
@@ -617,27 +590,3 @@ def property_from_data(
617
590
return _property_from_data (name = name , required = required , data = data )
618
591
except ValidationError :
619
592
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_method_arg (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