Skip to content

Commit 6fdf678

Browse files
authored
fix: change empty to property (#425)
* breaking: make empty a property * update release notes * add deprecation wrapper * add missing types
1 parent 733f266 commit 6fdf678

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

doc/release_notes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Release Notes
33

44
Upcoming Version
55
----------------
6+
**Deprecations**
7+
8+
* Renamed `expression.empty()` to `expression.empty` to align with the use of empty in
9+
pandas. A custom wrapper ensures that `expression.empty()` continues to work, but emits
10+
a DeprecationWarning.
11+
12+
**Features**
613

714
* Added support for arithmetic operations with custom classes.
815
* Avoid allocating a floating license for COPT during the initial solver check

linopy/common.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,3 +1005,34 @@ def __getitem__(
10051005
labels = indexing.expanded_indexer(key, self.object.ndim)
10061006
key = dict(zip(self.object.dims, labels))
10071007
return self.object.sel(key)
1008+
1009+
1010+
class EmptyDeprecationWrapper:
1011+
"""
1012+
Temporary wrapper for a smooth transition from .empty() to .empty
1013+
1014+
Use `bool(expr.empty)` to explicitly unwrap.
1015+
1016+
See Also
1017+
--------
1018+
https://github.com/PyPSA/linopy/pull/425
1019+
"""
1020+
1021+
__slots__ = ("value",)
1022+
1023+
def __init__(self, value: bool):
1024+
self.value = value
1025+
1026+
def __bool__(self) -> bool:
1027+
return self.value
1028+
1029+
def __repr__(self) -> str:
1030+
return repr(self.value)
1031+
1032+
def __call__(self) -> bool:
1033+
warn(
1034+
"Calling `.empty()` is deprecated, use `.empty` property instead",
1035+
DeprecationWarning,
1036+
stacklevel=2,
1037+
)
1038+
return self.value

linopy/expressions.py

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

3838
from linopy import constraints, variables
3939
from linopy.common import (
40+
EmptyDeprecationWrapper,
4041
LocIndexer,
4142
as_dataarray,
4243
assign_multiindex_safe,
@@ -1347,11 +1348,16 @@ def size(self) -> int:
13471348
"""
13481349
return self.vars.size
13491350

1350-
def empty(self) -> bool:
1351+
@property
1352+
def empty(self) -> EmptyDeprecationWrapper:
13511353
"""
13521354
Get whether the linear expression is empty.
1355+
1356+
.. versionchanged:: 0.5.1
1357+
Returns a EmptyDeprecationWrapper which behaves like a bool and emits
1358+
a DeprecationWarning when called.
13531359
"""
1354-
return not self.size
1360+
return EmptyDeprecationWrapper(not self.size)
13551361

13561362
def densify_terms(self) -> LinearExpression:
13571363
"""

linopy/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import logging
1010
import os
1111
import re
12-
from collections.abc import Mapping, Sequence
12+
from collections.abc import Callable, Mapping, Sequence
1313
from pathlib import Path
1414
from tempfile import NamedTemporaryFile, gettempdir
15-
from typing import Any, Callable
15+
from typing import Any
1616

1717
import numpy as np
1818
import pandas as pd
@@ -705,7 +705,7 @@ def add_objective(
705705
The objective function assigned to the model.
706706
"""
707707
if not overwrite:
708-
assert self.objective.expression.empty(), (
708+
assert self.objective.expression.empty, (
709709
"Objective already defined."
710710
" Set `overwrite` to True to force overwriting."
711711
)

test/test_linear_expression.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,11 @@ def test_linear_expression_loc(x: Variable, y: Variable) -> None:
541541

542542
def test_linear_expression_empty(v: Variable) -> None:
543543
expr = 7 * v
544-
assert not expr.empty()
545-
assert expr.loc[[]].empty()
544+
assert not expr.empty
545+
assert expr.loc[[]].empty
546+
547+
with pytest.warns(DeprecationWarning, match="use `.empty` property instead"):
548+
assert expr.loc[[]].empty()
546549

547550

548551
def test_linear_expression_isnull(v: Variable) -> None:

0 commit comments

Comments
 (0)