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

Compare long numeric prerelease identifiers without integer conversion - #488

Merged
tomschr merged 2 commits into
python-semver:masterfrom
SAY-5:fix-long-numeric-prerelease
Oct 1, 2026
Merged

tomschr merged 2 commits into
python-semver:masterfrom
SAY-5:fix-long-numeric-prerelease

Conversation

@SAY-5

@SAY-5 SAY-5 commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #487.

A valid version with a 5,000-digit numeric prerelease parses successfully but raises ValueError when 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:

  • Baseline: six regression failures; 80 comparison cases passed.
  • Candidate 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.
  • Ruff found no new lint or formatting findings against the exact baseline. Both whole-file commands remain nonzero for 74 existing diagnostics and 10 existing formatting groups.

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.

@tomschr tomschr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @SAY-5! I have only one suggestion (see below).

Comment thread src/semver/version.py Resolved
@tomschr
tomschr merged commit 1d51591 into python-semver:master Oct 1, 2026
17 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is_valid() accepts versions that compare() cannot order (int_max_str_digits)

2 participants