Typing stubs for enum34

1.1.8 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to define and use an enumeration, consistent with the `enum34` library's API (which `types-enum34` provides stubs for). The `types-enum34` package itself does not introduce new runtime functionality; it merely provides static type hints. This code is designed to work with the `enum34` runtime package. For Python versions 3.4 and above, the `enum` module is part of the standard library, rendering `enum34` (and thus `types-enum34`) largely obsolete for new projects.

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

view raw JSON →