Skip to content

fix: format_python_constraint IndexError on unions outside PYTHON_VERSION - #971

Open
otiscuilei wants to merge 1 commit into
python-poetry:mainfrom
otiscuilei:fix/format-python-constraint-unknown-union
Open

fix: format_python_constraint IndexError on unions outside PYTHON_VERSION#971
otiscuilei wants to merge 1 commit into
python-poetry:mainfrom
otiscuilei:fix/format-python-constraint-unknown-union

Conversation

@otiscuilei

@otiscuilei otiscuilei commented Sep 9, 2026

Copy link
Copy Markdown

format_python_constraint walks a hardcoded PYTHON_VERSION list (currently through 3.14) and then takes accepted[0] as the lower bound. A VersionUnion that matches none of those series raises IndexError while writing wheel Requires-Python.

That path is used for legacy [tool.poetry.dependencies] python = "…" (PEP 621 requires-python bypasses the helper). Adjacent ranges such as ~3.15 || ~3.16 merge into a single VersionRange and are fine. Non-adjacent unions do not merge and crash:

from pathlib import Path
from poetry.core.masonry.metadata import Metadata
from poetry.core.packages.project_package import ProjectPackage

pkg = ProjectPackage("foo", "1.0")
pkg.root_dir = Path(".")
pkg.python_versions = "~3.15 || ~3.17"  # skip 3.16; also "4.0 || 4.1"
Metadata.from_package(pkg)
# IndexError: list index out of range

^3.15 / >=3.15 / ~3.15 are ranges and already work. Falling back to str(union) would emit poetry || syntax, which is not valid PEP 440.

When nothing in PYTHON_VERSION matched, take the union's own lower bound and keep excluding the known series — the same approximation the helper already uses for in-list unions (3.8 || 3.10 also allows later 3.x not in the list). This does not add 3.15 to PYTHON_VERSION (see #961).

  • Added tests for changed code.
  • Updated documentation for changed code. — n/a

@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 reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the formatting is wrong, published package metadata could allow installation on an unsupported Python version or reject a supported one, and reverting would not change metadata already released. The impact is bounded to affected package artifacts and can be repaired with a subsequent release.


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

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