Skip to content

gh-158856: Fix enum membership for values of a different hashability - #160576

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:gh-158856-enum-contains-hashability
Open

tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:gh-158856-enum-contains-hashability

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Since #159250 made _hashable_values_ a set, value in EnumClass returns False for a value that equals a member value but has a different hashability. A set value is not found even for its own member: set.__contains__() looks up a set key as a frozenset instead of raising TypeError, so the list of unhashable values is never checked.

from enum import Enum
class Sets(Enum):
    A = {1, 2}
class Magic(Enum):
    PNG = b'\x89PNG'
Sets.A.value in Sets            # False, True on 3.14
frozenset({1, 2}) in Sets       # False, True on 3.14
bytearray(b'\x89PNG') in Magic  # False, True on 3.14

Check the unhashable values whenever the set lookup misses, and compare against the hashable values on TypeError. The regression is not in a release, so there is no NEWS entry.

…ility

Since _hashable_values_ became a set, `value in EnumClass` missed values
equal to a member value but of a different hashability, including a set
member's own value. Check the unhashable values on a miss and compare
against the hashable values on TypeError.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant