Skip to content

fix: don't crash when merging incompatible platform_release marker forms - #957

Closed
Cyrus580529 wants to merge 2 commits into
python-poetry:mainfrom
Cyrus580529:fix/platform-release-marker-merge-crash
Closed

Cyrus580529 wants to merge 2 commits into
python-poetry:mainfrom
Cyrus580529:fix/platform-release-marker-merge-crash

Conversation

@Cyrus580529

Copy link
Copy Markdown

Resolves: python-poetry/poetry-core#10978

platform_release != "1" parses to a version-based constraint, but the swapped form "a" not in platform_release parses to a generic string Constraint (this is intentional — the swapped form is a substring check, not a version comparison). _merge_single_markers assumed same-name markers always share a constraint type and crashed with AttributeError: 'Constraint' object has no attribute 'flatten' when trying to intersect the two.

Added a type check before merging: if the two constraints aren't both version-like, fall back to keeping them as separate ANDed/ORed markers (the existing fallback path for unmergeable markers), instead of crashing. Added regression tests.

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

@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, and left some high level feedback:

  • The type check in _merge_single_markers would be clearer and less error-prone as an explicit positive check (e.g., if not (isinstance(marker1.constraint, VersionConstraint) and isinstance(marker2.constraint, VersionConstraint)): return None) rather than comparing the boolean results of isinstance with !=.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The type check in `_merge_single_markers` would be clearer and less error-prone as an explicit positive check (e.g., `if not (isinstance(marker1.constraint, VersionConstraint) and isinstance(marker2.constraint, VersionConstraint)): return None`) rather than comparing the boolean results of `isinstance` with `!=`.

## Individual Comments

### Comment 1
<location path="tests/version/test_markers.py" line_range="1483-1486" />
<code_context>
+        # platform_release marker used to raise AttributeError while merging
+        # markers, because the two forms parse to incompatible constraint
+        # types (VersionConstraint vs. generic Constraint).
+        (
+            'platform_release != "1" and "a" not in platform_release',
+            {"platform_release": "1"},
+            False,
+        ),
+        (
</code_context>
<issue_to_address>
**suggestion (testing):** Add a corresponding `or`-combination regression case to cover MarkerUnion merging as well as MultiMarker merging.

These parametrized cases cover the `and` (`MultiMarker`) merge path for mixed `platform_release !=` and `"a" not in platform_release` constraints. However, `_merge_single_markers` is also used for `or` (`MarkerUnion`) combinations, and that path currently has no regression test. Please add at least one `or` case using the same mixed constraints, e.g.:

```python
(
    'platform_release != "1" or "a" not in platform_release',
    {"platform_release": "1"},
    False,  # or True, depending on intended semantics
),
(
    'platform_release != "1" or "a" not in platform_release',
    {"platform_release": "2"},
    True,
),
```

(or equivalent expectations for union semantics), so we verify that `or` expressions with these incompatible constraint types no longer crash and that both `_merge_single_markers` code paths are covered.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +1483 to +1486
(
'platform_release != "1" and "a" not in platform_release',
{"platform_release": "1"},
False,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Add a corresponding or-combination regression case to cover MarkerUnion merging as well as MultiMarker merging.

These parametrized cases cover the and (MultiMarker) merge path for mixed platform_release != and "a" not in platform_release constraints. However, _merge_single_markers is also used for or (MarkerUnion) combinations, and that path currently has no regression test. Please add at least one or case using the same mixed constraints, e.g.:

(
    'platform_release != "1" or "a" not in platform_release',
    {"platform_release": "1"},
    False,  # or True, depending on intended semantics
),
(
    'platform_release != "1" or "a" not in platform_release',
    {"platform_release": "2"},
    True,
),

(or equivalent expectations for union semantics), so we verify that or expressions with these incompatible constraint types no longer crash and that both _merge_single_markers code paths are covered.

@skdas20 skdas20 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.

I checked this out and ran it locally — the diagnosis and the fix both look right to me.

Verified: tests/version/test_markers.py is green (555 passed) with the patch applied.

I also poked at some neighbouring shapes that aren't in the added tests, and they all behave correctly with this change:

"a" not in platform_release and platform_release != "1"     # swapped form first
"a" not in platform_release or platform_release != "1"
"a" in platform_release and platform_release >= "2"          # 'in' + comparison
platform_release != "1" and "a" not in platform_release and platform_release != "2"   # 3-way

And python_version != "3.8" and "x" not in python_version still raises InvalidMarkerError rather than reaching the merge path at all — correct, since python_version is in PYTHON_VERSION_MARKERS and the swapped form was never valid there.

Re: overlap with #955 — worth noting for whoever triages these, since they look adjacent but are not the same bug:

  • This PR fixes the crash (AttributeError: 'Constraint' object has no attribute 'flatten'), which only reaches a version-like name (platform_release), where the two forms parse to different constraint types.

  • #955 fixes a silent wrong result on non-version-like names, where both forms parse to a generic Constraint and merging drops a term:

    >>> parse_marker('platform_version != "1" and "a" not in platform_version')
    "a" not in platform_version          # the != term is gone
    >>> _.validate({'platform_version': '1'})
    True                                  # should be False

    (same on sys_platform, platform_machine.)

So neither one subsumes the other. I applied both patches together: they don't conflict, and the full tests/version + tests/constraints suites pass (1559 passed). Both bug classes resolve.

Only nit, take it or leave it: the comment says the two forms "can't be intersected/unioned together", which is true for the mismatched pair but reads slightly like generic constraints can't be merged at all. Something like "a version constraint and a generic constraint have no common merge implementation, so leave them un-merged" might be marginally clearer. Not blocking.

@radoering

Copy link
Copy Markdown
Member

already resolved by #956

@radoering radoering closed this Aug 14, 2026
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.

3 participants