jsons.serializers.default_enum   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 8
dl 0
loc 19
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_enum_serializer() 0 15 2
1
from enum import EnumMeta
2
3
4
def default_enum_serializer(obj: EnumMeta,
5
                            *,
6
                            use_enum_name: bool = True,
7
                            **_) -> str:
8
    """
9
    Serialize the given obj. By default, the name of the enum element is
10
    returned.
11
    :param obj: an instance of an enum.
12
    :param use_enum_name: determines whether the name or the value should be
13
    used for serialization.
14
    :param _: not used.
15
    :return: ``obj`` serialized as a string.
16
    """
17
    attr = 'name' if use_enum_name else 'value'
18
    return getattr(obj, attr)
19