0
# types-enum34
1
2
Type stubs for enum34, a backport of Python 3.4's enum module to earlier Python versions. This package provides comprehensive type annotations for the enum34 library, enabling static type checking with mypy and other type checkers in Python 2.7 codebases.
3
4
## Package Information
5
6
- **Package Name**: types-enum34
7
- **Language**: Python (Type Stubs)
8
- **Installation**: `pip install types-enum34`
9
- **Target Platform**: Python 2.7
10
- **Purpose**: Type annotations for enum34 backport library
11
12
## Core Imports
13
14
```python
15
import enum
16
```
17
18
Common imports for specific enum components:
19
20
```python
21
from enum import Enum, IntEnum, Flag, IntFlag, EnumMeta, unique, auto
22
```
23
24
## Basic Usage
25
26
```python
27
from enum import Enum, IntEnum, unique
28
29
# Basic enumeration
30
class Color(Enum):
31
RED = 1
32
GREEN = 2
33
BLUE = 3
34
35
# Integer enumeration
36
class Status(IntEnum):
37
INACTIVE = 0
38
ACTIVE = 1
39
PENDING = 2
40
41
# Unique enumeration (prevents duplicate values)
42
@unique
43
class Direction(Enum):
44
NORTH = 1
45
SOUTH = 2
46
EAST = 3
47
WEST = 4
48
49
# Usage
50
print(Color.RED.name) # 'RED'
51
print(Color.RED.value) # 1
52
print(Status.ACTIVE == 1) # True (IntEnum)
53
```
54
55
## Capabilities
56
57
### Base Enum Class
58
59
The fundamental enumeration class that provides the core enum functionality with named constants and member access.
60
61
```python { .api }
62
class Enum(metaclass=EnumMeta):
63
name: str
64
value: Any
65
_name_: str
66
_value_: Any
67
_member_names_: list[str]
68
_member_map_: dict[str, Enum]
69
_value2member_map_: dict[int, Enum]
70
_ignore_: Union[str, list[str]] # Python 3.7+
71
_order_: str
72
__order__: str
73
74
@classmethod
75
def _missing_(cls, value: object) -> Any: ...
76
77
@staticmethod
78
def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any: ...
79
80
def __new__(cls: type[Self], value: object) -> Self: ...
81
def __dir__(self) -> list[str]: ...
82
def __format__(self, format_spec: str) -> str: ...
83
def __hash__(self) -> Any: ...
84
def __reduce_ex__(self, proto: object) -> Any: ...
85
```
86
87
### Enum Metaclass
88
89
The metaclass that provides enumeration behavior including iteration, membership testing, and member access by name.
90
91
```python { .api }
92
class EnumMeta(ABCMeta):
93
def __iter__(self: type[_T]) -> Iterator[_T]: ...
94
def __reversed__(self: type[_T]) -> Iterator[_T]: ...
95
def __contains__(self, member: object) -> bool: ...
96
def __getitem__(self: type[_T], name: str) -> _T: ...
97
def __len__(self) -> int: ...
98
99
@property
100
def __members__(self: type[_T]) -> Mapping[str, _T]: ...
101
```
102
103
### Integer Enumeration
104
105
Enumeration where members are also integers, providing both enum behavior and integer arithmetic compatibility.
106
107
```python { .api }
108
class IntEnum(int, Enum):
109
value: int
110
```
111
112
### Flag Enumeration
113
114
Enumeration designed for bitwise operations, allowing flag combinations using OR, AND, XOR, and NOT operators.
115
116
```python { .api }
117
class Flag(Enum):
118
def __contains__(self: _T, other: _T) -> bool: ...
119
def __bool__(self) -> bool: ...
120
def __or__(self: Self, other: Self) -> Self: ...
121
def __and__(self: Self, other: Self) -> Self: ...
122
def __xor__(self: Self, other: Self) -> Self: ...
123
def __invert__(self: Self) -> Self: ...
124
```
125
126
### Integer Flag Enumeration
127
128
Flag enumeration where members are also integers, supporting bitwise operations with both flags and integer values.
129
130
```python { .api }
131
class IntFlag(int, Flag):
132
def __or__(self: Self, other: Union[int, Self]) -> Self: ...
133
def __and__(self: Self, other: Union[int, Self]) -> Self: ...
134
def __xor__(self: Self, other: Union[int, Self]) -> Self: ...
135
__ror__ = __or__
136
__rand__ = __and__
137
__rxor__ = __xor__
138
```
139
140
### Auto Value Generation
141
142
Sentinel class for automatic value generation in enum definitions.
143
144
```python { .api }
145
class auto(IntFlag):
146
value: Any
147
```
148
149
### Uniqueness Decorator
150
151
Decorator function that ensures all enum values are unique, raising ValueError for duplicate values.
152
153
```python { .api }
154
def unique(enumeration: _S) -> _S:
155
"""
156
Decorator that ensures only unique enum values exist.
157
158
Parameters:
159
- enumeration: Enum class to validate
160
161
Returns:
162
The same enum class with uniqueness enforced
163
164
Raises:
165
ValueError: If duplicate values are found
166
"""
167
```
168
169
## Type Variables
170
171
```python { .api }
172
_T = TypeVar("_T")
173
_S = TypeVar("_S", bound=type[Enum])
174
```
175
176
## Constants
177
178
```python { .api }
179
_auto_null: Any # Internal constant for auto value handling
180
```
181
182
## Usage Examples
183
184
### Creating Basic Enums
185
186
```python
187
from enum import Enum
188
189
class Animal(Enum):
190
DOG = 1
191
CAT = 2
192
BIRD = 3
193
194
# Access members
195
print(Animal.DOG.name) # 'DOG'
196
print(Animal.DOG.value) # 1
197
198
# Iteration
199
for animal in Animal:
200
print(f"{animal.name}: {animal.value}")
201
202
# Membership testing
203
print(Animal.DOG in Animal) # True
204
```
205
206
### Working with IntEnum
207
208
```python
209
from enum import IntEnum
210
211
class Priority(IntEnum):
212
LOW = 1
213
MEDIUM = 2
214
HIGH = 3
215
216
# IntEnum members are also integers
217
print(Priority.HIGH == 3) # True
218
print(Priority.HIGH > Priority.LOW) # True
219
print(Priority.HIGH + 1) # 4
220
```
221
222
### Flag Operations
223
224
```python
225
from enum import Flag, auto
226
227
class Permission(Flag):
228
READ = auto()
229
WRITE = auto()
230
EXECUTE = auto()
231
232
# Combine flags
233
read_write = Permission.READ | Permission.WRITE
234
all_perms = Permission.READ | Permission.WRITE | Permission.EXECUTE
235
236
# Test flags
237
print(Permission.READ in read_write) # True
238
print(Permission.EXECUTE in read_write) # False
239
```
240
241
### Using unique Decorator
242
243
```python
244
from enum import Enum, unique
245
246
@unique
247
class Color(Enum):
248
RED = 1
249
GREEN = 2
250
BLUE = 3
251
# CRIMSON = 1 # Would raise ValueError due to duplicate value
252
```
253
254
### Auto Value Generation
255
256
```python
257
from enum import Enum, auto
258
259
class Direction(Enum):
260
NORTH = auto()
261
SOUTH = auto()
262
EAST = auto()
263
WEST = auto()
264
265
print(Direction.NORTH.value) # 1
266
print(Direction.SOUTH.value) # 2
267
```