Skip to content

Commit 2d43d5a

Browse files
RobbieKiwiRobbie Muirpre-commit-ci[bot]
authored
Fixed typing of arithmetic methods (#454)
* Fixed typing of arithmetic methods * changes based on pr comments * Changes to typing * Further typing changes * fixed test * added tests * Went down a rabbit hole * fixed tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed coverage * fixed precommit issue * fixed test * formatting * Added tests to improve code coverage * minor changes * Deprecated using single-value tuple for LinearExpression.from_tuples * added tests to improve code coverage * improved code coverage a tiny bit more * fixed mypy warnign * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * minor changes --------- Co-authored-by: Robbie Muir <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ca3d90e commit 2d43d5a

15 files changed

+1073
-551
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ benchmark/notebooks/.ipynb_checkpoints
3636
benchmark/scripts/__pycache__
3737
benchmark/scripts/benchmarks-pypsa-eur/__pycache__
3838
benchmark/scripts/leftovers/
39+
40+
# IDE
41+
.idea/

doc/release_notes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Release Notes
44
.. Upcoming Version
55
.. ----------------
66
7+
8+
* Improved variable/expression arithmetic methods so that they correctly handle types
9+
710
Version 0.5.5
811
--------------
912

1013
* Internally assign new data fields to expressions with a multiindexed-safe routine.
1114

12-
1315
Version 0.5.4
1416
--------------
1517

linopy/common.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
if TYPE_CHECKING:
3939
from linopy.constraints import Constraint
40-
from linopy.expressions import LinearExpression
40+
from linopy.expressions import LinearExpression, QuadraticExpression
4141
from linopy.variables import Variable
4242

4343

@@ -994,13 +994,13 @@ def check_common_keys_values(list_of_dicts: list[dict[str, Any]]) -> bool:
994994

995995

996996
def align(
997-
*objects: LinearExpression | Variable | T_Alignable,
997+
*objects: LinearExpression | QuadraticExpression | Variable | T_Alignable,
998998
join: JoinOptions = "inner",
999999
copy: bool = True,
10001000
indexes: Any = None,
10011001
exclude: str | Iterable[Hashable] = frozenset(),
10021002
fill_value: Any = dtypes.NA,
1003-
) -> tuple[LinearExpression | Variable | T_Alignable, ...]:
1003+
) -> tuple[LinearExpression | QuadraticExpression | Variable | T_Alignable, ...]:
10041004
"""
10051005
Given any number of Variables, Expressions, Dataset and/or DataArray objects,
10061006
returns new objects with aligned indexes and dimension sizes.
@@ -1055,13 +1055,13 @@ def align(
10551055
10561056
10571057
"""
1058-
from linopy.expressions import LinearExpression
1058+
from linopy.expressions import LinearExpression, QuadraticExpression
10591059
from linopy.variables import Variable
10601060

10611061
finisher: list[partial[Any] | Callable[[Any], Any]] = []
10621062
das: list[Any] = []
10631063
for obj in objects:
1064-
if isinstance(obj, LinearExpression):
1064+
if isinstance(obj, (LinearExpression, QuadraticExpression)):
10651065
finisher.append(partial(obj.__class__, model=obj.model))
10661066
das.append(obj.data)
10671067
elif isinstance(obj, Variable):
@@ -1090,7 +1090,14 @@ def align(
10901090
return tuple([f(da) for f, da in zip(finisher, aligned)])
10911091

10921092

1093-
LocT = TypeVar("LocT", "Dataset", "Variable", "LinearExpression", "Constraint")
1093+
LocT = TypeVar(
1094+
"LocT",
1095+
"Dataset",
1096+
"Variable",
1097+
"LinearExpression",
1098+
"QuadraticExpression",
1099+
"Constraint",
1100+
)
10941101

10951102

10961103
class LocIndexer(Generic[LocT]):

0 commit comments

Comments
 (0)