From: Gleb Kisenkov Date: Thu, 12 Jan 2023 17:56:29 +0000 (+0100) Subject: Operators stubs X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e43c4ebe7e9d398d3b0f35a768d4cd2042ec18f1;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Operators stubs --- diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index 2bc209eaf6..a5c7860e86 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -690,17 +690,17 @@ class ColumnOperators(Operators): """ return self.operate(ilike_op, other, escape=escape) - def bitwise_xor(self, other): + def bitwise_xor(self, other: Any) -> ColumnOperators: """Return bitwise XOR operation""" return self.operate(bitwise_xor_op, other) - def bitwise_or(self, other): + def bitwise_or(self, other: Any) -> ColumnOperators: """Return bitwise OR operation""" return self.operate(bitwise_or_op, other) - def bitwise_and(self, other): + def bitwise_and(self, other: Any) -> ColumnOperators: """Return bitwise AND operation""" return self.operate(bitwise_and_op, other) @@ -2145,6 +2145,21 @@ def bitwise_and_op(a: Any, b: Any) -> Any: return a.bitwise_and(b) +@_operator_fn +def bitwise_not_op(a: Any) -> Any: + return a.bitwise_not() + + +@_operator_fn +def bitwise_lshift_op(a: Any, b: Any) -> Any: + return a.bitwise_lshift(b) + + +@_operator_fn +def bitwise_rshift_op(a: Any, b: Any) -> Any: + return a.bitwise_rshift(b) + + def is_comparison(op: OperatorType) -> bool: return op in _comparison or isinstance(op, custom_op) and op.is_comparison diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index ce71eba704..305b74f677 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -4518,26 +4518,19 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL): class BitOpTest(fixtures.TestBase, testing.AssertsCompiledSQL): - def test_bitwise_xor(self): - c1 = column("c1", Integer) - c2 = column("c2", Integer) - self.assert_compile( - select(c1.bitwise_xor(c2)), - "SELECT c1 ^ c2 AS anon_1", - ) - - def test_bitwise_or(self): - c1 = column("c1", Integer) - c2 = column("c2", Integer) - self.assert_compile( - select(c1.bitwise_or(c2)), - "SELECT c1 | c2 AS anon_1", - ) - - def test_bitwise_and(self): + @testing.combinations( + ("xor", operators.bitwise_xor_op, "^"), + ("or", operators.bitwise_or_op, "|"), + ("and", operators.bitwise_and_op, "&"), + ("not", operators.bitwise_not_op, "~"), + ("lshift", operators.bitwise_lshift_op, "<<"), + ("rshift", operators.bitwise_rshift_op, ">>"), + id_="iaa", + ) + def test_default_compile(self, py_op, sql_op): c1 = column("c1", Integer) c2 = column("c2", Integer) self.assert_compile( - select(c1.bitwise_and(c2)), - "SELECT c1 & c2 AS anon_1", + select(py_op(c1, c2)), + f"SELECT c1 {sql_op} c2 AS anon_1", )