Skip to content

Commit

Permalink
depr(api): deprecate bool_val.negate()/-bool_val in favor of `~bo…
Browse files Browse the repository at this point in the history
…ol_val`
  • Loading branch information
jcrist committed Sep 10, 2024
1 parent 29b865e commit 499fc03
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 73 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/clickhouse/tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_field_in_literals(con, alltypes, df, container):
tm.assert_series_equal(result_col, expected_col)


@pytest.mark.parametrize("column", ["int_col", "float_col", "bool_col"])
@pytest.mark.parametrize("column", ["int_col", "float_col"])
def test_negate(con, alltypes, column, assert_sql):
expr = -alltypes[column]
assert_sql(expr)
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/impala/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,10 @@ def test_aggregations(alltypes):
d.var(how="pop"),
table.bool_col.any(),
table.bool_col.notany(),
-table.bool_col.any(),
~table.bool_col.any(),
table.bool_col.all(),
table.bool_col.notall(),
-table.bool_col.all(),
~table.bool_col.all(),
table.bool_col.count(where=cond),
d.sum(where=cond),
d.mean(where=cond),
Expand Down
6 changes: 3 additions & 3 deletions ibis/backends/impala/tests/test_value_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_decimal_casts(table, expr_fn, snapshot):
snapshot.assert_match(result, "out.sql")


@pytest.mark.parametrize("colname", ["a", "f", "h"])
@pytest.mark.parametrize("colname", ["a", "f"])
def test_negate(table, colname, snapshot):
result = translate(-table[colname])
snapshot.assert_match(result, "out.sql")
Expand Down Expand Up @@ -263,9 +263,9 @@ def test_correlated_predicate_subquery(table, snapshot):
"expr_fn",
[
param(lambda b: b.any(), id="any"),
param(lambda b: -b.any(), id="not_any"),
param(lambda b: ~b.any(), id="not_any"),
param(lambda b: b.all(), id="all"),
param(lambda b: -b.all(), id="not_all"),
param(lambda b: ~b.all(), id="not_all"),
],
)
def test_any_all(table, expr_fn, snapshot):
Expand Down
16 changes: 3 additions & 13 deletions ibis/backends/postgres/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,13 +1011,11 @@ def test_analytic_functions(alltypes, assert_sql):
assert_sql(expr)


@pytest.mark.parametrize("opname", ["invert", "neg"])
def test_not_and_negate_bool(con, opname, df):
op = getattr(operator, opname)
def test_invert_bool(con, df):
t = con.table("functional_alltypes").limit(10)
expr = t.select(op(t.bool_col).name("bool_col"))
expr = t.select((~t.bool_col).name("bool_col"))
result = expr.execute().bool_col
expected = op(df.head(10).bool_col)
expected = ~df.head(10).bool_col
tm.assert_series_equal(result, expected)


Expand All @@ -1042,14 +1040,6 @@ def test_negate_non_boolean(con, field, df):
tm.assert_series_equal(result, expected)


def test_negate_boolean(con, df):
t = con.table("functional_alltypes").limit(10)
expr = t.select((-t.bool_col).name("bool_col"))
result = expr.execute().bool_col
expected = -df.head(10).bool_col
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("opname", ["sum", "mean", "min", "max", "std", "var"])
def test_boolean_reduction(alltypes, opname, df):
op = operator.methodcaller(opname)
Expand Down
16 changes: 3 additions & 13 deletions ibis/backends/risingwave/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,11 @@ def test_identical_to(con, df):
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("opname", ["invert", "neg"])
def test_not_and_negate_bool(con, opname, df):
op = getattr(operator, opname)
def test_invert_bool(con, df):
t = con.table("functional_alltypes").limit(10)
expr = t.select(op(t.bool_col).name("bool_col"))
expr = t.select((~t.bool_col).name("bool_col"))
result = expr.execute().bool_col
expected = op(df.head(10).bool_col)
expected = ~df.head(10).bool_col
tm.assert_series_equal(result, expected)


Expand All @@ -704,14 +702,6 @@ def test_negate_non_boolean(con, field, df):
tm.assert_series_equal(result, expected)


def test_negate_boolean(con, df):
t = con.table("functional_alltypes").limit(10)
expr = t.select((-t.bool_col).name("bool_col"))
result = expr.execute().bool_col
expected = -df.head(10).bool_col
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("opname", ["sum", "mean", "min", "max", "std", "var"])
def test_boolean_reduction(alltypes, opname, df):
op = operator.methodcaller(opname)
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/tests/sql/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_isnull_notnull(functional_alltypes, method_name, snapshot):


