Skip to content

Enum by-value lookup no longer compares hashable argument to unhashable values #125710

Closed

Description

@alien-valkyrie

Bug report

Bug description:

When defining enum members with hashable values (e.g. frozensets), it was previously (prior to 3.13) possible to look those members up by passing in unhashable values (e.g. sets) that compare equal to those original values.
This no longer works as of 3.13 (specifically GH-112514), since the unhashable argument is now only compared to unhashable enum values. I haven't been able to find any documentation of this change in the What's New or even the changelog, so I'm assuming it's not entirely intended.

Reproducible example:

from enum import Enum

class Directions(Enum):
    DOWN_ONLY = frozenset({"sc"})
    UP_ONLY = frozenset({"cs"})
    UNRESTRICTED = frozenset({"sc", "cs"})

dirs = {"sc"}
# in 3.13, this raises ValueError
the_dirs = Directions(dirs)
assert the_dirs is Directions.DOWN_ONLY

Note that in this example (which is based on our use case) there's the obvious user-side fix of Directions(frozenset(dirs)), but if there happens to be another hashable-equal-to-unhashable use case outside of (frozen)sets, such a workaround might not exist.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Activity

  1. self-assigned this
    on Oct 18, 2024
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.13only security fixes
    3.14bugs and security fixes
    on Oct 19, 2024
  3. added a commit that references this issue on Oct 22, 2024
  4. added a commit that references this issue on Oct 22, 2024
  5. added a commit that references this issue on Oct 22, 2024
  6. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions