Compare versions with PEP 440 equivalence - #523
Open
dchaudhari7177 wants to merge 1 commit into
Open
Conversation
dchaudhari7177
force-pushed
the
fix/505-pep440-version-compare
branch
from
August 14, 2026 16:23
e2fa6e3 to
15b7276
Compare
`get_mismatches()` compared the pinned and installed versions as raw
strings, so any pair that differs only in spelling was reported as a
mismatch. The reported case is trailing-zero normalization: pip-compile
writes `gdal==3.10` while the installed distribution reports `3.10.0`,
and `pip-lock` failed with
* Package gdal has version 3.10 but you have version 3.10.0 installed.
Compare with `packaging.version.Version` instead, which implements PEP
440 equivalence. Exact string equality is still checked first, so the
common case does not pay for parsing, and versions that are not valid
PEP 440 (`InvalidVersion`) fall back to that string comparison rather
than raising.
Genuinely different versions are unaffected: differing release segments
(`3.10` vs `3.10.1`) and differing epochs (`1!1.0` vs `1.0`) are still
mismatches, and both are covered by new tests.
Fixes adamchainz#505
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
dchaudhari7177
force-pushed
the
fix/505-pep440-version-compare
branch
from
August 15, 2026 04:02
15b7276 to
d80c6fa
Compare
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 #505.
Problem
get_mismatches()compares the pinned and the installed version as raw strings:so any pair that differs only in spelling is reported as a mismatch. The reported case is trailing-zero normalization —
pip-compilewritesgdal==3.10into the.txt, the installed distribution reports3.10.0, andpip-lockfails with:even though
Version("3.10") == Version("3.10.0")under PEP 440.Change
Compare with
packaging.version.Version:InvalidVersionfalls back to that string comparison instead of raising, so a non-PEP-440 version string behaves exactly as it does today.One thing to flag
The issue says packaging "is already a transitive dependency of pip-lock". That isn't quite right —
pyproject.tomlcurrently hasdependencies = [], so this PR adds pip-lock's first runtime dependency. I went ahead because pip-lock's workflow is built around pip-compile output and pip-tools itself depends onpackaging, so it is present in practice in any environment where pip-lock is used.If you would rather keep
dependencies = [], I am happy to swap this for a dependency-free normalization of the release segment instead — that covers the reported trailing-zero case but not the pre-release spellings. Say the word and I will push it.Tests
Six tests added to
TestGetMismatches. Verified they fail against the old comparison — with the one-line change reverted,test_no_mismatches_trailing_zeroandtest_no_mismatches_prerelease_spellingboth fail; the four mismatch tests are regression guards that pass either way.3.10vs3.10.0,1.0alpha1vs1.0a13.10vs3.10.1), differing epoch (1!1.0vs1.0), two different non-PEP-440 strings39 passedlocally,ruff checkandruff format --checkclean.🤖 Generated with Claude Code