From 28a39907080267f2b3357a0d9a91c9f800742fcb Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Tue, 7 Nov 2023 16:47:54 -0500 Subject: [PATCH] test: remove breakpoints and update coverage on expressions --- ilpy/expressions.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/ilpy/expressions.py b/ilpy/expressions.py index 4fdcb77..f62aa15 100644 --- a/ilpy/expressions.py +++ b/ilpy/expressions.py @@ -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}") @@ -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 @@ -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) @@ -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) @@ -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]) @@ -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) @@ -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 @@ -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 @@ -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)}"