3939 import xarray .core .rolling
4040 from xarray .core .rolling import DatasetRolling # type: ignore
4141
42+ from types import EllipsisType , NotImplementedType
43+
4244from linopy import constraints , variables
4345from linopy .common import (
4446 EmptyDeprecationWrapper ,
7779from linopy .types import (
7880 ConstantLike ,
7981 DimsLike ,
80- EllipsisType ,
8182 ExpressionLike ,
82- NotImplementedType ,
8383 SideLike ,
8484 SignLike ,
8585 VariableLike ,
@@ -129,7 +129,7 @@ def _exprwrap(
129129def _expr_unwrap (
130130 maybe_expr : Any | LinearExpression | QuadraticExpression ,
131131) -> Any :
132- if isinstance (maybe_expr , ( LinearExpression , QuadraticExpression ) ):
132+ if isinstance (maybe_expr , LinearExpression | QuadraticExpression ):
133133 return maybe_expr .data
134134
135135 return maybe_expr
@@ -249,6 +249,8 @@ def sum(self, use_fallback: bool = False, **kwargs: Any) -> LinearExpression:
249249 orig_group = group
250250 group = group .apply (tuple , axis = 1 ).map (int_map )
251251
252+ # At this point, group is always a pandas Series
253+ assert isinstance (group , pd .Series )
252254 group_dim = group .index .name
253255
254256 arrays = [group , group .groupby (group ).cumcount ()]
@@ -529,13 +531,11 @@ def __div__(self: GenericExpression, other: SideLike) -> GenericExpression:
529531 try :
530532 if isinstance (
531533 other ,
532- (
533- variables .Variable ,
534- variables .ScalarVariable ,
535- LinearExpression ,
536- ScalarLinearExpression ,
537- QuadraticExpression ,
538- ),
534+ variables .Variable
535+ | variables .ScalarVariable
536+ | LinearExpression
537+ | ScalarLinearExpression
538+ | QuadraticExpression ,
539539 ):
540540 raise TypeError (
541541 "unsupported operand type(s) for /: "
@@ -930,7 +930,7 @@ def where(
930930 _other = FILL_VALUE
931931 else :
932932 _other = None
933- elif isinstance (other , ( int , float , DataArray ) ):
933+ elif isinstance (other , int | float | DataArray ):
934934 _other = {** self ._fill_value , "const" : other }
935935 else :
936936 _other = _expr_unwrap (other )
@@ -970,7 +970,7 @@ def fillna(
970970 A new object with missing values filled with the given value.
971971 """
972972 value = _expr_unwrap (value )
973- if isinstance (value , ( DataArray , np .floating , np .integer , int , float ) ):
973+ if isinstance (value , DataArray | np .floating | np .integer | int | float ):
974974 value = {"const" : value }
975975 return self .__class__ (self .data .fillna (value ), self .model )
976976
@@ -1362,10 +1362,10 @@ def __mul__(
13621362 return other .__rmul__ (self )
13631363
13641364 try :
1365- if isinstance (other , ( variables .Variable , variables .ScalarVariable ) ):
1365+ if isinstance (other , variables .Variable | variables .ScalarVariable ):
13661366 other = other .to_linexpr ()
13671367
1368- if isinstance (other , ( LinearExpression , ScalarLinearExpression ) ):
1368+ if isinstance (other , LinearExpression | ScalarLinearExpression ):
13691369 return self ._multiply_by_linear_expression (other )
13701370 else :
13711371 return self ._multiply_by_constant (other )
@@ -1403,7 +1403,7 @@ def __matmul__(
14031403 """
14041404 Matrix multiplication with other, similar to xarray dot.
14051405 """
1406- if not isinstance (other , ( LinearExpression , variables .Variable ) ):
1406+ if not isinstance (other , LinearExpression | variables .Variable ):
14071407 other = as_dataarray (other , coords = self .coords , dims = self .coord_dims )
14081408
14091409 common_dims = list (set (self .coord_dims ).intersection (other .dims ))
@@ -1620,7 +1620,7 @@ def process_one(
16201620 # assume first element is coefficient and second is variable
16211621 c , v = t
16221622 if isinstance (v , variables .ScalarVariable ):
1623- if not isinstance (c , ( int , float ) ):
1623+ if not isinstance (c , int | float ):
16241624 raise TypeError (
16251625 "Expected int or float as coefficient of scalar variable (first element of tuple)."
16261626 )
@@ -1703,12 +1703,10 @@ def __mul__(self, other: SideLike) -> QuadraticExpression:
17031703 """
17041704 if isinstance (
17051705 other ,
1706- (
1707- BaseExpression ,
1708- ScalarLinearExpression ,
1709- variables .Variable ,
1710- variables .ScalarVariable ,
1711- ),
1706+ BaseExpression
1707+ | ScalarLinearExpression
1708+ | variables .Variable
1709+ | variables .ScalarVariable ,
17121710 ):
17131711 raise TypeError (
17141712 "unsupported operand type(s) for *: "
@@ -1787,12 +1785,10 @@ def __matmul__(
17871785 """
17881786 if isinstance (
17891787 other ,
1790- (
1791- BaseExpression ,
1792- ScalarLinearExpression ,
1793- variables .Variable ,
1794- variables .ScalarVariable ,
1795- ),
1788+ BaseExpression
1789+ | ScalarLinearExpression
1790+ | variables .Variable
1791+ | variables .ScalarVariable ,
17961792 ):
17971793 raise TypeError (
17981794 "Higher order non-linear expressions are not yet supported."
@@ -1915,9 +1911,9 @@ def as_expression(
19151911 ValueError
19161912 If object cannot be converted to LinearExpression.
19171913 """
1918- if isinstance (obj , ( LinearExpression , QuadraticExpression ) ):
1914+ if isinstance (obj , LinearExpression | QuadraticExpression ):
19191915 return obj
1920- elif isinstance (obj , ( variables .Variable , variables .ScalarVariable ) ):
1916+ elif isinstance (obj , variables .Variable | variables .ScalarVariable ):
19211917 return obj .to_linexpr ()
19221918 else :
19231919 try :
@@ -2134,7 +2130,7 @@ def __neg__(self) -> ScalarLinearExpression:
21342130 )
21352131
21362132 def __mul__ (self , other : float | int ) -> ScalarLinearExpression :
2137- if not isinstance (other , ( int , float , np .number ) ):
2133+ if not isinstance (other , int | float | np .number ):
21382134 raise TypeError (
21392135 f"unsupported operand type(s) for *: { type (self )} and { type (other )} "
21402136 )
@@ -2147,7 +2143,7 @@ def __rmul__(self, other: int) -> ScalarLinearExpression:
21472143 return self .__mul__ (other )
21482144
21492145 def __div__ (self , other : float | int ) -> ScalarLinearExpression :
2150- if not isinstance (other , ( int , float , np .number ) ):
2146+ if not isinstance (other , int | float | np .number ):
21512147 raise TypeError (
21522148 f"unsupported operand type(s) for /: { type (self )} and { type (other )} "
21532149 )
@@ -2157,23 +2153,23 @@ def __truediv__(self, other: float | int) -> ScalarLinearExpression:
21572153 return self .__div__ (other )
21582154
21592155 def __le__ (self , other : int | float ) -> AnonymousScalarConstraint :
2160- if not isinstance (other , ( int , float , np .number ) ):
2156+ if not isinstance (other , int | float | np .number ):
21612157 raise TypeError (
21622158 f"unsupported operand type(s) for <=: { type (self )} and { type (other )} "
21632159 )
21642160
21652161 return constraints .AnonymousScalarConstraint (self , LESS_EQUAL , other )
21662162
21672163 def __ge__ (self , other : int | float ) -> AnonymousScalarConstraint :
2168- if not isinstance (other , ( int , float , np .number ) ):
2164+ if not isinstance (other , int | float | np .number ):
21692165 raise TypeError (
21702166 f"unsupported operand type(s) for >=: { type (self )} and { type (other )} "
21712167 )
21722168
21732169 return constraints .AnonymousScalarConstraint (self , GREATER_EQUAL , other )
21742170
21752171 def __eq__ (self , other : int | float ) -> AnonymousScalarConstraint : # type: ignore
2176- if not isinstance (other , ( int , float , np .number ) ):
2172+ if not isinstance (other , int | float | np .number ):
21772173 raise TypeError (
21782174 f"unsupported operand type(s) for ==: { type (self )} and { type (other )} "
21792175 )
0 commit comments