Skip to main content

Base class for creating enumerated constants that are also subclasses of str

Project description

A backport of (copy and paste from) python 3.11’s StrEnum class for >=3.8.6:

See the design discussion, and Ethan Furman’s first and second PR with this implementation.

A slightly different implementation would likely be compatible with lower python versions; PRs are welcome if they pass the test suite. The existing (reference) implementation should still be the one used on supported versions.

Install with pip install backports.strenum, and use with:

import sys

if sys.version_info >= (3, 11):
    from enum import StrEnum
else:
    from backports.strenum import StrEnum

class MyStrEnum(StrEnum):
    POTATO = "potato"
    ORANGE = "orange"
    SPADE = "spade"

MyStrEnum.POTATO == "potato"  # True
MyStrEnum.ORANGE.upper() == "ORANGE"  # True
str(MyStrEnum.SPADE) == "spade"  # True

From version 1.3.0, this package cannot be installed on python >=3.11. It shouldn’t be used on them anyway. Make sure that in your package, this is a conditional dependency.

Gotchas

A number of behaviours relating to the treatment of enum classes as containers of their members (e.g. iterating and containment checks) will be changing in python 3.12.

This package intends only to allow pre-3.11 users to get 3.11-like behaviour; after that, stick with the standard library.


These are the docs provided with python 3.11:

class enum.StrEnum

StrEnum is the same as Enum, but its members are also strings and can be used in most of the same places that a string can be used. The result of any string operation performed on or with a StrEnum member is not part of the enumeration.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backports_strenum-1.3.1.tar.gz (7.3 kB view hashes)

Uploaded Source

Built Distribution

backports_strenum-1.3.1-py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page