def test_negate(functional_alltypes, snapshot):
expr = -(functional_alltypes.double_col > 0)
expr = ~(functional_alltypes.double_col > 0)
snapshot.assert_match(to_sql(expr.name("tmp")), "out.sql")


Expand Down Expand Up @@ -319,7 +319,7 @@ def test_self_reference_in_not_exists(functional_alltypes, snapshot):
cond = (t.string_col == t2.string_col).any()

semi = t.filter(cond)
anti = t.filter(-cond)
anti = t.filter(~cond)

snapshot.assert_match(to_sql(semi), "semi.sql")
snapshot.assert_match(to_sql(anti), "anti.sql")
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/tests/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def mean_and_std(v):
],
),
param(
lambda t, where: -t.bool_col.any(where=where),
lambda t, where: ~t.bool_col.any(where=where),
lambda t, where: ~t.bool_col[where].any(),
id="any_negate",
marks=[
Expand Down Expand Up @@ -330,7 +330,7 @@ def mean_and_std(v):
],
),
param(
lambda t, where: -t.bool_col.all(where=where),
lambda t, where: ~t.bool_col.all(where=where),
lambda t, where: ~t.bool_col[where].all(),
id="all_negate",
marks=[
Expand Down
8 changes: 3 additions & 5 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import decimal
from collections import Counter
from itertools import permutations
from operator import invert, methodcaller, neg
from operator import invert, methodcaller

import pytest
import toolz
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_null_literal_typed(con, backend):
expr = ibis.null(bool)
assert expr.type() == dt.boolean
assert pd.isna(con.execute(expr))
assert pd.isna(con.execute(expr.negate()))
assert pd.isna(con.execute(~expr))
assert pd.isna(con.execute(expr.cast(str).upper()))


Expand Down Expand Up @@ -1003,15 +1003,13 @@ def test_isin_notin_column_expr(backend, alltypes, df, ibis_op, pandas_op):
param(False, False, toolz.identity, id="false_noop"),
param(True, False, invert, id="true_invert"),
param(False, True, invert, id="false_invert"),
param(True, False, neg, id="true_negate"),
param(False, True, neg, id="false_negate"),
],
)
def test_logical_negation_literal(con, expr, expected, op):
assert con.execute(op(ibis.literal(expr)).name("tmp")) == expected


@pytest.mark.parametrize("op", [toolz.identity, invert, neg])
@pytest.mark.parametrize("op", [toolz.identity, invert])
def test_logical_negation_column(backend, alltypes, df, op):
result = op(alltypes["bool_col"]).name("tmp").execute()
expected = op(df["bool_col"])
Expand Down
35 changes: 9 additions & 26 deletions ibis/expr/types/logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import ibis
import ibis.expr.operations as ops
from ibis import util
from ibis.expr.types.core import _binop
from ibis.expr.types.numeric import NumericColumn, NumericScalar, NumericValue

Expand Down Expand Up @@ -227,34 +228,16 @@ def __invert__(self) -> BooleanValue:
│ NULL │
└──────────┘
"""
return self.negate()
return ops.Not(self).to_expr()

def negate(self) -> BooleanValue:
"""Negate a boolean expression.
Returns
-------
BooleanValue
A boolean value expression
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [True, False, False, None]})
>>> t.values.negate()
┏━━━━━━━━━━━━━┓
┃ Not(values) ┃
┡━━━━━━━━━━━━━┩
│ boolean │
├─────────────┤
│ False │
│ True │
│ True │
│ NULL │
└─────────────┘
"""
return ops.Not(self).to_expr()
"""DEPRECATED."""
util.warn_deprecated(
"`-bool_val`/`bool_val.negate()`",
instead="use `~bool_val` instead",
as_of="9.5",
)
return ~self


@public
Expand Down
10 changes: 4 additions & 6 deletions ibis/tests/expr/test_value_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,17 +501,15 @@ def test_negate(table, col):
assert isinstance(result.op(), ops.Negate)


@pytest.mark.parametrize("op", [operator.neg, operator.invert])
@pytest.mark.parametrize("value", [True, False])
def test_negate_boolean_scalar(op, value):
result = op(ibis.literal(value))
def test_negate_boolean_scalar(value):
result = ~ibis.literal(value)
assert isinstance(result, ir.BooleanScalar)
assert isinstance(result.op(), ops.Not)


@pytest.mark.parametrize("op", [operator.neg, operator.invert])
def test_negate_boolean_column(table, op):
result = op(table["h"])
def test_negate_boolean_column(table):
result = ~table["h"]
assert isinstance(result, ir.BooleanColumn)
assert isinstance(result.op(), ops.Not)

Expand Down

0 comments on commit 499fc03

Please sign in to comment.