• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

domdfcoding / enum_tools / 5278844857

pending completion
5278844857

push

github

domdfcoding
Bump version v0.9.0.post1 -> v0.10.0

1 of 1 new or added line in 1 file covered. (100.0%)

445 of 508 relevant lines covered (87.6%)

1.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/enum_tools/utils.py
1
#!/usr/bin/env python3
2
#
3
#  utils.py
4
"""
2✔
5
General utility functions.
6
"""
7
#
8
#  Copyright (c) 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
9
#
10
#  This program is free software; you can redistribute it and/or modify
11
#  it under the terms of the GNU General Public License as published by
12
#  the Free Software Foundation; either version 3 of the License, or
13
#  (at your option) any later version.
14
#
15
#  This program is distributed in the hope that it will be useful,
16
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
#  GNU General Public License for more details.
19
#
20
#  You should have received a copy of the GNU General Public License
21
#  along with this program; if not, write to the Free Software
22
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
#  MA 02110-1301, USA.
24
#
25

26
# stdlib
27
import inspect
2✔
28
from enum import Enum, EnumMeta, Flag
2✔
29
from typing import Tuple, Type
2✔
30

31
# 3rd party
32
from typing_extensions import Protocol, runtime_checkable
2✔
33

34
__all__ = ["HasMRO", "is_enum", "is_enum_member", "is_flag", "get_base_object"]
2✔
35

36

37
@runtime_checkable
2✔
38
class HasMRO(Protocol):
2✔
39
        """
40
        :class:`typing.Protocol` for classes that have a method resolution order magic method (``__mro__``).
41
        """
42

43
        @property
2✔
44
        def __mro__(self) -> Tuple[Type]: ...
2✔
45

46

47
def is_enum(obj: Type) -> bool:
2✔
48
        """
49
        Returns :py:obj:`True` if ``obj`` is an :class:`enum.Enum`.
50

51
        :param obj:
52
        """
53

54
        # The enum itself is subclass of EnumMeta; enum members subclass Enum
55
        return isinstance(obj, EnumMeta)
2✔
56

57

58
def is_enum_member(obj: Type) -> bool:
2✔
59
        """
60
        Returns :py:obj:`True` if ``obj`` is an :class:`enum.Enum` member.
61

62
        :param obj:
63
        """
64

65
        # The enum itself is subclass of EnumMeta; enum members subclass Enum
66
        return isinstance(obj, Enum)
2✔
67

68

69
def is_flag(obj: Type) -> bool:
2✔
70
        """
71
        Returns :py:obj:`True` if ``obj`` is an :class:`enum.Flag`.
72

73
        :param obj:
74
        """
75

76
        # The enum itself is subclass of EnumMeta; enum members subclass Enum
77
        if is_enum(obj) and isinstance(obj, HasMRO):
2✔
78
                return Flag in inspect.getmro(obj)
2✔
79
        else:
80
                return False
2✔
81

82

83
def get_base_object(enum: Type[HasMRO]) -> Type:
2✔
84
        """
85
        Returns the object type of the enum's members.
86

87
        If the members are of indeterminate type then the :class:`object` class is returned.
88

89
        :param enum:
90

91
        :rtype:
92

93
        :raises TypeError: If ``enum`` is not an Enum.
94
        """
95

96
        try:
2✔
97
                mro = inspect.getmro(enum)
2✔
98
        except AttributeError:
2✔
99
                raise TypeError("not an Enum")
2✔
100

101
        if Flag in mro:
2✔
102
                mro = mro[:mro.index(Flag)]
2✔
103
        elif Enum in mro:
2✔
104
                mro = mro[:mro.index(Enum)]
2✔
105
        else:
106
                raise TypeError("not an Enum")
2✔
107

108
        mro = mro[1:]
2✔
109

110
        for obj in mro:
2✔
111
                if not isinstance(obj, EnumMeta):
2✔
112
                        return obj
2✔
113

114
        return object
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc