Skip to content

Commit

Permalink
use python3.7+ typing in model_builder and cp_model
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jul 21, 2023
1 parent b980449 commit 9a5c7e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions ortools/linear_solver/python/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import math
import numbers
import typing
from typing import Callable, Mapping, Optional, Sequence, Union
from typing import Callable, Mapping, Optional, Sequence, Union, cast

import numpy as np
from numpy import typing as npt
Expand Down Expand Up @@ -1017,8 +1017,8 @@ def new_var_series(
if (
isinstance(is_integral, bool)
and is_integral
and isinstance(lower_bounds, NumberT)
and isinstance(upper_bounds, NumberT)
and mbn.is_a_number(lower_bounds)
and mbn.is_a_number(upper_bounds)
and math.isfinite(lower_bounds)
and math.isfinite(upper_bounds)
and math.ceil(lower_bounds) > math.floor(upper_bounds)
Expand Down Expand Up @@ -1583,8 +1583,8 @@ def _as_flat_linear_expression(base_expr: LinearExprT) -> _LinearExpression:
to_process.append((expr._right, coeff))
elif isinstance(expr, Variable):
terms[expr] += coeff
elif isinstance(expr, NumberT):
offset += coeff * expr
elif mbn.is_a_number(expr):
offset += coeff * cast(NumberT, expr)
elif isinstance(expr, _Product):
to_process.append((expr._expression, coeff * expr._coefficient))
elif isinstance(expr, _LinearExpression):
Expand Down Expand Up @@ -1680,7 +1680,7 @@ def _convert_to_series_and_validate_index(
TypeError: If the type of `value_or_series` is not recognized.
ValueError: If the index does not match.
"""
if isinstance(value_or_series, (bool, NumberT)):
if mbn.is_a_number(value_or_series) or isinstance(value_or_series, bool):
result = pd.Series(data=value_or_series, index=index)
elif isinstance(value_or_series, pd.Series):
if value_or_series.index.equals(index):
Expand Down
6 changes: 3 additions & 3 deletions ortools/sat/python/cp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,8 @@ def NewIntVarSeries(
if not name.isidentifier():
raise ValueError("name={} is not a valid identifier".format(name))
if (
isinstance(lower_bounds, IntegralT)
and isinstance(upper_bounds, IntegralT)
cmh.is_integral(lower_bounds)
and cmh.is_integral(upper_bounds)
and lower_bounds > upper_bounds
):
raise ValueError(
Expand Down Expand Up @@ -2984,7 +2984,7 @@ def _ConvertToSeriesAndValidateIndex(
TypeError: If the type of `value_or_series` is not recognized.
ValueError: If the index does not match.
"""
if isinstance(value_or_series, (bool, IntegralT)):
if cmh.is_integral(value_or_series) or isinstance(value_or_series, bool):
result = pd.Series(data=value_or_series, index=index)
elif isinstance(value_or_series, pd.Series):
if value_or_series.index.equals(index):
Expand Down

0 comments on commit 9a5c7e8

Please sign in to comment.