Skip to content

fix(solver): apply active extras before splitting Python markers - #11301

Open
notatallshaw wants to merge 2 commits into
python-poetry:mainfrom
notatallshaw:fix/active-extra-python-markers
Open

notatallshaw wants to merge 2 commits into
python-poetry:mainfrom
notatallshaw:fix/active-extra-python-markers

Conversation

@notatallshaw

@notatallshaw notatallshaw commented Nov 30, 2026 •

Copy link
Copy Markdown

Resolves: #11300
Resolves: #11262

Alternative to: #11264 (which fixes #11262 but not #11300)

This PR checks the selected extras before deciding which dependency rules apply to the project’s Python range. In #11300, sktime’s dl extra requires pytorch-forecasting version 1.8.0 or newer throughout Python 3.12, so the fallback that allows any version never applies.

Compared to #11264, which fixes how Poetry combines results in the lockfile, this PR also skips that fallback before starting a separate solve.

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

@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit a2ac6c0

#11262 poetry lock >= 2.5.0: overlapping markers for a package locked in two versions (torch / nvidia-cuda-nvrtc) · 1 of 1 met

# Requirement Status Notes
1 Keep markers for different locked versions of a package mutually exclusive so both versions are not installed in the same environment. ✅ Met The change evaluates package-local extras before resolving duplicate dependency markers and when merging override branches; the new platform-branch test checks that only the matching version applies.

#11300 `poetry lock` stalls with sktime's `dl` and `notebooks` extras · 1 of 1 met

# Requirement Status Notes
1 Avoid starting a separate solve for a fallback requirement that does not apply to the selected extras and Python range. ✅ Met The change applies active extras before checking marker relevance against the Python constraint, and adds tests for filtering irrelevant override branches.

@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 2 issues

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

## Individual Comments

### Comment 1
<location path="src/poetry/puzzle/provider.py" line_range="97" />
<code_context>
+        dependencies = []
+        for dep in package.extras.get(name, ()):
+            if dep.name == package.name:
+                pending.extend(sorted(dep.extras))
+            else:
+                dependencies.append(dep)
</code_context>
<issue_to_address>
**Conditional extras widen override regions**

When a selected extra references another extra on the same package under a Python or platform marker, `_iter_package_extras` queues the referenced extras without retaining the self-reference marker, and `get_override_marker` treats every yielded extra as active across the whole override. Override results therefore include environments where the nested extra is not enabled, so the merged package markers can be wrong.

Carry the self-reference marker through extra expansion and restrict each nested extra’s contribution to the environments where that marker applies.

Also at `src/poetry/puzzle/provider.py:116`.
</issue_to_address>

### Comment 2
<location path="tests/puzzle/test_solver.py" line_range="5868" />
<code_context>
+            ">=3.6,<3.13",
+            ["dask"],
+            {"3.6": "2021.12.0", "3.7": "2021.12.0", "3.12": "2021.3.0"},
+        ),
+    ],
+)
</code_context>
<issue_to_address>
**Missing lower bound goes unnoticed**

When the impossible-conflict case drops the applicable `leaf>=2` requirement, the solver drops the always-applicable `leaf>=2` requirement in the `impossible-conflict` case, and the root’s unconstrained `leaf` dependency still selects 2.0. `test_solver_limits_override_markers_to_the_owners_extras` therefore passes its 2.0 assertion without showing that this requirement was applied.

Add an assertion that distinguishes enforcing `leaf>=2` from resolving the root’s wildcard dependency alone.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if the marker branches are combined incorrectly, the solver can select or record the wrong dependency version for some Python or extra combinations. Reverting restores the old solver behavior, but lockfiles already generated with the wrong resolution would need to be regenerated; that state is bounded and repairable.

Blocking findings: src/poetry/puzzle/provider.py:97, tests/puzzle/test_solver.py:5868


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

Comment thread src/poetry/puzzle/provider.py Resolved
Comment thread tests/puzzle/test_solver.py Resolved
@notatallshaw
notatallshaw force-pushed the fix/active-extra-python-markers branch from 97baf5b to 42c0c1e Compare November 30, 2026 06:06
@dimbleby

Copy link
Copy Markdown
Contributor

Thanks - continuing to trust my AI on such matters - it agrees that this fixes cases that #11264 does not, but proposes that there are parts from the earlier PR worth keeping. Any chance you can include them here?

#11301 is the better fix, but #11264 has cleaner factoring worth keeping. Its expand_package_extras() returns a mapping from expanded extra names to their dependencies. The mapping doubles as the traversal’s visited set, and callers can use its keys directly. In #11301, the iterator tracks seen, then each caller builds another collection of the same names. I’d use the mapping in #11301 and keep it provider-local unless another module needs it.

I’d also carry over #11264’s direct regression for #11262 and its test that identically named extras on different packages don’t interfere. Both already pass on #11301, and neither is directly covered by its new tests. I wouldn’t carry over the uncovered-platform test: #11301’s version checks that case and that each supported platform gets the right dependencies.

@notatallshaw

notatallshaw commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Any chance you can include them here?

I replaced this PR’s extra iterator with a private helper that returns a mapping from extra names to dependencies, both callers use it, simplifying bookkeeping.

I brought over #11264’s regression for #11262 and its test for extras with the same name. I added a check for the lib version because #11264’s check for essential still passed when I deliberately mixed the packages’ extras.

I kept the platform test since it already checks x86_64, aarch64, and the uncovered case on s390x.

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

2 participants