Skip to content

fix(version): normalize all-digit local segments as integers - #20

Merged
jonyoder merged 1 commit into
mainfrom
local-digit-normalization
Aug 5, 2026
Merged

jonyoder merged 1 commit into
mainfrom
local-digit-normalization

Conversation

@jonyoder

@jonyoder jonyoder commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

What

An all-digit segment of a local version label now normalizes as an integer, so 1.0+007
renders as 1.0+7 and 1.0+ubuntu-007 as 1.0+ubuntu.7. 1.0+foo0100 is unchanged.

Why

PEP 440: "If a segment consists entirely of ASCII digits then that section should be
considered an integer"
, so its integer-normalization rule ("an integer version of 00
would normalize to 0"
) reaches it. We left it verbatim.

⚠️ The carve-out is easy to read backwards, and that is how this was missed. PEP 440
exempts integers inside an alphanumeric local segment — "such as 1.0+foo0100 which is
already in its normalized form"
. It does not exempt an all-digit segment. The reference
implementation does exactly that split: part.lower() if not part.isdigit() else int(part).

Ordering was never wrong, which is why nothing caught it

cmpkey already compares all-digit segments numerically, so 1.0+007 and 1.0+7 compared
equal before and after. The defect is the rendered string, and the one place that is
load-bearing is ===: PEP 440 defines arbitrary equality as string equality on the
normalized form, so ===1.0+7 failed to match 1.0+007 while upstream matched it.

The test asserts both halves — that Compare returns 0, and that the strings match — so
nobody later removes the normalization on the grounds that comparison already handles it. It
drives the === case through NewSpecifiers rather than only comparing String(), because
the specifier path is where a consumer would feel it.

How it was found, which is the part worth reading

A differential run compared this package against pypa/packaging 26.2 over every distinct
version string in a production PyPI snapshot
:

Distinct version strings compared 634,187 (100%, nothing sampled)
Parse disagreements 0 — both accept 634,150, both reject 37
Normalized-rendering disagreements 0 of 634,150
Packages whose sort order differs 0 of 680,711
Intra-package pairs implied to agree 983,548,294
Go comparator transitivity/antisymmetry violations (exhaustive) 0

That corpus could not possibly have found this bug. PEP 440 forbids PyPI from accepting
local version identifiers at all, so the real corpus contained zero of them — and local
labels are precisely what 0.2.0 changed. The finding came from the synthetic supplement added
because the real data could not exercise the new code.

Worth stating as a method: a clean differential over a large real corpus says nothing about
the shapes that corpus cannot contain. Check coverage of the shapes you changed, not the size
of the dataset.

Two further divergences, deliberately NOT changed here

  • 1.0.poſt1 (U+017F) parses here and is rejected upstream: Go's (?i) case-folds Unicode
    while PEP 440 scopes case-insensitivity to ascii letters. Real over-acceptance, but
    unreachable from PyPI (the corpus has zero non-ASCII), so it wants its own change.
  • We reject 1.0\xa0, 1.0\x85, 1.0  where upstream accepts them. Here we are right:
    PEP 440 enumerates the strippable whitespace exhaustively as space, \t, \n, \r, \f,
    \v — exactly what this package implements. Python's Unicode \s is an upstream
    deviation, so this is intentionally left alone rather than "fixed" toward the reference.

Coverage the corpus did exercise

Epochs (891), pre-releases (161,293 across every spelling), post (40,905 including 354
implicit N-M), dev (174,031), implicit-number forms, release segments 1→9+, integers beyond
int64 (153 release / 47 dev), 1,736 non-canonical inputs, and 59 genuine ties across 56
packages. Absent from it: local labels, non-ASCII, surrounding whitespace — the first of
which is this PR.

Release

The CHANGELOG entry goes in the unreleased 0.2.0 Breaking section, beside the related
local-label entry, since it changes rendered output and 0.2.0 is not yet tagged. This should
land before that tag.

Verification

Both new tests verified to fail individually with only this fix reverted, and the Compare
assertion verified to pass either way — which is the evidence that the defect really is
rendering-only rather than an ordering bug in disguise. Full suite green, -race green,
gofmt clean, golangci-lint 0 issues.

PEP 440: "If a segment consists entirely of ASCII digits then that section
should be considered an integer", so its integer-normalization rule ("an integer
version of 00 would normalize to 0") reaches such a segment. We left it verbatim:
`1.0+007` rendered as `1.0+007` where pypa/packaging renders `1.0+7`.

