Skip to content

Commit cf74caf

Browse files
committed
fix: resolve mypy errors in SOS constraints tests
- Add type ignore for intentional invalid sos_type test case - Add proper Path type annotation for tmp_path parameter - Add null checks before np.isclose calls for objective values - Replace list literals with np.array for arithmetic operations
1 parent 3f0df2b commit cf74caf

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

test/test_sos_constraints.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
4+
35
import numpy as np
46
import pandas as pd
57
import pytest
@@ -27,7 +29,7 @@ def test_add_sos_constraints_validation() -> None:
2729
m = Model()
2830
strings = pd.Index(["a", "b"], name="strings")
2931
with pytest.raises(ValueError, match="sos_type"):
30-
m.add_sos_constraints(m.add_variables(name="x"), sos_type=3, sos_dim="i")
32+
m.add_sos_constraints(m.add_variables(name="x"), sos_type=3, sos_dim="i") # type: ignore[arg-type]
3133

3234
variable = m.add_variables(coords=[strings], name="string_var")
3335

@@ -43,7 +45,7 @@ def test_add_sos_constraints_validation() -> None:
4345
m.add_sos_constraints(numeric, sos_type=1, sos_dim="dim")
4446

4547

46-
def test_sos_constraints_written_to_lp(tmp_path) -> None:
48+
def test_sos_constraints_written_to_lp(tmp_path: Path) -> None:
4749
m = Model()
4850
breakpoints = pd.Index([0.0, 1.5, 3.5], name="bp")
4951
lambdas = m.add_variables(coords=[breakpoints], name="lambda")
@@ -83,14 +85,15 @@ def test_sos1_binary_maximize_lp_polars() -> None:
8385
locations = pd.Index([0, 1, 2], name="locations")
8486
build = m.add_variables(coords=[locations], name="build", binary=True)
8587
m.add_sos_constraints(build, sos_type=1, sos_dim="locations")
86-
m.add_objective(build * [1, 2, 3], sense="max")
88+
m.add_objective(build * np.array([1, 2, 3]), sense="max")
8789

8890
try:
8991
m.solve(solver_name="gurobi", io_api="lp-polars")
9092
except gurobipy.GurobiError as exc: # pragma: no cover - depends on license setup
9193
pytest.skip(f"Gurobi environment unavailable: {exc}")
9294

9395
assert np.isclose(build.solution.values, [0, 0, 1]).all()
96+
assert m.objective.value is not None
9497
assert np.isclose(m.objective.value, 3)
9598

9699

@@ -102,14 +105,15 @@ def test_sos2_binary_maximize_direct() -> None:
102105
locations = pd.Index([0, 1, 2], name="locations")
103106
build = m.add_variables(coords=[locations], name="build", binary=True)
104107
m.add_sos_constraints(build, sos_type=2, sos_dim="locations")
105-
m.add_objective(build * [1, 2, 3], sense="max")
108+
m.add_objective(build * np.array([1, 2, 3]), sense="max")
106109

107110
try:
108111
m.solve(solver_name="gurobi", io_api="direct")
109112
except gurobipy.GurobiError as exc: # pragma: no cover - depends on license setup
110113
pytest.skip(f"Gurobi environment unavailable: {exc}")
111114

112115
assert np.isclose(build.solution.values, [0, 1, 1]).all()
116+
assert m.objective.value is not None
113117
assert np.isclose(m.objective.value, 5)
114118

115119

@@ -121,14 +125,15 @@ def test_sos2_binary_maximize_different_coeffs() -> None:
121125
locations = pd.Index([0, 1, 2], name="locations")
122126
build = m.add_variables(coords=[locations], name="build", binary=True)
123127
m.add_sos_constraints(build, sos_type=2, sos_dim="locations")
124-
m.add_objective(build * [2, 1, 3], sense="max")
128+
m.add_objective(build * np.array([2, 1, 3]), sense="max")
125129

126130
try:
127131
m.solve(solver_name="gurobi", io_api="direct")
128132
except gurobipy.GurobiError as exc: # pragma: no cover - depends on license setup
129133
pytest.skip(f"Gurobi environment unavailable: {exc}")
130134

131135
assert np.isclose(build.solution.values, [0, 1, 1]).all()
136+
assert m.objective.value is not None
132137
assert np.isclose(m.objective.value, 4)
133138

134139

@@ -137,7 +142,7 @@ def test_unsupported_solver_raises_error() -> None:
137142
locations = pd.Index([0, 1, 2], name="locations")
138143
build = m.add_variables(coords=[locations], name="build", binary=True)
139144
m.add_sos_constraints(build, sos_type=1, sos_dim="locations")
140-
m.add_objective(build * [1, 2, 3], sense="max")
145+
m.add_objective(build * np.array([1, 2, 3]), sense="max")
141146

142147
for solver in ["glpk", "mosek", "mindopt", "highs"]:
143148
if solver in available_solvers:

0 commit comments

Comments
 (0)