Skip to content

Commit

Permalink
simplify extra markers 2 (#845)
Browse files Browse the repository at this point in the history
Without this change the union of `extra == "a" and extra == "c"` and `extra == "a" or extra == "b"` is `extra == "a" and extra == "c" or extra == "a" or extra == "b"`. With the change, it is simplified to `extra == "a" or extra == "b"`.
  • Loading branch information
radoering authored Mar 5, 2025
1 parent 369f5a9 commit ab81345
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/poetry/core/constraints/generic/union_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ def union(self, other: BaseConstraint) -> BaseConstraint:

else:
assert isinstance(other, MultiConstraint)
# (A or B) or (C and D) => nothing to do
# (A or B) or (A and D) => A or B
if any(c in other.constraints for c in self._constraints):
return self

# (A or B) or (C and D) => nothing to do
new_constraints = [*self._constraints, other]

if len(new_constraints) == 1:
Expand Down
5 changes: 5 additions & 0 deletions tests/constraints/generic/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,11 @@ def test_union(
),
),
),
(
UnionConstraint(ExtraConstraint("extra1"), ExtraConstraint("extra2")),
ExtraMultiConstraint(ExtraConstraint("extra1"), ExtraConstraint("extra3")),
UnionConstraint(ExtraConstraint("extra1"), ExtraConstraint("extra2")),
),
],
)
def test_union_extra(
Expand Down
10 changes: 10 additions & 0 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,16 @@ def test_single_marker_union_with_inverse() -> None:
'extra == "a" or extra == "c"',
'extra != "a" and extra != "b" or extra == "a" or extra == "c"',
),
(
'extra == "a" or extra == "b"',
'extra == "a" and extra == "c"',
'extra == "a" or extra == "b"',
),
(
'extra == "a" and extra == "c"',
'extra == "a" or extra == "b"',
'extra == "a" or extra == "b"',
),
],
)
def test_single_marker_union_extras(marker1: str, marker2: str, expected: str) -> None:
Expand Down

0 comments on commit ab81345

Please sign in to comment.