@@ -187,6 +187,23 @@ def values_from_list(values: list[str] | list[int]) -> dict[str, ValueType]:
187
187
"""Convert a list of values into dict of {name: value}, where value can sometimes be None"""
188
188
output : dict [str , ValueType ] = {}
189
189
190
+ # We normally would like to make nice-looking Python constant names for enum values, so that "myValue"
191
+ # becomes MY_VALUE, etc. However, that won't work if an enum has two values that differ only by case
192
+ # (which is allowed in OpenAPI).
193
+ use_case_sensitive_names = False
194
+ for i , value1 in enumerate (values ):
195
+ if use_case_sensitive_names :
196
+ break
197
+ for j , value2 in enumerate (values ):
198
+ if (
199
+ i != j
200
+ and isinstance (value1 , str )
201
+ and isinstance (value2 , str )
202
+ and value1 .upper () == value2 .upper ()
203
+ ):
204
+ use_case_sensitive_names = True
205
+ break
206
+
190
207
for i , value in enumerate (values ):
191
208
value = cast (Union [str , int ], value )
192
209
if isinstance (value , int ):
@@ -196,11 +213,14 @@ def values_from_list(values: list[str] | list[int]) -> dict[str, ValueType]:
196
213
output [f"VALUE_{ value } " ] = value
197
214
continue
198
215
if value and value [0 ].isalpha ():
199
- key = value . upper ()
216
+ key = value
200
217
else :
201
218
key = f"VALUE_{ i } "
202
219
if key in output :
203
220
raise ValueError (f"Duplicate key { key } in Enum" )
204
- sanitized_key = utils .snake_case (key ).upper ()
221
+ if use_case_sensitive_names :
222
+ sanitized_key = utils .sanitize (key .replace (" " , "_" ))
223
+ else :
224
+ sanitized_key = utils .snake_case (key .upper ()).upper ()
205
225
output [sanitized_key ] = utils .remove_string_escapes (value )
206
226
return output
0 commit comments