Skip to main content

Deprecate individual Enum members with warnings, for Enum/IntEnum/StrEnum/custom mixins.

Project description

🧩 Enum-Deprecation

PyPI Python Versions License Tests

Add deprecation warnings to your Python Enum members — works for all Enum subclasses (Enum, IntEnum, StrEnum, or your own mixins).


✨ Features

  • ✅ Works with Enum, IntEnum, StrEnum, or any custom subclass

  • ⚙️ Emits DeprecationWarning when deprecated members are accessed:

    • by attribute: MyEnum.OLD
    • by name: MyEnum["OLD"]
    • by value: MyEnum(value)
  • 🧾 Fully type-checked (mypy --strict) and tested on Python 3.11 – 3.13

  • 🧱 Compatible with SQLAlchemy enums

  • 🛡 Zero runtime dependencies


📦 Installation

pip install enum-deprecation

Python ≥ 3.11 is required (because of StrEnum support and typing.Self).


🧠 Usage

from enum import Enum, IntEnum, StrEnum, auto
from enum_deprecation import allow_deprecation, deprecated
import warnings

warnings.simplefilter("default")  # enable DeprecationWarnings for demo


# --- Basic Enum --------------------------------------------------------------

class MyEnum(Enum, metaclass=allow_deprecation):
    A = auto()
    OLD = deprecated()        # auto value, but deprecated
    B = auto()
    OLD2 = deprecated(42)     # explicit value
    OLD3 = deprecated("X", msg_tpl="{attr} will go away soon")


print(MyEnum.A)
print(MyEnum.OLD)     # emits a DeprecationWarning
print(MyEnum["OLD"])  # warning again
print(MyEnum(42))     # warning for explicit value

💡 Works with StrEnum, IntEnum, and custom mixins

class MyStrEnum(StrEnum, metaclass=allow_deprecation):
    OK = "ok"
    OLD = deprecated("deprecated")  # string value
class MyIntEnum(IntEnum, metaclass=allow_deprecation):
    NEW = 1
    OLD = deprecated(2)

⚙️ Custom deprecation messages

class Status(Enum, metaclass=allow_deprecation):
    ACTIVE = 1
    LEGACY = deprecated(2, msg_tpl="Status {attr} is legacy")

You can format {attr} anywhere in the message template.


🧩 How it works

The metaclass allow_deprecation wraps EnumMeta and:

  1. Records all members defined as deprecated(...).
  2. Replaces deprecated(auto) with the proper sentinel so auto() still works.
  3. Emits a DeprecationWarning whenever those members are retrieved via attribute, name, or value lookup.

It’s completely transparent to the rest of the enum machinery — the resulting class is still a regular Enum subclass.


🧰 SQLAlchemy compatibility

The resulting enums can be used directly with SQLAlchemy:

from sqlalchemy import Enum as SAEnum, Integer
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase): ...

class Thing(Base):
    __tablename__ = "thing"
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    status: Mapped[MyEnum] = mapped_column(SAEnum(MyEnum, name="status_enum"))

⚠️ Note: accessing deprecated enum values during ORM loading will emit warnings. If you wish to suppress them during DB round-trip, wrap with a small TypeDecorator that uses MyEnum.__members__ directly.


🧪 Testing

tox           # runs tests under Python 3.11–3.13
pytest -v     # run in the current environment
mypy .        # type checking

🧾 License

MIT License © 2025 Marcin Kornat


🧭 Links

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

enum_deprecation-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enum_deprecation-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file enum_deprecation-0.1.0.tar.gz.

File metadata

  • Download URL: enum_deprecation-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.16.11-lqx1-1-lqx

File hashes

Hashes for enum_deprecation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 95fd69bf63608528aff4f2fa4439180eb8f8218d51be3419d81ca5109dddd093
MD5 5f563658e19061e308a10727f13b3965
BLAKE2b-256 4616b3df84343f35bb63be8c921a5b6ba35e6533d01a7cc686900520f564e17f

See more details on using hashes here.

File details

Details for the file enum_deprecation-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: enum_deprecation-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.16.11-lqx1-1-lqx

File hashes

Hashes for enum_deprecation-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 445cd7584a5065d12bd9d3841205b8bc8889110f65f66dd985c9d70ae259c17b
MD5 3a1f7129ed4daf6b9b9415c5b7d9d194
BLAKE2b-256 8f08c1144243068b5724d6316772665638cf1bb4c1d31ca00ac1a497000210ae

See more details on using hashes here.

Supported by

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