Repository navigation
Compare long numeric prerelease identifiers without integer conversion - #488
Merged
Merged
Conversation
SonicHedghog
added a commit
to SonicHedghog/python-semver
that referenced
this pull request
Nov 18, 2026
str.isdigit() accepts superscripts like "²" for which int() fails, and since __hash__ now reaches _prerelease_key, the crash became reachable from hashing too. Switch to str.isdecimal() and normalize each character via unicodedata.decimal, which also avoids Python's integer-string length limit and removes int() from prerelease comparison entirely. Deliberately supersedes the eager ValueError introduced for these identifiers in python-semver#488: a prerelease like "²" now compares lexically as an alphanumeric identifier instead of raising. Test updated accordingly.
tomschr
added a commit
that referenced
this pull request
Nov 18, 2026
Fixes #515 * Fix hash contract: equal Versions must hash equal (#515) Version.__hash__ hashed to_tuple()[:4], keeping the raw prerelease string, while _nat_cmp normalizes numeric identifiers for comparison. A zero-padded prerelease (e.g. "007") compared equal to its normalized form ("7") but hashed differently, so equal Versions behaved as distinct set/dict keys — a contract violation live since 3.0.3. Hoist the prerelease normalization out of _nat_cmp into a shared _prerelease_key helper and build the hash from the normalized key, so comparison and hashing cannot drift again. Empty/absent prerelease maps to a canonical key. Adds parametrized coverage for zero-padded, multi-identifier, non-ASCII-digit, and empty-prerelease equalities (set/dict semantics included), plus a changelog fragment. * Treat non-decimal digits as alphanumeric identifiers str.isdigit() accepts superscripts like "²" for which int() fails, and since __hash__ now reaches _prerelease_key, the crash became reachable from hashing too. Switch to str.isdecimal() and normalize each character via unicodedata.decimal, which also avoids Python's integer-string length limit and removes int() from prerelease comparison entirely. Deliberately supersedes the eager ValueError introduced for these identifiers in #488: a prerelease like "²" now compares lexically as an alphanumeric identifier instead of raising. Test updated accordingly. * Fix GHA to use pinned versions from pyproject.toml --------- Co-authored-by: SonicHedghog <SonicHedghog@users.noreply.github.com> Co-authored-by: Tom Schraitle <tomschr@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #487.
A valid version with a 5,000-digit numeric prerelease parses successfully but raises
ValueErrorwhen compared under Python’s default integer-conversion limit. Compare ASCII numeric prerelease identifiers by normalized digit count and text, preserving numeric-before-text ordering without converting them to integers.The change retains constructor leading-zero comparison behavior, legacy Unicode handling and eager validation. It leaves core version parsing, release/build precedence and the optional native parser unchanged. Regression tests and a changelog fragment are included.
Validation on CPython 3.14.7 with the 4,300-digit limit unchanged:
tests/: 450 passed, one optional-native-backend skip. After annotation and test-literal/formatting-only cleanup, all 86 comparison cases passed again; the algorithm and test AST were unchanged.The supported-Python tox matrix, native backend, coverage and documentation/doctest builds were not run.
OpenAI Codex assisted with the implementation, tests, independent review and this description.