Skip to content

Commit 533a966

Browse files
fix: unwrap operators of cell operations (#932)
### 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]>
1 parent fdd971a commit 533a966

File tree

18 files changed

+212
-0
lines changed

18 files changed

+212
-0
lines changed

src/safeds/data/tabular/containers/_cell.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,36 @@ def eq(self, other: Any) -> Cell[bool]:
599599
"""
600600
return self.__eq__(other)
601601

602+
def neq(self, other: Any) -> Cell[bool]:
603+
"""
604+
Check if not equal to a value. This is equivalent to the `!=` operator.
605+
606+
Examples
607+
--------
608+
>>> from safeds.data.tabular.containers import Column
609+
>>> column = Column("example", [1, 2])
610+
>>> column.transform(lambda cell: cell.neq(2))
611+
+---------+
612+
| example |
613+
| --- |
614+
| bool |
615+
+=========+
616+
| true |
617+
| false |
618+
+---------+
619+
620+
>>> column.transform(lambda cell: cell != 2)
621+
+---------+
622+
| example |
623+
| --- |
624+
| bool |
625+
+=========+
626+
| true |
627+
| false |
628+
+---------+
629+
"""
630+
return self.__ne__(other)
631+
602632
def ge(self, other: Any) -> Cell[bool]:
603633
"""
604634
Check if greater than or equal to a value. This is equivalent to the `>=` operator.

src/safeds/data/tabular/containers/_lazy_cell.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,27 @@ def __invert__(self) -> Cell[bool]:
3939
return _wrap(self._expression.cast(pl.Boolean).__invert__())
4040

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

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

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

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

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

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

5965
# Comparison ---------------------------------------------------------------

tests/safeds/data/tabular/containers/_cell/test_add.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell + _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 + cell, expected)
2732

33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) + cell, expected)
35+
2836
def test_named_method(self, value1: float, value2: float, expected: float) -> None:
2937
assert_cell_operation_works(value1, lambda cell: cell.add(value2), expected)
38+
39+
def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
40+
assert_cell_operation_works(value1, lambda cell: cell.add(_LazyCell(pl.lit(value2))), expected)

tests/safeds/data/tabular/containers/_cell/test_and.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Any
22

3+
import polars as pl
34
import pytest
5+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
46

57
from tests.helpers import assert_cell_operation_works
68

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

37+
def test_dunder_method_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
38+
assert_cell_operation_works(value1, lambda cell: cell & _LazyCell(pl.lit(value2)), expected)
39+
3540
def test_dunder_method_inverted_order(self, value1: Any, value2: bool, expected: bool) -> None:
3641
assert_cell_operation_works(value2, lambda cell: value1 & cell, expected)
3742

43+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
44+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) & cell, expected)
45+
3846
def test_named_method(self, value1: Any, value2: bool, expected: bool) -> None:
3947
assert_cell_operation_works(value1, lambda cell: cell.and_(value2), expected)
48+
49+
def test_named_method_wrapped_in_cell(self, value1: Any, value2: bool, expected: bool) -> None:
50+
assert_cell_operation_works(value1, lambda cell: cell.and_(_LazyCell(pl.lit(value2))), expected)

tests/safeds/data/tabular/containers/_cell/test_div.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell / _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 / cell, expected)
2732

33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) / cell, expected)
35+
2836
def test_named_method(self, value1: float, value2: float, expected: float) -> None:
2937
assert_cell_operation_works(value1, lambda cell: cell.div(value2), expected)
38+
39+
def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
40+
assert_cell_operation_works(value1, lambda cell: cell.div(_LazyCell(pl.lit(value2))), expected)

tests/safeds/data/tabular/containers/_cell/test_eq.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell == _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 == cell, expected) # type: ignore[arg-type,return-value]
2732

33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) == cell, expected) # type: ignore[arg-type,return-value]
35+
2836
def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
2937
assert_cell_operation_works(value1, lambda cell: cell.eq(value2), expected)
38+
39+
def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
40+
assert_cell_operation_works(value1, lambda cell: cell.eq(_LazyCell(pl.lit(value2))), expected)

tests/safeds/data/tabular/containers/_cell/test_floordiv.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell // _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: float) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 // cell, expected)
32+
33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: float) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) // cell, expected)

tests/safeds/data/tabular/containers/_cell/test_ge.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell >= _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 >= cell, expected)
2732

33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) >= cell, expected)
35+
2836
def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
2937
assert_cell_operation_works(value1, lambda cell: cell.ge(value2), expected)
38+
39+
def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
40+
assert_cell_operation_works(value1, lambda cell: cell.ge(_LazyCell(pl.lit(value2))), expected)

tests/safeds/data/tabular/containers/_cell/test_gt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell > _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 > cell, expected)
2732

33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) > cell, expected)
35+
2836
def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
2937
assert_cell_operation_works(value1, lambda cell: cell.gt(value2), expected)
38+
39+
def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
40+
assert_cell_operation_works(value1, lambda cell: cell.gt(_LazyCell(pl.lit(value2))), expected)

tests/safeds/data/tabular/containers/_cell/test_le.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import polars as pl
12
import pytest
3+
from safeds.data.tabular.containers._lazy_cell import _LazyCell
24

35
from tests.helpers import assert_cell_operation_works
46

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

27+
def test_dunder_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
28+
assert_cell_operation_works(value1, lambda cell: cell <= _LazyCell(pl.lit(value2)), expected)
29+
2530
def test_dunder_method_inverted_order(self, value1: float, value2: float, expected: bool) -> None:
2631
assert_cell_operation_works(value2, lambda cell: value1 <= cell, expected)
2732

33+
def test_dunder_method_inverted_order_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
34+
assert_cell_operation_works(value2, lambda cell: _LazyCell(pl.lit(value1)) <= cell, expected)
35+
2836
def test_named_method(self, value1: float, value2: float, expected: bool) -> None:
2937
assert_cell_operation_works(value1, lambda cell: cell.le(value2), expected)
38+
39+
def test_named_method_wrapped_in_cell(self, value1: float, value2: float, expected: bool) -> None:
40+
assert_cell_operation_works(value1, lambda cell: cell.le(_LazyCell(pl.lit(value2))), expected)

0 commit comments

Comments
 (0)