forked from dmontagu/fastapi-utils
/
enums.py
37 lines (27 loc) · 1.2 KB
/
enums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import annotations
from enum import Enum
from typing import List
from .camelcase import snake2camel
class StrEnum(str, Enum):
"""
StrEnum subclasses that create variants using `auto()` will have values equal to their names
Enums inheriting from this class that set values using `enum.auto()` will have variant values equal to their names
"""
@staticmethod
def _generate_next_value_(name: str, start: int, count: int, last_values: List[str]) -> str:
"""
Uses the name as the automatic value, rather than an integer
See https://docs.python.org/3/library/enum.html#using-automatic-values for reference
"""
return name
class CamelStrEnum(str, Enum):
"""
CamelStrEnum subclasses that create variants using `auto()` will have values equal to their camelCase names
"""
@staticmethod
def _generate_next_value_(name: str, start: int, count: int, last_values: List[str]) -> str:
"""
Uses the camelCase name as the automatic value, rather than an integer
See https://docs.python.org/3/library/enum.html#using-automatic-values for reference
"""
return snake2camel(name, start_lower=True)