⚠️ THE CARVE-OUT IS EASY TO READ BACKWARDS, and reading it backwards is how this
was missed. PEP 440's exemption is scoped to integers inside an ALPHANUMERIC
local segment -- "such as 1.0+foo0100 which is already in its normalized form."
It does NOT exempt an all-digit segment. So `foo0100` stays exactly as it is
while `007` becomes `7`, and the reference implementation does precisely that:
`part.lower() if not part.isdigit() else int(part)`.

ORDERING WAS NEVER WRONG, which is why nothing caught this. cmpkey already
compares all-digit segments numerically, so `1.0+007` and `1.0+7` compared equal
before and after -- verified, and asserted in the test so nobody "simplifies" the
normalization away on the grounds that comparison covers it. The defect is the
rendered STRING, and the one place that is load-bearing is `===`: PEP 440 defines
arbitrary equality as string equality on the normalized form, so `===1.0+7`
failed to match `1.0+007` while upstream matched it. The test drives that through
NewSpecifiers rather than only comparing String() output, because the specifier
path is where a consumer would actually feel it.

HOW IT WAS FOUND, because the provenance is the interesting part. A differential
run compared this package against pypa/packaging 26.2 over every distinct version
string in a production PyPI snapshot: 634,187 strings, with identical accept/reject
on all of them, identical normalized rendering on all 634,150 accepted, and
identical total order across all 680,711 packages.

That corpus could not possibly have found this bug. PEP 440 forbids the Python
Package Index from accepting local version identifiers at all, so the real corpus
contained ZERO of them -- and local labels are exactly what 0.2.0 changed. The
finding came from the synthetic supplement that was added precisely because the
real data could not exercise the new code. Worth recording as a method: a clean
differential over a large real corpus says nothing about the shapes that corpus
cannot contain, so check coverage of the shapes you changed rather than the size
of the dataset.

Also from that run, and NOT changed here:
- `1.0.poſt1` (U+017F) parses here and is rejected upstream, because Go's `(?i)`
  case-folds Unicode while PEP 440 scopes case-insensitivity to ascii letters.
  Over-acceptance, unreachable from PyPI, left for a follow-up.
- We reject `1.0\xa0`, `1.0\x85` and `1.0 `, which upstream accepts. Here WE
  are right: PEP 440 enumerates the strippable whitespace exhaustively as
  space, \t, \n, \r, \f, \v, which is what this package implements. Python's
  Unicode `\s` is an upstream deviation, so this is deliberately not "fixed".

CHANGELOG entry goes in the unreleased 0.2.0 Breaking section, beside the related
local-label entry, since it changes rendered output and 0.2.0 is not yet tagged.

Both new tests verified to fail individually with only this fix reverted, and the
Compare assertion verified to pass either way -- which is the evidence that the
defect really is rendering-only rather than an ordering bug in disguise.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jonyoder
jonyoder merged commit 7092ea0 into main Aug 5, 2026
2 checks passed
jonyoder added a commit that referenced this pull request Aug 5, 2026
#21)

PEP 508 makes a requirement's version specifier optional, and a requirement
without one accepts any version. The canonical spelling of that is an empty
specifier set, which is what upstream produces and what upstream treats as
containing everything -- measured against pypa/packaging 26.2:

    Requirement('flask').specifier   ->  <SpecifierSet('')>   len 0
      contains '3.0.0'    -> True
      contains '0.1'      -> True
      contains '99!1.0'   -> True
      contains '2.0.0rc1' -> True

Specifiers.Check answered false to all of those. "No constraint" was being read
as "no versions" instead of "all versions", which inverts every gate built on a
specifier set: a caller filtering candidates against an unconstrained set
discards all of them and concludes the package is unsatisfiable rather than
unconstrained.

THE PACKAGE WAS ALREADY INCONSISTENT WITH ITSELF, which is what makes this a
defect rather than a debatable choice. andCheck returns true for an empty
AND-group, because a conjunction over no constraints is vacuously true. Zero
OR-groups is the same statement one level up, and the two disagreed.

This is the root cause of the inversion fixed downstream in
posit-dev/go-pyresolver#8, where an absent Requires-Python -- 2,027,153 versions
in a production PyPI snapshot -- was read as "compatible with no interpreter at
all." That module's accessor stays as intention-revealing API and as protection
if it is ever built against an older version of this one.

