@@ -51,9 +51,16 @@ def get_type_string(self, no_optional: bool = False) -> str:
5151 Args:
5252 no_optional: Do not include Optional even if the value is optional (needed for isinstance checks)
5353 """
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 )
5764
5865 def get_imports (self , * , prefix : str ) -> Set [str ]:
5966 """
@@ -63,24 +70,42 @@ def get_imports(self, *, prefix: str) -> Set[str]:
6370 prefix: A prefix to put before any relative (local) module names. This should be the number of . to get
6471 back to the root of the generated client.
6572 """
66- if self .nullable or not self .required :
73+ if self .required and self .nullable :
6774 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" }
6879 return set ()
6980
7081 def to_string (self ) -> str :
7182 """ How this should be declared in a dataclass """
7283 if self .default :
7384 default = self .default
7485 elif not self .required :
75- default = "None "
86+ default = "UNSET "
7687 else :
7788 default = None
7889
7990 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 } "
8192 else :
8293 return f"{ self .python_name } : { self .get_type_string ()} "
8394
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+
84109
85110@dataclass
86111class StringProperty (Property ):
@@ -222,9 +247,13 @@ class ListProperty(Property, Generic[InnerProp]):
222247
223248 def get_type_string (self , no_optional : bool = False ) -> str :
224249 """ 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 ()} ]" )
228257
229258 def get_imports (self , * , prefix : str ) -> Set [str ]:
230259 """
@@ -254,9 +283,15 @@ def get_type_string(self, no_optional: bool = False) -> str:
254283 """ Get a string representation of type that should be used when declaring this property """
255284 inner_types = [p .get_type_string () for p in self .inner_properties ]
256285 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 )
260295
261296 def get_imports (self , * , prefix : str ) -> Set [str ]:
262297 """
@@ -328,10 +363,13 @@ def get_enum(name: str) -> Optional["EnumProperty"]:
328363
329364 def get_type_string (self , no_optional : bool = False ) -> str :
330365 """ 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 )
331367
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 )
335373
336374 def get_imports (self , * , prefix : str ) -> Set [str ]:
337375 """
@@ -390,9 +428,13 @@ def template(self) -> str: # type: ignore
390428
391429 def get_type_string (self , no_optional : bool = False ) -> str :
392430 """ 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 )
396438
397439 def get_imports (self , * , prefix : str ) -> Set [str ]:
398440 """
@@ -575,3 +617,27 @@ def property_from_data(
575617 return _property_from_data (name = name , required = required , data = data )
576618 except ValidationError :
577619 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