Skip to content

Commit

Permalink
fix endless recursion in intersection and union of multiconstraints (#…
Browse files Browse the repository at this point in the history
…593)

(cherry picked from commit 73dd45a)
  • Loading branch information
dimbleby authored and radoering committed May 20, 2023
1 parent 0b4c8c7 commit bf6c63f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/poetry/core/constraints/generic/multi_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def invert(self) -> UnionConstraint:
return UnionConstraint(*(c.invert() for c in self._constraints))

def intersect(self, other: BaseConstraint) -> BaseConstraint:
if isinstance(other, MultiConstraint):
ours = set(self.constraints)
union = list(self.constraints) + [
c for c in other.constraints if c not in ours
]
return MultiConstraint(*union)

if not isinstance(other, Constraint):
return other.intersect(self)

Expand All @@ -90,6 +97,11 @@ def intersect(self, other: BaseConstraint) -> BaseConstraint:
return MultiConstraint(*self._constraints, other)

def union(self, other: BaseConstraint) -> BaseConstraint:
if isinstance(other, MultiConstraint):
theirs = set(other.constraints)
common = [c for c in self.constraints if c in theirs]
return MultiConstraint(*common)

if not isinstance(other, Constraint):
return other.union(self)

Expand Down
14 changes: 14 additions & 0 deletions tests/constraints/generic/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ def test_invert(constraint: BaseConstraint, inverted: BaseConstraint) -> None:
MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")),
EmptyConstraint(),
),
(
MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")),
MultiConstraint(Constraint("win32", "!="), Constraint("darwin", "!=")),
MultiConstraint(
Constraint("win32", "!="),
Constraint("linux", "!="),
Constraint("darwin", "!="),
),
),
],
)
def test_intersect(
Expand Down Expand Up @@ -393,6 +402,11 @@ def test_intersect(
MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")),
),
),
(
MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")),
MultiConstraint(Constraint("win32", "!="), Constraint("darwin", "!=")),
MultiConstraint(Constraint("win32", "!=")),
),
],
)
def test_union(
Expand Down

0 comments on commit bf6c63f

Please sign in to comment.