Skip to content

epy.StrEnum case-insensitive equality violates hash consistency #843

Open

Description

@sansynx

Description

epy.StrEnum advertises case-insensitive equality, but its hash remains case-sensitive. Two distinct members whose values differ only by case compare equal while both remain in a set. An enum member can also compare equal to an uppercase string but fail as a key lookup for that string. This violates Python's equality/hash contract and can produce inconsistent deduplication and dictionary behavior.

Reproduced on main at d18f006162a8da2b7eb79de2e1e1cac0d07774c5 with Python 3.12.14.

Reproduction

This occurs with a single enum member. Requires Git, uv and an activated Python 3.12 virtual environment. Install the exact tested revision:

uv pip install "etils[epy] @ git+https://github.com/google/etils@d18f006162a8da2b7eb79de2e1e1cac0d07774c5"

Save this as repro.py:

import enum
import sys
from etils import epy

class Mode(epy.StrEnum):
    NORMAL = enum.auto()

member = Mode.NORMAL
probe = 'NORMAL'
normalized = Mode(probe)
observed = {
    'python': sys.version.split()[0],
    'member_value': member.value,
    'equal_to_uppercase': member == probe,
    'list_contains_uppercase': probe in [member],
    'set_contains_uppercase': probe in {member},
    'dictionary_lookup_uppercase_key': {probe: 'found'}.get(member),
    'normalized_is_member': normalized is member,
    'normalized_set_contains': normalized in {member},
    'normalized_dictionary_lookup': {member: 'found'}.get(normalized),
    'hashes_equal': hash(member) == hash(probe),
}
for key, value in observed.items():
    print(f'{key}: {value!r}')
assert observed['member_value'] == 'normal'
assert observed['equal_to_uppercase'] is True
assert observed['list_contains_uppercase'] is True
assert observed['set_contains_uppercase'] is False
assert observed['dictionary_lookup_uppercase_key'] is None
assert observed['normalized_is_member'] is True
assert observed['normalized_set_contains'] is True
assert observed['normalized_dictionary_lookup'] == 'found'
assert observed['hashes_equal'] is False

Run python repro.py. Observed output with etils 1.14.0:

python: '3.12.14'
member_value: 'normal'
equal_to_uppercase: True
list_contains_uppercase: True
set_contains_uppercase: False
dictionary_lookup_uppercase_key: None
normalized_is_member: True
normalized_set_contains: True
normalized_dictionary_lookup: 'found'
hashes_equal: False

The member compares equal to 'NORMAL', but its different hash prevents consistent set membership and dictionary lookup. Expected: values comparing equal obey Python's equality/hash contract. The normalized enum-construction control works without changing the hash or equality implementation.
The custom __eq__ in etils/epy/py_utils.py lowercases both strings, while __hash__ delegates to the normal string hash of each original value. etils/epy/py_utils_test.py checks a lowercase member against its exact lowercase string, but not differently cased equal values in sets or dictionaries.

Suggested direction

Reconcile equality and hashing together in etils/epy/py_utils.py, with regressions in etils/epy/py_utils_test.py. Lowercasing the enum hash alone would still leave it unequal to the hash of every differently cased string that currently compares equal, so the fix needs a deliberate choice about cross-case string equality and hashability. Case-insensitive construction through _missing_ can remain independent of that choice.

Activity

  1. mohmedmm commented on Nov 3, 2026

    @mohmedmm

    opened #849 with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions