You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Version equality with strings and tuples violates the hash contract #506
BugError, flaw or fault to produce incorrect or unexpected resultsDesignIdeas, suggestions, musings about design questionsRelease_3.x.yOnly for the major release 3Release_4.x.yOnly for the major release 4
What OS are you using? (Add more in the Environment section)
Windows
Situation
Version.parse("1.2.3") compares equal to both the string "1.2.3" and tuple (1, 2, 3), but its hash differs from theirs. Looking up an equal string or tuple in a set/dict keyed by the Version fails. String equality is documented in docs/usage/determine-version-equality.rst; Version is also documented as usable as a dict key.
Output on main commit f3c7138120d7f49e190bd7c555c6e7eb155b482d:
True False None
True False
Expected behavior
Python requires equal hashable objects to have equal hashes. Either an equal string/tuple should find the Version key, or cross-type equality should be reconsidered so the equality/hash contract holds. Version.__hash__ currently hashes its version-parts tuple, which cannot match both the equal string and tuple hashes.
Environment
Python 3.14.8 on Windows/amd64; reproduced from the current repository source at f3c7138120d7f49e190bd7c555c6e7eb155b482d.
Thanks for reporting this @agammann. I agree that the current behavior violates Python’s equality/hash contract. I’m considering the fix, but there’s a compatibility conflict: Version is documented as comparing equal to strings, and it’s also documented as hashable for use in dictionaries and sets.
Keeping both behaviors can’t satisfy the hash contract. SemVer comparison ignores build metadata, so one Version can compare equal to multiple distinct strings, which have different hashes. Changing equality to only compare Version objects would fix that, but would break documented behavior and likely require a major release. Making Version unhashable would preserve cross-type equality but break its use as a dictionary key or set member.
I’m not preparing a major release right now, so I’d appreciate your view: would you favor deprecating cross-type equality now and removing it in a future major release, or keeping the current behavior until then? I’m open to another approach if you see one.
Thanks for weighing the compatibility tradeoff. I would deprecate equality with strings and tuples now, then remove it in the next major release while keeping Version hashable. During the transition, the docs and release notes could warn that mixing Version with strings or tuples as dict/set keys is unsafe and point users to explicit Version.parse(...) or version-part conversion. A runtime warning in __eq__ might be noisy in ordinary collection operations, so a documented deprecation may be the better first step. Making Version unhashable now would also break a documented use case.
BugError, flaw or fault to produce incorrect or unexpected resultsDesignIdeas, suggestions, musings about design questionsRelease_3.x.yOnly for the major release 3Release_4.x.yOnly for the major release 4
Activity
tomschr commentedon Nov 1, 2026
Thanks for reporting this @agammann. I agree that the current behavior violates Python’s equality/hash contract. I’m considering the fix, but there’s a compatibility conflict: Version is documented as comparing equal to strings, and it’s also documented as hashable for use in dictionaries and sets.
Keeping both behaviors can’t satisfy the hash contract. SemVer comparison ignores build metadata, so one Version can compare equal to multiple distinct strings, which have different hashes. Changing equality to only compare Version objects would fix that, but would break documented behavior and likely require a major release. Making Version unhashable would preserve cross-type equality but break its use as a dictionary key or set member.
I’m not preparing a major release right now, so I’d appreciate your view: would you favor deprecating cross-type equality now and removing it in a future major release, or keeping the current behavior until then? I’m open to another approach if you see one.
Thanks a lot!
agammann commentedon Nov 2, 2026
Thanks for weighing the compatibility tradeoff. I would deprecate equality with strings and tuples now, then remove it in the next major release while keeping
Versionhashable. During the transition, the docs and release notes could warn that mixingVersionwith strings or tuples as dict/set keys is unsafe and point users to explicitVersion.parse(...)or version-part conversion. A runtime warning in__eq__might be noisy in ordinary collection operations, so a documented deprecation may be the better first step. MakingVersionunhashable now would also break a documented use case.Deprecate cross-type Version equality in semver 3
Deprecate cross-type Version equality in semver 3 (#507)