Skip to content

Toggle enum member value

Toggle an enum member reference between the enum member object and its literal value. This is useful when a call site or assertion needs to switch between comparing enum members and comparing their serialized values.

How to invoke

Place the caret on an enum member reference or on a literal value with an expected enum type, then use ⌥⏎ / Alt+Enter -> "BetterPy: Toggle enum member / value".

Example

from enum import Enum


class Color(Enum):
    RED = "red"


payload = Color.RED

becomes:

payload = "red"

Running the intention on "red" changes it back to Color.RED when the expression has an expected Color type, such as an annotated assignment or annotated function parameter.

What it supports

  • Members of Enum, IntEnum, StrEnum, Flag, and IntFlag classes.
  • Qualified enum member references such as Color.RED.
  • Literal values whose expected type resolves to an enum class.
  • Enum references used inside expressions, assignments, and comparisons.

What it deliberately avoids

  • Bare names such as RED where the enum class is not part of the reference.
  • Literal values without an expected enum type annotation.
  • Attributes on non-enum classes.
  • Longer attribute chains such as Color.RED.name or Color.RED.custom.
  • Files outside your own project sources.

Notes

  • The intention changes call-site references only. It does not rewrite enum member declarations.
  • The feature is gated by the Toggle enum member value setting under Settings | BetterPy | Refactoring & code transformations | Type Editing.