Skip to main content

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.

Release files for backports.strenum 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for backports.strenum 1.3.1
File Size Uploaded
backports_strenum-1.3.1.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for backports.strenum 1.3.1
File Interpreter ABI Platform
backports_strenum-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / backports_strenum-1.3.1.tar.gz

Download URL backports_strenum-1.3.1.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
77c52407342898497714f0596e86188bb7084f89063226f4ba66863482f42414
BLAKE2b-256 checksum
How to use checksums
35c72ed54c32fed313591ffb21edbd48db71e68827d43a61938e5a0bc2b6ec91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / backports_strenum-1.3.1-py3-none-any.whl

Download URL backports_strenum-1.3.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdcfe36dc897e2615dc793b7d3097f54d359918fc448754a517e6f23044ccf83
BLAKE2b-256 checksum
How to use checksums
d65056cf20e2ee5127b603b81d5a69580a1a325083e2b921aa8f067da83927c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.2.8

2 release files

1.2.4

1 release file

1.1.1

1 release file

1.0.4

1 release file

1.0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page