Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint intersection #297

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/poetry/core/packages/constraints/union_constraint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Tuple
from typing import Union
from typing import cast

from poetry.core.packages.constraints.base_constraint import BaseConstraint
Expand Down Expand Up @@ -78,19 +77,43 @@ def intersect(self, other: "BaseConstraint") -> "BaseConstraint":

return EmptyConstraint()

other = cast(Union[MultiConstraint, UnionConstraint], other)

# Two remaining cases: an intersection with another union, or an intersection
# with a multi.
#
# In the first case:
# (A or B) and (C or D) => (A and C) or (A and D) or (B and C) or (B and D)
#
# In the second case:
# (A or B) and (C and D) => (A and C and D) or (B and C and D)
new_constraints = []
for our_constraint in self._constraints:
for their_constraint in other.constraints:
intersection = our_constraint.intersect(their_constraint)
if isinstance(other, UnionConstraint):
for our_constraint in self._constraints:
for their_constraint in other.constraints:
intersection = our_constraint.intersect(their_constraint)

if (
not intersection.is_empty()
and intersection not in new_constraints
):
new_constraints.append(intersection)

else:
other = cast(MultiConstraint, other)

for our_constraint in self._constraints:
intersection = our_constraint
for their_constraint in other.constraints:
intersection = intersection.intersect(their_constraint)

if not intersection.is_empty() and intersection not in new_constraints:
new_constraints.append(intersection)

if not new_constraints:
return EmptyConstraint()

if len(new_constraints) == 1:
return new_constraints[0]

return UnionConstraint(*new_constraints)

def union(self, other: BaseConstraint) -> "UnionConstraint":
Expand Down
10 changes: 10 additions & 0 deletions tests/packages/constraints/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ def test_allows_all():
Constraint("linux", "!="),
MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")),
),
(
UnionConstraint(Constraint("win32"), Constraint("linux")),
UnionConstraint(Constraint("win32"), Constraint("darwin")),
Constraint("win32"),
),
(
UnionConstraint(Constraint("win32"), Constraint("linux")),
MultiConstraint(Constraint("win32", "!="), Constraint("darwin", "!=")),
Constraint("linux"),
),
],
)
def test_intersect(
Expand Down