⚠️ THIS IS A FAIL-OPEN CHANGE, so the blast radius was measured rather than
argued. NO INPUT STRING CAN REACH IT: every degenerate input either errors or
yields at least one group, verified across "", " ", "\t", ",", ",,", "()",
"( )", "|", "||", "-" and "*" through both NewSpecifiers and NewRSpecifiers
("*" yields one group, >=0.0.0). The only route to a zero-group Specifiers is
Go's zero value, which today gets the wrong answer anyway. That reachability
fact is now a test, so if a future change makes some input parse to zero groups
the risk assessment gets redone instead of silently invalidated.

Also verified: PPM is unaffected. It pins v0.1.1, and it never calls Check on a
possibly-empty value -- src/requirements/filter.go guards specs == "" and returns
true first, and PythonCheckSpecifier returns early on a parse error. That
workaround existing in the only other consumer is itself evidence that this is
the semantics callers expect and currently have to implement themselves.

TESTS PIN THREE WRONG IMPLEMENTATIONS, each verified to fail on its own:
- dropping the guard (the original defect);
- an unconditional `return true`, which would be a far worse bug than the one
  being fixed, caught by asserting a real constraint still EXCLUDES;
- treating the empty set as a ">=0.0.0" floor. That one is subtler than it
  looks and my first draft of these tests missed it: a dev or pre-release
  segment sorts BELOW the release it belongs to, so 0.0.0.dev0 and 0.0.0a1 are
  less than 0.0.0 and a >=0.0.0 floor wrongly excludes them. Upstream admits
  both under an empty set and rejects both under >=0.0.0.

One thing I got wrong while writing this and corrected: I assumed a ">=0.0.0"
floor would also wrongly exclude pre-releases generally. It does not. Upstream's
contains() admits 2.0.0rc1 under ">=1.0"; PEP 440's default pre-release exclusion
is applied by filter(), not by contains(). Conflating those two is a different
bug, and the test comment now says so explicitly so the distinction is not lost.

CHANGELOG goes in the unreleased 0.2.0 Breaking section, placed first in that
list so it does not textually collide with the entry in #20.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
jonyoder added a commit that referenced this pull request Aug 7, 2026
…23)

v0.2.0 WAS ALREADY CUT. It was tagged and pushed on 2026-08-03 at 3dfc5dc, and
proxy.golang.org has served it since:

    {"Version":"v0.2.0","Time":"2026-08-03T16:19:37Z",
     "Origin":{"Hash":"3dfc5dc4447367996e677fbf7f7008fa307624d1",...}}

Four commits landed after that tag -- #19, #20, #21, #22 -- and three of them
wrote their entries into the dated `## [0.2.0]` section rather than
`[Unreleased]`, on the stated reasoning that the tag had not been cut yet. So
the changelog claimed for v0.2.0 three changes that v0.2.0 does not contain:
the required comma between specifiers, the empty-specifier-set inversion, and
all-digit local segment normalization. `[Unreleased]` was empty.

A GO MODULE TAG IS IMMUTABLE once the proxy has seen it, so there is no version
of this that ends with v0.2.0 being correct. The entries move forward instead.

Two of the three narrow what parses and one changes rendered output, so per this
module's own stated policy -- breaking changes take a minor bump while major is
0 -- the next tag is 0.3.0, not 0.2.1.

VERIFIED that the 0.2.0 section is now byte-identical to what the tag shipped:
`git diff 3dfc5dc -- CHANGELOG.md` removes exactly one line, the [Unreleased]
compare link, which is replaced. Everything else is additive.

Also documents #19 under 0.3.0's Notes. It was NOTICE-only so it had no entry,
but it is the release's most consequential change for a redistributor: it credits
pkginfo and twine, whose material distribution/ ports uncredited through v0.2.0,
corrects pypa/packaging's license to the Apache-2.0-OR-BSD-2-Clause dual offer,
and deletes a claim that no source from the cited projects was incorporated --
false since tags/ landed on 2026-07-08, and therefore false in v0.1.0, v0.1.1,
v0.1.2 and v0.2.0 alike.

And adds the guardrail to CLAUDE.md, since the existing rule said entries go
under [Unreleased] without saying to verify the assumption that a dated section
is unreleased. CI does not validate the changelog.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@jonyoder
jonyoder deleted the local-digit-normalization branch August 27, 2026 14:38
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.

1 participant