Repository navigation
Conversation
_nat_cmp compares all-digit prerelease identifiers numerically, so Version(1, 2, 3, prerelease="1") and Version(1, 2, 3, prerelease="01") are equal. __hash__ hashed the raw prerelease string, so those two equal versions got different hashes. That breaks the equal-versions-have-equal -hashes invariant from python-semver#283 and lets both live as distinct keys in a set or dict, or raises KeyError on a key that compares equal to one present. Normalize numeric identifiers in __hash__ the same way _nat_cmp does, so equal versions hash equally. Build metadata stays excluded, as python-semver#283 set.
|
Thank you for all your efforts! I was unsure and had to look into the spec. Section 9 of the Semantic Versioning 2.0.0 specification explicitly prohibits leading zeroes in numeric pre-release identifiers:
In other words, a Version If I'm not mistaken, NPM's semver has the same approach. Comparing As I strictly follow the semver spec, I'm sorry I will have to reject this PR. However, we can discuss if this library should allow a strict and loose mode similar to NPM. This isn't implemented yet, but would require some deeper refactoring of the code. |
|
You're right, and thanks for digging into the spec. Validate-at-construction is the correct fix location: Section 9 makes |
Equal versions can hash differently when a prerelease identifier is numeric
#283 established that equal versions must have equal hashes and fixed it for build metadata. This is the same invariant for the numeric-prerelease case it did not cover.
__eq__compares prereleases through_nat_cmp, which reads all-digit identifiers as integers, so these two versions are equal:__hash__hashed the raw prerelease string, so the two equal versions land on different hashes and break set and dict membership.Scope, stated up front
This is not reachable through
Version.parse: the parser's regex rejects a leading-zero numeric identifier like01. It is reachable through the publicVersion(...)constructor and.replace(), which store the prerelease without validating it. So the impact is limited to versions built that way, and I did not want to overstate it.Fix
Normalize numeric prerelease identifiers in
__hash__exactly as_nat_cmp(and therefore__eq__) already do, so equal versions hash equally. Build metadata stays excluded, matching #283.Added a regression test next to
test_equal_versions_have_equal_hashes; it fails without the change. The full suite passes.