Skip to content

Commit

Permalink
Change isinstance(x, Union[type, type]) to `isinstance(x, (type, ty…
Browse files Browse the repository at this point in the history
…pe))` for backwards compatibility.
  • Loading branch information
mathgeekcoder committed Sep 23, 2024
1 parent 170a877 commit ce1bc5d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/highspy/highs.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,17 +632,17 @@ def addVariables(
# parameter can be scalar, array, or mapping lookup (i.e., dictionary, custom class, etc.)
# scalar: repeat for all N, array: use as is, lookup: convert to array using indices
def ensure_real(x: Union[Any, Mapping[Any, Any], Sequence[Any]]):
if isinstance(x, Union[float, int]):
if isinstance(x, (float, int)):
return np.full(N, x, dtype=np.float64)

elif isinstance(x, Mapping):
mt: Mapping[Any, Any] = x

if all(isinstance(v, Union[float, int]) for v in mt.values()):
if all(isinstance(v, (float, int)) for v in mt.values()):
m: Mapping[Any, Union[float, int]] = x
return np.fromiter((m[i] for i in indices), np.float64)

elif isinstance(x, Sequence) and len(x) == N and all(isinstance(v, Union[float, int]) for v in x):
elif isinstance(x, Sequence) and len(x) == N and all(isinstance(v, (float, int)) for v in x):
return np.asarray(x, dtype=np.float64)

raise Exception("Invalid parameter.")
Expand Down Expand Up @@ -1825,7 +1825,7 @@ def __eq__(self, other: Any) -> highs_linear_expression: # type: ignore
raise Exception("Bounds have already been set.")

# self == c
elif isinstance(other, Union[float, int]):
elif isinstance(other, (float, int)):
copy = highs_linear_expression(self)
copy.bounds = (
float(other) - (self.constant or 0.0),
Expand Down Expand Up @@ -1876,7 +1876,7 @@ def __eq__(self, other: Any) -> highs_linear_expression: # type: ignore

# support expr == [lb, ub] --> lb <= expr <= ub
elif hasattr(other, "__getitem__") and hasattr(other, "__len__") and len(other) == 2:
if not (isinstance(other[0], Union[float, int]) and isinstance(other[1], Union[float, int])):
if not (isinstance(other[0], (float, int)) and isinstance(other[1], (float, int))):
raise Exception("Provided bounds were not valid numbers.")

copy = highs_linear_expression(self)
Expand Down Expand Up @@ -1912,7 +1912,7 @@ def __le__(self, other: Union[float, int, highs_var, highs_linear_expression]):
self.__reset_chain(self, other, None)

# self <= c
if isinstance(other, Union[float, int]):
if isinstance(other, (float, int)):
copy = highs_linear_expression(self)
copy.bounds = (-kHighsInf, float(other) - (copy.constant or 0.0))
copy.constant = None
Expand Down Expand Up @@ -1980,7 +1980,7 @@ def __ge__(self, other: Union[float, int, highs_var, highs_linear_expression]):
self.__reset_chain(None, other, self)

# c <= self
if isinstance(other, Union[float, int]):
if isinstance(other, (float, int)):
copy = highs_linear_expression(self)
copy.bounds = (float(other) - (self.constant or 0.0), kHighsInf)
copy.constant = None
Expand Down Expand Up @@ -2176,7 +2176,7 @@ def __isub__(self, other: Union[float, int, highs_var, highs_linear_expression])
return self

def __imul__(self, other: Union[float, int, highs_var, highs_linear_expression]):
if isinstance(other, Union[float, int]):
if isinstance(other, (float, int)):
scale = float(other)

# other is a constant expression, so treat as a scalar
Expand Down

0 comments on commit ce1bc5d

Please sign in to comment.