Skip to content

Pattern matching on Enum.name fails to distinguish between members with duplicate values #127375

@1957-001B

Description

@1957-001B

Bug report

Bug description:

# How to reproduce

from enum import Enum

class Registers(Enum):
    SP = 31   # Stack pointer
    XZR = 31  # Zero register

# Example
for token in ["XZR"]:
    match token:
        case Registers.XZR._name_:
            print("Matched XZR")
        case Registers.SP._name_:
            print("Matched SP")
        case _:
            print("No match")

# Current behavior: Always matches SP case first because SP is defined first
# Expected behavior: Should match XZR case when checking against XZR._name_

The issue appears to be that when pattern matching against _name_ of enum members with duplicate values, Python always matches the first enum member with that value, regardless of which _name_ is being matched against. This makes it impossible to differentiate between enum members with the same value in pattern matching contexts.
This behavior is particularly problematic in cases like ARM assembly parsing where different registers (SP and XZR) share the same value (31) but have distinct semantic meanings and need to be handled differently.

much obliged,

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions