Skip to content

remove future warned scalar get item #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Release Notes
Upcoming Version
----------------

**Breaking Changes**

* The selection of a single item in `__getitem__` now returns a `Variable` instead of a `ScalarVariable`.

Version 0.5.1
--------------

Expand Down
19 changes: 3 additions & 16 deletions linopy/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,9 @@ def __init__(
def __getitem__(
self, selector: list[int] | int | slice | tuple[int64, str_]
) -> Variable | ScalarVariable:
keys = selector if isinstance(selector, tuple) else (selector,)
if all(map(pd.api.types.is_scalar, keys)):
warn(
"Accessing a single value with `Variable[...]` and return type "
"ScalarVariable is deprecated. In future, this will return a Variable."
"To get a ScalarVariable use `Variable.at[...]` instead.",
FutureWarning,
)
return self.at[keys]

else:
# return selected Variable
data = Dataset(
{k: self.data[k][selector] for k in self.data}, attrs=self.attrs
)
return self.__class__(data, self.model, self.name)
# return selected Variable
data = Dataset({k: self.data[k][selector] for k in self.data}, attrs=self.attrs)
return self.__class__(data, self.model, self.name)

@property
def attrs(self) -> dict[str, Hashable]:
Expand Down
7 changes: 3 additions & 4 deletions test/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ def test_wrong_variable_init(m: Model, x: linopy.Variable) -> None:


def test_variable_getter(x: linopy.Variable, z: linopy.Variable) -> None:
with pytest.warns(FutureWarning):
assert isinstance(x[0], linopy.variables.ScalarVariable)
assert isinstance(z[0], linopy.variables.ScalarVariable)
assert isinstance(x[0], linopy.variables.Variable)
assert isinstance(z[0], linopy.variables.Variable)

assert isinstance(x.at[0], linopy.variables.ScalarVariable)
assert isinstance(x.at[0], linopy.variables.Variable)


def test_variable_getter_slice(x: linopy.Variable) -> None:
Expand Down
Loading