11from dataclasses import dataclass , field
22from typing import Any , ClassVar , Dict , List , Optional
33
4+ from openapi_python_client import utils
5+
46from .reference import Reference
57
68
@@ -15,6 +17,11 @@ class Property:
1517 constructor_template : ClassVar [Optional [str ]] = None
1618 _type_string : ClassVar [str ]
1719
20+ python_name : str = field (init = False )
21+
22+ def __post_init__ (self ) -> None :
23+ self .python_name = utils .snake_case (self .name )
24+
1825 def get_type_string (self ) -> str :
1926 """ Get a string representation of type that should be used when declaring this property """
2027 if self .required :
@@ -31,13 +38,13 @@ def to_string(self) -> str:
3138 default = None
3239
3340 if default is not None :
34- return f"{ self .name } : { self .get_type_string ()} = { self .default } "
41+ return f"{ self .python_name } : { self .get_type_string ()} = { self .default } "
3542 else :
36- return f"{ self .name } : { self .get_type_string ()} "
43+ return f"{ self .python_name } : { self .get_type_string ()} "
3744
3845 def transform (self ) -> str :
3946 """ What it takes to turn this object into a native python type """
40- return self .name
47+ return self .python_name
4148
4249 def constructor_from_dict (self , dict_name : str ) -> str :
4350 """ How to load this property from a dict (used in generated model from_dict function """
@@ -57,6 +64,7 @@ class StringProperty(Property):
5764 _type_string : ClassVar [str ] = "str"
5865
5966 def __post_init__ (self ) -> None :
67+ super ().__post_init__ ()
6068 if self .default is not None :
6169 self .default = f'"{ self .default } "'
6270
@@ -132,6 +140,7 @@ class EnumListProperty(Property):
132140 constructor_template : ClassVar [str ] = "enum_list_property.pyi"
133141
134142 def __post_init__ (self ) -> None :
143+ super ().__post_init__ ()
135144 self .reference = Reference .from_ref (self .name )
136145
137146 def get_type_string (self ) -> str :
@@ -149,6 +158,7 @@ class EnumProperty(Property):
149158 reference : Reference
150159
151160 def __post_init__ (self ) -> None :
161+ super ().__post_init__ ()
152162 inverse_values = {v : k for k , v in self .values .items ()}
153163 if self .default is not None :
154164 self .default = f"{ self .reference .class_name } .{ inverse_values [self .default ]} "
@@ -162,11 +172,14 @@ def get_type_string(self) -> str:
162172
163173 def transform (self ) -> str :
164174 """ Output to the template, convert this Enum into a JSONable value """
165- return f"{ self .name } .value"
175+ return f"{ self .python_name } .value"
166176
167177 def constructor_from_dict (self , dict_name : str ) -> str :
168178 """ How to load this property from a dict (used in generated model from_dict function """
169- return f'{ self .reference .class_name } ({ dict_name } ["{ self .name } "]) if "{ self .name } " in { dict_name } else None'
179+ constructor = f'{ self .reference .class_name } ({ dict_name } ["{ self .name } "])'
180+ if not self .required :
181+ constructor += f' if "{ self .name } " in { dict_name } else None'
182+ return constructor
170183
171184 @staticmethod
172185 def values_from_list (l : List [str ], / ) -> Dict [str , str ]:
@@ -200,22 +213,22 @@ def get_type_string(self) -> str:
200213
201214 def transform (self ) -> str :
202215 """ Convert this into a JSONable value """
203- return f"{ self .name } .to_dict()"
216+ return f"{ self .python_name } .to_dict()"
204217
205218
206219@dataclass
207220class DictProperty (Property ):
208221 """ Property that is a general Dict """
209222
210- _type_string : ClassVar [str ] = "Dict"
223+ _type_string : ClassVar [str ] = "Dict[Any, Any] "
211224
212225
213226_openapi_types_to_python_type_strings = {
214227 "string" : "str" ,
215228 "number" : "float" ,
216229 "integer" : "int" ,
217230 "boolean" : "bool" ,
218- "object" : "Dict" ,
231+ "object" : "Dict[Any, Any] " ,
219232}
220233
221234
0 commit comments