Skip to content

gh-139122: Reset the cached hash of C UUIDs in __init__ and __setstate__ - #160575

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:gh-139122-uuid-subclass-cached-hash
Open

tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:gh-139122-uuid-subclass-cached-hash

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Python subclasses of uuid.UUID are allocated with PyType_GenericAlloc(), so cached_hash starts at 0 instead of -1 and __init__() never resets it. With the C implementation every subclass instance hashes to 0 and is not found next to an equal UUID in a set or dict:

import uuid
class MyUUID(uuid.UUID): pass
hash(MyUUID(int=1))                  # 0
uuid.UUID(int=1) in {MyUUID(int=1)}  # False

Calling __init__() or __setstate__() on an existing UUID also kept its old cached hash. Reset cached_hash in both. The C implementation is unreleased, so there is no NEWS entry.

…etstate__

Subclass instances are allocated zeroed, so cached_hash started at 0 and
hash() returned 0. Re-initializing an existing UUID also kept the old
hash.
@bedevere-app bedevere-app Bot added awaiting review type-feature A feature request or enhancement labels Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant