Skip to content

Commit 265724b

Browse files
authored
Update add_constrs type hints and docs (#85)
- Update typing overloads to allow either side to be a single expression, but require at least one side to be a series - Add the corresponding comments in the doctring. Closes #77
1 parent b9a40ca commit 265724b

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

src/gurobipy_pandas/api.py

+40-6
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,63 @@ def add_vars(
193193
raise ValueError("`pandas_obj` must be an index, series, or dataframe")
194194

195195

196+
# Two overloads are used here to specify that at least one of the left- and
197+
# right-hand sides must be a series (the other can be a single expression).
198+
199+
200+
@overload
196201
def add_constrs(
197202
model: gp.Model,
198-
lhs: Union[pd.Series, float],
203+
lhs: pd.Series,
199204
sense: Union[pd.Series, str],
200-
rhs: Union[pd.Series, float],
205+
rhs: Union[pd.Series, gp.Var, gp.LinExpr, gp.QuadExpr, float],
201206
*,
202207
name: Optional[str] = None,
203208
index_formatter: Union[str, Callable, Mapping[str, Callable]] = "default",
204209
) -> pd.Series:
205-
"""Add a constraint to the model for each row in lhs & rhs.
210+
... # pragma: no cover
211+
212+
213+
@overload
214+
def add_constrs(
215+
model: gp.Model,
216+
lhs: Union[pd.Series, gp.Var, gp.LinExpr, gp.QuadExpr, float],
217+
sense: Union[pd.Series, str],
218+
rhs: pd.Series,
219+
*,
220+
name: Optional[str] = None,
221+
index_formatter: Union[str, Callable, Mapping[str, Callable]] = "default",
222+
) -> pd.Series:
223+
... # pragma: no cover
224+
225+
226+
def add_constrs(
227+
model,
228+
lhs,
229+
sense,
230+
rhs,
231+
*,
232+
name=None,
233+
index_formatter="default",
234+
) -> pd.Series:
235+
"""Add a constraint to the model for each row in lhs & rhs. At least one of
236+
`lhs` and `rhs` must be a Series, while the other side may be a constant or a
237+
single gurobipy expression. If both sides are Series, then their indexes
238+
must match.
206239
207240
Parameters
208241
----------
209242
model : Model
210243
A Gurobi model to which new constraints will be added
211244
lhs : Series
212-
A series of expressions forming the left hand side of constraints
245+
A series of expressions forming the left hand side of constraints, a
246+
constant, or a single expression.
213247
sense : Series or str
214248
Constraint sense; can be a series if senses vary, or a single string
215249
if all constraints have the same sense
216250
rhs : Series or float
217-
A series of expressions forming the right hand side of constraints,
218-
or a common constant
251+
A series of expressions forming the right hand side of constraints, a
252+
constant, or a single expression.
219253
name : str
220254
Used as the returned series name, as well as the base name for added
221255
Gurobi constraints. Constraint name suffixes come from the lhs/rhs

0 commit comments

Comments
 (0)