|
1 | 1 | __all__ = ["EnumProperty"]
|
2 | 2 |
|
3 |
| -from typing import Any, ClassVar, Dict, List, Optional, Set, Type, Union, cast |
| 3 | +from typing import Any, ClassVar, Dict, Optional, Sequence, Set, Type, Union |
4 | 4 |
|
5 | 5 | import attr
|
6 | 6 |
|
@@ -50,24 +50,30 @@ def get_imports(self, *, prefix: str) -> Set[str]:
|
50 | 50 | return imports
|
51 | 51 |
|
52 | 52 | @staticmethod
|
53 |
| - def values_from_list(values: Union[List[str], List[int]]) -> Dict[str, ValueType]: |
| 53 | + def values_from_list( |
| 54 | + values: Union[Sequence[str], Sequence[int]], case_sensitive_enums: bool = False |
| 55 | + ) -> Dict[str, ValueType]: |
54 | 56 | """Convert a list of values into dict of {name: value}, where value can sometimes be None"""
|
55 | 57 | output: Dict[str, ValueType] = {}
|
56 | 58 |
|
57 |
| - for i, value in enumerate(values): |
58 |
| - value = cast(Union[str, int], value) |
| 59 | + for value in values: |
59 | 60 | if isinstance(value, int):
|
60 | 61 | if value < 0:
|
61 | 62 | output[f"VALUE_NEGATIVE_{-value}"] = value
|
62 | 63 | else:
|
63 | 64 | output[f"VALUE_{value}"] = value
|
64 | 65 | continue
|
65 |
| - if value and value[0].isalpha(): |
66 |
| - key = value.upper() |
| 66 | + |
| 67 | + if case_sensitive_enums: |
| 68 | + sanitized_key = utils.case_insensitive_snake_case(value) |
67 | 69 | else:
|
68 |
| - key = f"VALUE_{i}" |
69 |
| - if key in output: |
70 |
| - raise ValueError(f"Duplicate key {key} in Enum") |
71 |
| - sanitized_key = utils.snake_case(key).upper() |
| 70 | + sanitized_key = utils.snake_case(value.lower()).upper() |
| 71 | + if not value or not value[0].isalpha(): |
| 72 | + sanitized_key = f"LITERAL_{sanitized_key}" |
| 73 | + |
| 74 | + if sanitized_key in output: |
| 75 | + raise ValueError(f"Duplicate key {sanitized_key} in Enum") |
| 76 | + |
72 | 77 | output[sanitized_key] = utils.remove_string_escapes(value)
|
| 78 | + |
73 | 79 | return output
|
0 commit comments