Typing stubs for enum34
types-enum34 is a PEP 561 type stub package providing static type information for the `enum34` library. The `enum34` library itself is a backport of Python 3.4's standard library `enum` module, enabling enumeration support for older Python versions (2.4-3.3). This stub package allows type-checking tools like MyPy, PyCharm, and Pytype to analyze code that uses `enum34`. The current version is 1.1.8, released in January 2022, and its development cadence follows contributions to the `typeshed` project.
Warnings
- breaking Installing `enum34` (the runtime library for which `types-enum34` provides stubs) on Python 3.4 or newer environments can cause conflicts with the built-in `enum` module. This may lead to unexpected behavior, import issues, or break `pip` itself.
- gotcha `types-enum34` is a stub package for static type checking. It does not provide the `enum` runtime functionality. You must install the `enum34` package separately (e.g., `pip install enum34`) for the code to run in Python environments older than 3.4.
- deprecated The `enum` module became a part of the Python standard library in Python 3.4. The `enum34` backport (and consequently `types-enum34`) is primarily relevant for maintaining compatibility with Python 2.x and earlier Python 3.x versions. New projects targeting Python 3.4+ should use the built-in `enum` module directly and typically do not require `enum34` or `types-enum34`.
- gotcha There have been past issues with type checkers like MyPy correctly identifying `types-enum34` as Python 2 compatible when using `--py2` flags, or issues with metadata declarations in `typeshed` affecting stub compatibility.
Install
-
pip install types-enum34 -
pip install enum34
Imports
- Enum
from enum import Enum
Quickstart
from enum import Enum
from typing import TYPE_CHECKING
# This example demonstrates usage of enum34 (type-checked by types-enum34)
# Ensure 'enum34' is installed in your Python 2.x or pre-3.4 environment.
# For Python 3.4+, the 'enum' module is built-in.
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
def get_color_name(color_enum: Color) -> str:
return color_enum.name
def get_color_value(color_enum: Color) -> int:
return color_enum.value
if __name__ == '__main__':
my_color = Color.RED
print(f"Selected color: {my_color}")
print(f"Name: {get_color_name(my_color)}")
print(f"Value: {get_color_value(my_color)}")
# Example of type checking with mypy (assuming mypy is run externally)
# if TYPE_CHECKING:
# reveal_type(my_color)
# # The following would ideally be flagged by a type checker if `enum34` was in use and `types-enum34` enabled
# # get_color_name('invalid_string') # type: ignore