Skip to content

Commit

Permalink
test: remove breakpoints and update coverage on expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Nov 7, 2023
1 parent 4da32d0 commit 28a3990
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions ilpy/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def as_constraint(self) -> Constraint:

def as_objective(self, sense: Sense = Sense.Minimize) -> Objective:
"""Create a linear objective from this expression."""
if _get_relation(self) is not None:
if _get_relation(self) is not None: # pragma: no cover
# TODO: may be supported in the future, eg. for piecewise objectives?
raise ValueError(f"Objective function cannot have comparisons: {self}")

Expand Down Expand Up @@ -94,7 +94,7 @@ def __mul__(self, other: Any) -> BinOp | Constant:
return BinOp(self, ast.Mult(), other)

def __rmul__(self, other: Number) -> BinOp | Constant:
if not isinstance(other, (int, float)):
if not isinstance(other, (int, float)): # pragma: no cover
raise TypeError("Right multiplication must be with a number")
return Constant(other) * self

Expand Down Expand Up @@ -191,7 +191,7 @@ def __mul__(self, other: Any) -> BinOp | Constant:
return super().__mul__(other)

def __pow__(self, other: Number) -> Expression:
if not isinstance(other, (int, float)):
if not isinstance(other, (int, float)): # pragma: no cover
raise TypeError("Exponent must be a number")
return Constant(self.value**other)

Expand All @@ -210,20 +210,20 @@ def __init__(self, id: str, index: int | None = None) -> None:
super().__init__(str(id), ctx=ast.Load())

def __pow__(self, other: Number) -> Expression:
if not isinstance(other, (int, float)):
if not isinstance(other, (int, float)): # pragma: no cover
raise TypeError("Exponent must be a number")
if other == 2:
return BinOp(self, ast.Mult(), self)
elif other == 1:
return self
raise ValueError("Only quadratic variables are supported")
raise ValueError("Only quadratic variables are supported") # pragma: no cover

def __hash__(self) -> int:
# allow use as dict key
return id(self)

def __int__(self) -> int:
if self.index is None:
if self.index is None: # pragma: no cover
raise TypeError(f"Variable {self!r} has no index")
return int(self.index)

Expand All @@ -247,7 +247,7 @@ def _get_relation(expr: Expression) -> Relation | None:
relation: Relation | None = None
for sub in ast.walk(expr):
if isinstance(sub, Compare):
if seen_compare:
if seen_compare: # pragma: no cover
raise ValueError("Only single comparisons are supported")

op_type = type(sub.ops[0])
Expand Down Expand Up @@ -318,14 +318,10 @@ def _get_coefficients(
coeffs = {}

if isinstance(expr, Constant):
if var_scale is not None:
breakpoint()
coeffs.setdefault(None, 0)
coeffs[None] += expr.value * scale

elif isinstance(expr, UnaryOp):
if var_scale is not None:
breakpoint()
if isinstance(expr.op, ast.USub):
scale = -scale
_get_coefficients(expr.operand, coeffs, scale, var_scale)
Expand All @@ -341,7 +337,7 @@ def _get_coefficients(
coeffs[expr] += scale

elif isinstance(expr, Compare):
if len(expr.ops) != 1:
if len(expr.ops) != 1: # pragma: no cover
raise ValueError("Only single comparisons are supported")
_get_coefficients(expr.left, coeffs, scale, var_scale)
# negate the right hand side of the comparison
Expand All @@ -355,10 +351,10 @@ def _get_coefficients(
if isinstance(expr.op, (ast.USub, ast.Sub)):
scale = -scale
_get_coefficients(expr.right, coeffs, scale, var_scale)
else:
else: # pragma: no cover
raise ValueError(f"Unsupported binary operator: {type(expr.op)}")

else:
else: # pragma: no cover
raise ValueError(f"Unsupported expression type: {type(expr)}")

return coeffs
Expand Down Expand Up @@ -396,10 +392,10 @@ def _process_mult_op(
raise TypeError("Cannot multiply by more than two variables.")
_get_coefficients(expr.right, coeffs, scale, expr.left)
elif isinstance(expr.right, Variable):
if var_scale is not None:
if var_scale is not None: # pragma: no cover
raise TypeError("Cannot multiply by more than two variables.")
_get_coefficients(expr.left, coeffs, scale, expr.right)
else:
else: # pragma: no cover
raise TypeError(
"Unexpected multiplcation or division between "
f"{type(expr.left)} and {type(expr.right)}"
Expand Down

0 comments on commit 28a3990

Please sign in to comment.