Skip to content

Commit

Permalink
more tests, fixes and simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Jul 20, 2024
1 parent 76227be commit f42758e
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 152 deletions.
168 changes: 40 additions & 128 deletions src/poetry/core/constraints/generic/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,8 @@ def allows(self, other: BaseConstraint) -> bool:
f' ("other" must be a constraint with operator "=="): {other}'
)

if self._operator == "==":
return self._value == other.value

if self._operator == "in":
return bool(self._trans_op_str["in"](other.value, self._value))

if self._operator == "!=":
return self._value != other.value

if self._operator == "not in":
return bool(self._trans_op_str["not in"](other.value, self._value))
if op := self._trans_op_str.get(self._operator):
return op(other.value, self._value)

return False

Expand All @@ -91,21 +82,17 @@ def allows_all(self, other: BaseConstraint) -> bool:
from poetry.core.constraints.generic import UnionConstraint

if isinstance(other, Constraint):
is_in_op = self._operator == "in"
is_not_in_op = self._operator == "not in"

is_other_equal_op = other.operator == "=="
is_other_in_op = other.operator == "in"
is_other_not_in_op = other.operator == "not in"

if is_other_equal_op:
if other.operator == "==":
return self.allows(other)

if is_other_in_op and is_in_op:
return self._op(self.value, other.value)
if other.operator == "in" and self._operator == "in":
return self.value in other.value

if is_other_not_in_op and not is_not_in_op:
return self._trans_op_str["not in"](other.value, self.value)
if other.operator == "not in":
if self._operator == "not in":
return other.value in self.value
if self._operator == "!=":
return self.value not in other.value

return self == other

Expand All @@ -121,40 +108,29 @@ def allows_any(self, other: BaseConstraint) -> bool:
from poetry.core.constraints.generic import MultiConstraint
from poetry.core.constraints.generic import UnionConstraint

is_equal_op = self._operator == "=="
is_non_equal_op = self._operator == "!="
is_in_op = self._operator == "in"
is_not_in_op = self._operator == "not in"

if is_equal_op:
if self._operator == "==":
return other.allows(self)

if isinstance(other, Constraint):
is_other_equal_op = other.operator == "=="
is_other_non_equal_op = other.operator == "!="
is_other_in_op = other.operator == "in"
is_other_not_in_op = other.operator == "not in"

if is_other_equal_op:
if other.operator == "==":
return self.allows(other)

if is_equal_op and is_other_non_equal_op:
if other.operator == "!=" and self._operator == "==":
return self._value != other.value

return (
is_in_op
and is_other_in_op
or is_not_in_op
and is_other_not_in_op
or is_non_equal_op
and other.operator in {"!=", "in", "not in"}
)
if other.operator == "not in" and self._operator == "in":
return other.value not in self.value

if other.operator == "in" and self._operator == "not in":
return self.value not in other.value

return True

elif isinstance(other, MultiConstraint):
return is_non_equal_op
return self._operator == "!="

elif isinstance(other, UnionConstraint):
return is_non_equal_op and any(
return self._operator == "!=" and any(
self.allows_any(c) for c in other.constraints
)

Expand All @@ -176,52 +152,16 @@ def intersect(self, other: BaseConstraint) -> BaseConstraint:
if other == self:
return self

if self.operator == "!=" and other.operator == "==" and self.allows(other):
if self.allows_all(other):
return other

if other.operator == "!=" and self.operator == "==" and other.allows(self):
if other.allows_all(self):
return self

if (
other.operator == "!="
and self.operator == "!="
or self.operator == "not in"
and other.operator == "not in"
):
return MultiConstraint(self, other)

other_in_self = self._trans_op_str["in"](self.value, other.value)
self_in_other = self._trans_op_str["in"](other.value, self.value)
is_in_op = self._operator == "in"
is_other_in_op = other.operator == "in"

if is_in_op or other.operator == "not in":
# If self is a subset of other, return self
if is_other_in_op and other_in_self:
return self
# If neither are subsets of each other then its a MC
if (is_other_in_op and not self_in_other) or (
other.operator == "!=" and self_in_other
):
return MultiConstraint(self, other)
# if it allows any of other, return other
if self.allows_any(other):
return other

if is_other_in_op or self.operator == "not in":
# If other is a subset of self, return other
if is_in_op and self_in_other:
return other
# If neither are subsets of each other then its a MC
if (is_in_op and not other_in_self) or (
self.operator == "!=" and other_in_self
):
return MultiConstraint(self, other)
# if other allows any of self, return self
if other.allows_any(self):
return self
if not self.allows_any(other) or not other.allows_any(self):
return EmptyConstraint()

return EmptyConstraint()
return MultiConstraint(self, other)

return other.intersect(self)

Expand All @@ -232,53 +172,25 @@ def union(self, other: BaseConstraint) -> BaseConstraint:
if other == self:
return self

if self.operator == "!=" and other.operator == "==" and self.allows(other):
if self.allows_all(other):
return self

if other.operator == "!=" and self.operator == "==" and other.allows(self):
if other.allows_all(self):
return other

ops = {self.operator, other.operator}
if (
other.operator == "=="
and self.operator == "=="
or self.operator == "not in"
and other.operator == "not in"
(ops in ({"!="}, {"not in"}))
or (
ops in ({"in", "!="}, {"in", "not in"})
and (self.operator == "in" and self.value in other.value)
or (other.operator == "in" and other.value in self.value)
)
or self.invert() == other
):
return UnionConstraint(self, other)

other_in_self = self._trans_op_str["in"](self.value, other.value)
other_not_in_self = self._trans_op_str["not in"](self.value, other.value)
self_in_other = self._trans_op_str["in"](other.value, self.value)
self_not_in_other = self._trans_op_str["not in"](other.value, self.value)
is_in_op = self._operator == "in"
is_other_in_op = other.operator == "in"
not_in_each_other = other_not_in_self and self_not_in_other

if is_in_op:
if is_other_in_op and self_in_other:
return self
if (
(is_other_in_op and self_in_other)
or (is_other_in_op and not_in_each_other)
or (other.operator == "!=" and not_in_each_other)
):
return UnionConstraint(self, other)
if other.allows_all(self):
return other

if is_other_in_op:
if is_in_op and other_in_self:
return other
if (
(is_in_op and other_in_self)
or (is_in_op and not_in_each_other)
or (self.operator == "!=" and not_in_each_other)
):
return UnionConstraint(self, other)
if self.allows_all(other):
return self

return AnyConstraint()
return AnyConstraint()

return UnionConstraint(self, other)

# to preserve order (functionally not necessary)
if isinstance(other, UnionConstraint):
Expand Down
Loading

0 comments on commit f42758e

Please sign in to comment.