← 输入框|github.com|刷新
Skip to content

Version equality with strings and tuples violates the hash contract #506

Open

Description

@agammann

Which version of Python is the problem with?

3.14

What semver version are you using?

3.1.0 (main source)

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.

How to reproduce

from semver import Version
v = Version.parse("1.2.3")
print(v == "1.2.3", "1.2.3" in {v}, {v: "hit"}.get("1.2.3"))
print(v == (1, 2, 3), (1, 2, 3) in {v})

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.

Activity

  1. tomschr commented on Nov 1, 2026

    @tomschr
    Member

    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!

  2. agammann commented on Nov 2, 2026

    @agammann
    Author

    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.

  3. added
    Release_3.x.y
    Bug
    Design
    Release_4.x.y
    on Nov 4, 2026
  4. added theissue type on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugDesignRelease_3.x.yRelease_4.x.y

Type

Fields

SIze

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions