Skip to content

Commit 646882b

Browse files
FBumannclaude
andcommitted
fix: mypy errors on Literal method/sign args in test_piecewise_constraints
CI's type checker flagged six argument-type errors after we tightened add_piecewise_formulation's signature to Literal-typed method and sign. The failing sites are all inside existing tests that loop over method strings (or take method/sign as a pytest.parametrize arg typed as str). Fix by introducing local Sign / Method TypeAliases that mirror the piecewise.py signature, then annotating the loop-variable lists and the one parametrized fixture accordingly. No behaviour change; the parametrize("method", ["sos2", "incremental"]) contract was always a subset of the allowed Literal. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f5bbf95 commit 646882b

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

test/test_piecewise_constraints.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
from pathlib import Path
7+
from typing import Literal, TypeAlias
78

89
import numpy as np
910
import pandas as pd
@@ -40,6 +41,9 @@
4041
)
4142
from linopy.solver_capabilities import SolverFeature, get_available_solvers_with_feature
4243

44+
Sign: TypeAlias = Literal["==", "<=", ">="]
45+
Method: TypeAlias = Literal["sos2", "incremental", "lp", "auto"]
46+
4347
_sos2_solvers = get_available_solvers_with_feature(
4448
SolverFeature.SOS_CONSTRAINTS, available_solvers
4549
)
@@ -1633,7 +1637,8 @@ def test_lp_consistency_with_sos2(self) -> None:
16331637
y_pts = [0, 20, 30, 35] # concave
16341638

16351639
solutions = {}
1636-
for method in ["lp", "sos2", "incremental"]:
1640+
methods: list[Method] = ["lp", "sos2", "incremental"]
1641+
for method in methods:
16371642
m = Model()
16381643
power = m.add_variables(lower=0, upper=30, name="power")
16391644
fuel = m.add_variables(lower=0, upper=40, name="fuel")
@@ -1686,7 +1691,8 @@ def test_lp_per_entity_nan_padding(self) -> None:
16861691
bp_y = pd.DataFrame([[0, 20, 30, 35], [0, 10, 15, np.nan]], index=["a", "b"])
16871692
bp_x = pd.DataFrame([[0, 10, 20, 30], [0, 5, 15, np.nan]], index=["a", "b"])
16881693
results: dict[str, float] = {}
1689-
for method in ["lp", "sos2"]:
1694+
methods: list[Method] = ["lp", "sos2"]
1695+
for method in methods:
16901696
m = Model()
16911697
coord = pd.Index(["a", "b"], name="entity")
16921698
x = m.add_variables(lower=0, upper=20, coords=[coord], name="x")
@@ -1723,7 +1729,7 @@ def test_lp_rejects_decreasing_x_concave_ge(self) -> None:
17231729

17241730
@pytest.mark.skipif(not _sos2_solvers, reason="no SOS2-capable solver available")
17251731
@pytest.mark.parametrize("method", ["sos2", "incremental"])
1726-
def test_active_off_with_sign_le_leaves_lower_open(self, method: str) -> None:
1732+
def test_active_off_with_sign_le_leaves_lower_open(self, method: Method) -> None:
17271733
"""
17281734
Documents the asymmetry between sign='==' and sign='<=' under
17291735
active=0: equality forces y=0, but '<=' only bounds y ≤ 0 — the
@@ -1816,7 +1822,8 @@ def test_lp_accepts_linear_curve(self) -> None:
18161822
A linear curve is both convex and concave per detection, so
18171823
LP must accept it with either sign and build the formulation.
18181824
"""
1819-
for sign in ["<=", ">="]:
1825+
signs: list[Sign] = ["<=", ">="]
1826+
for sign in signs:
18201827
m = Model()
18211828
x = m.add_variables(lower=0, upper=30, name="x")
18221829
y = m.add_variables(lower=0, upper=60, name="y")
@@ -1877,7 +1884,8 @@ def test_lp_matches_sos2_on_multi_dim_variables(self) -> None:
18771884
bp_x = pd.DataFrame([[0, 10, 20, 30], [0, 10, 20, 30]], index=["a", "b"])
18781885
bp_y = pd.DataFrame([[0, 20, 30, 35], [0, 15, 25, 30]], index=["a", "b"])
18791886
ys: dict[str, xr.DataArray] = {}
1880-
for method in ["lp", "sos2"]:
1887+
methods: list[Method] = ["lp", "sos2"]
1888+
for method in methods:
18811889
m = Model()
18821890
x = m.add_variables(lower=0, upper=30, coords=[entities], name="x")
18831891
y = m.add_variables(lower=0, upper=40, coords=[entities], name="y")
@@ -1905,9 +1913,10 @@ def test_lp_consistency_with_sos2_both_directions(self) -> None:
19051913
"""
19061914
x_pts = [0, 10, 20, 30]
19071915
y_pts = [0, 20, 30, 35] # concave
1916+
methods: list[Method] = ["lp", "sos2"]
19081917
for obj_sign in [-1.0, +1.0]:
19091918
sols: dict[str, float] = {}
1910-
for method in ["lp", "sos2"]:
1919+
for method in methods:
19111920
m = Model()
19121921
p = m.add_variables(lower=0, upper=30, name="p")
19131922
f = m.add_variables(lower=0, upper=50, name="f")

0 commit comments

Comments
 (0)