1
1
from dataclasses import dataclass , field
2
2
from typing import Any , ClassVar , Dict , List , Optional
3
3
4
+ from openapi_python_client import utils
5
+
4
6
from .reference import Reference
5
7
6
8
@@ -15,6 +17,11 @@ class Property:
15
17
constructor_template : ClassVar [Optional [str ]] = None
16
18
_type_string : ClassVar [str ]
17
19
20
+ python_name : str = field (init = False )
21
+
22
+ def __post_init__ (self ) -> None :
23
+ self .python_name = utils .snake_case (self .name )
24
+
18
25
def get_type_string (self ) -> str :
19
26
""" Get a string representation of type that should be used when declaring this property """
20
27
if self .required :
@@ -31,13 +38,13 @@ def to_string(self) -> str:
31
38
default = None
32
39
33
40
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 } "
35
42
else :
36
- return f"{ self .name } : { self .get_type_string ()} "
43
+ return f"{ self .python_name } : { self .get_type_string ()} "
37
44
38
45
def transform (self ) -> str :
39
46
""" What it takes to turn this object into a native python type """
40
- return self .name
47
+ return self .python_name
41
48
42
49
def constructor_from_dict (self , dict_name : str ) -> str :
43
50
""" How to load this property from a dict (used in generated model from_dict function """
@@ -57,6 +64,7 @@ class StringProperty(Property):
57
64
_type_string : ClassVar [str ] = "str"
58
65
59
66
def __post_init__ (self ) -> None :
67
+ super ().__post_init__ ()
60
68
if self .default is not None :
61
69
self .default = f'"{ self .default } "'
62
70
@@ -132,6 +140,7 @@ class EnumListProperty(Property):
132
140
constructor_template : ClassVar [str ] = "enum_list_property.pyi"
133
141
134
142
def __post_init__ (self ) -> None :
143
+ super ().__post_init__ ()
135
144
self .reference = Reference .from_ref (self .name )
136
145
137
146
def get_type_string (self ) -> str :
@@ -149,6 +158,7 @@ class EnumProperty(Property):
149
158
reference : Reference = field (init = False )
150
159
151
160
def __post_init__ (self ) -> None :
161
+ super ().__post_init__ ()
152
162
self .reference = Reference .from_ref (self .name )
153
163
inverse_values = {v : k for k , v in self .values .items ()}
154
164
if self .default is not None :
@@ -163,7 +173,7 @@ def get_type_string(self) -> str:
163
173
164
174
def transform (self ) -> str :
165
175
""" Output to the template, convert this Enum into a JSONable value """
166
- return f"{ self .name } .value"
176
+ return f"{ self .python_name } .value"
167
177
168
178
def constructor_from_dict (self , dict_name : str ) -> str :
169
179
""" How to load this property from a dict (used in generated model from_dict function """
@@ -204,7 +214,7 @@ def get_type_string(self) -> str:
204
214
205
215
def transform (self ) -> str :
206
216
""" Convert this into a JSONable value """
207
- return f"{ self .name } .to_dict()"
217
+ return f"{ self .python_name } .to_dict()"
208
218
209
219
210
220
@dataclass
0 commit comments