Skip to content

Commit

Permalink
fix: unwrap operators of cell operations (#932)
Browse files Browse the repository at this point in the history
### Summary of Changes

Always unwrap the right operators of cell operations. This was missing
in some cases, leading to runtime errors akin to

```cannot create expression literal for value of type _LazyCell: <Expr ['[(col("Shortness of breath on …'] at 0x18E78C3E010>```

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
lars-reimann and megalinter-bot authored Sep 26, 2024
1 parent fdd971a commit 533a966
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/safeds/data/tabular/containers/_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,36 @@ def eq(self, other: Any) -> Cell[bool]:
"""
return self.__eq__(other)

def neq(self, other: Any) -> Cell[bool]:
"""
Check if not equal to a value. This is equivalent to the `!=` operator.
Examples
--------
>>> from safeds.data.tabular.containers import Column
>>> column = Column("example", [1, 2])
>>> column.transform(lambda cell: cell.neq(2))
+---------+
| example |
| --- |
| bool |
+=========+
| true |
| false |
+---------+
>>> column.transform(lambda cell: cell != 2)
+---------+
| example |
| --- |
| bool |
+=========+
| true |
| false |
+---------+
"""
return self.__ne__(other)

def ge(self, other: Any) -> Cell[bool]:
"""
Check if greater than or equal to a value. This is equivalent to the `>=` operator.
Expand Down
6 changes: 6 additions & 0 deletions src/safeds/data/tabular/containers/_lazy_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,27 @@ def __invert__(self) -> Cell[bool]:
return _wrap(self._expression.cast(pl.Boolean).__invert__())

def __and__(self, other: bool | Cell[bool]) -> Cell[bool]:
other = _unwrap(other)
return _wrap(self._expression.__and__(other))

def __rand__(self, other: bool | Cell[bool]) -> Cell[bool]:
other = _unwrap(other)
return _wrap(self._expression.__rand__(other))

def __or__(self, other: bool | Cell[bool]) -> Cell[bool]:
other = _unwrap(other)
return _wrap(self._expression.__or__(other))

def __ror__(self, other: bool | Cell[bool]) -> Cell[bool]:
other = _unwrap(other)
return _wrap(self._expression.__ror__(other))

def __xor__(self, other: bool | Cell[bool]) -> Cell[bool]:
other = _unwrap(other)
return _wrap(self._expression.__xor__(other))

def __rxor__(self, other: bool | Cell[bool]) -> Cell[bool]:
other = _unwrap(other)
return _wrap(self._expression.__rxor__(other))

# Comparison ---------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_add.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeAddition:
def test_dunder_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell + value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell + _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: value1 + cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) + cell, expected)

def test_named_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.add(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.add(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_and.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any

import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand Down Expand Up @@ -32,8 +34,17 @@ class TestShouldComputeConjunction:
def test_dunder_method(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell & value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell & _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 & cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) & cell, expected)

def test_named_method(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.and_(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.and_(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_div.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeDivision:
def test_dunder_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell / value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell / _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: value1 / cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) / cell, expected)

def test_named_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.div(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.div(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_eq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeEquality:
def test_dunder_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell == value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell == _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 == cell, expected) # type: ignore[arg-type,return-value]

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) == cell, expected) # type: ignore[arg-type,return-value]

def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.eq(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.eq(_LazyCell(pl.lit(value2))), expected)
8 changes: 8 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_floordiv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,5 +24,11 @@ class TestShouldComputeDivision:
def test_dunder_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell // value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell // _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: value1 // cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) // cell, expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_ge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeGreaterThanOrEqual:
def test_dunder_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell >= value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell >= _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 >= cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) >= cell, expected)

def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.ge(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.ge(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_gt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeGreaterThan:
def test_dunder_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell > value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell > _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 > cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) > cell, expected)

def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.gt(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.gt(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_le.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeLessThanOrEqual:
def test_dunder_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell <= value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell <= _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 <= cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) <= cell, expected)

def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.le(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.le(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_lt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeLessThan:
def test_dunder_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell < value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell < _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 < cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) < cell, expected)

def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.lt(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.lt(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_mod.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeModulus:
def test_dunder_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell % value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell % _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: value1 % cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) % cell, expected)

def test_named_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.mod(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.mod(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_mul.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,8 +24,17 @@ class TestShouldComputeMultiplication:
def test_dunder_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell * value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell * _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: value1 * cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) * cell, expected)

def test_named_method(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.mul(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
assert_cell_operation_works(value1, lambda cell: cell.mul(_LazyCell(pl.lit(value2))), expected)
14 changes: 14 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_ne.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand All @@ -22,5 +24,17 @@ class TestShouldComputeNegatedEquality:
def test_dunder_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell != value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell != _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: value2 != cell, expected) # type: ignore[arg-type,return-value]

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: _LazyCell(pl.lit(value2)) != cell, expected) # type: ignore[arg-type,return-value]

def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.neq(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.neq(_LazyCell(pl.lit(value2))), expected)
11 changes: 11 additions & 0 deletions tests/safeds/data/tabular/containers/_cell/test_or.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any

import polars as pl
import pytest
from safeds.data.tabular.containers._lazy_cell import _LazyCell

from tests.helpers import assert_cell_operation_works

Expand Down Expand Up @@ -32,8 +34,17 @@ class TestShouldComputeDisjunction:
def test_dunder_method(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell | value2, expected)

def test_dunder_method_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell | _LazyCell(pl.lit(value2)), expected)

def test_dunder_method_inverted_order(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: value1 | cell, expected)

def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) | cell, expected)

def test_named_method(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.or_(value2), expected)

def test_named_method_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
assert_cell_operation_works(value1, lambda cell: cell.or_(_LazyCell(pl.lit(value2))), expected)
Loading

0 comments on commit 533a966

Please sign in to comment.