Skip to content

Keep the precision of ~= in python_full_version markers - #1001

Open
sohailshk wants to merge 1 commit into
python-poetry:mainfrom
sohailshk:fix/tilde-eq-python-full-version
Open

sohailshk wants to merge 1 commit into
python-poetry:mainfrom
sohailshk:fix/tilde-eq-python-full-version

Conversation

@sohailshk

Copy link
Copy Markdown

A python_full_version marker with fewer than three segments is padded to three ("3.8" becomes "3.8.0"). That is fine for ==, >= and friends, but for the compatible release operator the number of segments decides the upper bound:

  • ~= "3.8" means >= 3.8, == 3.*
  • ~= "3.8.0" means >= 3.8.0, == 3.8.*
>>> from poetry.core.version.markers import parse_marker
>>> m = parse_marker('python_full_version ~= "3.8"')
>>> str(m)
'python_full_version ~= "3.8.0"'
>>> m.validate({"python_full_version": "3.9.1"})
False   # packaging.markers.Marker(...).evaluate gives True

The same marker on a dependency (Dependency.create_from_pep_508('foo; python_full_version ~= "3.8"')) is rewritten to ~= "3.8.0", so the marker that ends up in poetry.lock and in exported requirements excludes Python 3.9 and later.

This also changes what gets locked and installed. With released poetry and requires-python = ">=3.10", poetry lock leaves such a dependency out of poetry.lock entirely. With requires-python = ">=3.8" it is locked as ~= "3.8.0", and poetry install on Python 3.13 skips it.

When a python_full_version marker is merged with a python_version marker, the result goes through the same padding, e.g. python_full_version ~= "3.9" and python_version > "3.8" became python_full_version ~= "3.9.0" (False on 3.10.1, while the original is True). The padding is now skipped for ~= there too.

Tests added in tests/version/test_markers.py; the second one covers the merge path.

The implementation and tests were written with AI assistance.

  • Added tests for changed code.
  • Updated documentation for changed code.

A two-segment value such as python_full_version ~= "3.8" was padded to "3.8.0", which turned >=3.8,==3.* into >=3.8.0,==3.8.*.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/poetry/core/version/markers.py" line_range="453" />
<code_context>
+                name == "python_full_version"
+                and not swapped_name_value
+                # the precision of a compatible release clause is significant
+                and self._operator != "~="
+            ):
                 # fix precision of python_full_version marker
</code_context>
<issue_to_address>
**One-segment markers export invalidly**

When a dependency uses `python_full_version ~= "3"`, `SingleMarker` preserves `python_full_version ~= "3"`, and the version parser accepts it as a range when the release has fewer than two parts. The resulting marker violates PEP 440’s minimum precision for `~=`, so downstream packaging tools reject it when evaluating the exported marker.

Reject one-segment compatible-release markers, or otherwise ensure they are emitted in a valid PEP 440 form.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/poetry/core/version/markers.py:453


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

name == "python_full_version"
and not swapped_name_value
# the precision of a compatible release clause is significant
and self._operator != "~="

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · One-segment markers export invalidly

When a dependency uses python_full_version ~= "3", SingleMarker preserves python_full_version ~= "3", and the version parser accepts it as a range when the release has fewer than two parts. The resulting marker violates PEP 440’s minimum precision for ~=, so downstream packaging tools reject it when evaluating the exported marker.

Reject one-segment compatible-release markers, or otherwise ensure they are emitted in a valid PEP 440 form.

Prompt for AI agents
In `src/poetry/core/version/markers.py` at line 453:

**One-segment markers export invalidly**

When a dependency uses `python_full_version ~= "3"`, `SingleMarker` preserves `python_full_version ~= "3"`, and the version parser accepts it as a range when the release has fewer than two parts. The resulting marker violates PEP 440’s minimum precision for `~=`, so downstream packaging tools reject it when evaluating the exported marker.

Reject one-segment compatible-release markers, or otherwise ensure they are emitted in a valid PEP 440 form.

This branch has not been deployed

No deployments
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