Fix Version.difference with a local variant of the same version#953
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix Version.difference with a local variant of the same version#953apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
Version("2.12.1").difference(Version("2.12.1+cpu")) returned self
unchanged, which still allows 2.12.1+cpu — the subtraction made no
progress, sending poetry's solver into an infinite loop when the same
release was available from multiple sources with different local tags.
Delegate to the equivalent single-point VersionRange, whose difference
arithmetic can represent the split, yielding >=2.12.1,<2.12.1+cpu.
Resolves python-poetry/poetry#10965
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In the new
differencebranch, consider explicitly checkingnot self.is_local()andother.is_local()(rather than relying solely onself.allows(other)) so the special-case delegation toVersionRange.differenceis clearly limited to the non-local vs local-variant scenario and remains robust ifallows()semantics evolve.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the new `difference` branch, consider explicitly checking `not self.is_local()` and `other.is_local()` (rather than relying solely on `self.allows(other)`) so the special-case delegation to `VersionRange.difference` is clearly limited to the non-local vs local-variant scenario and remains robust if `allows()` semantics evolve.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Author
|
Re the Sourcery suggestion: at that point in the method |
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.
Resolves: python-poetry/poetry#10965
Problem
As diagnosed by @dimbleby in python-poetry/poetry#10965:
Version.difference()only checksother.allows(self). Whenotheris a local variant ofself(whichselfallows via weak equality, but which does not allowselfback), the method returnsselfunchanged — a constraint that still allows the version that was just subtracted. In the solver this means excluding2.12.1+cpumakes no progress, which manifests as an infinite loop / unbounded memory growth onpoetry lockwhen the same release is available from multiple sources with different local tags (e.g. torch from PyPI and+cpufrom the PyTorch index).Fix
When
selfis a non-local version andotheris one of its local variants, delegate to the equivalent single-pointVersionRange(self, self, include_min=True, include_max=True), whose difference arithmetic can represent the split. The result for the example above is>=2.12.1,<2.12.1+cpu:This matches the approximation
VersionRange.differencealready produces for the analogous case (e.g.[2.0.0, 2.12.1].difference(2.12.1+cpu)→>=2.0.0,<2.12.1+cpu), so the behavior is consistent across constraint types. All otherVersion.differencepaths are unchanged (equal versions, unrelated versions, local-minus-public all behave exactly as before).Verification
test_difference_local_version(fails onmain, passes with this fix).tests/constraints/: 1000 passed;tests/version/: 892 passed; full suite (minus network-dependenttests/integration/): 2917 passed, 1 pre-existing environment failure unrelated to this change (test_valid_trove_classifiers, missing optionaltrove_classifiersmodule locally).ruff check/ruff formatclean on changed files.Disclosure
This fix was developed with the assistance of an AI tool (Claude Code), with the approach, diff and test results reviewed and owned by me. I'll respond to review feedback personally.