]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Operators stubs
authorGleb Kisenkov <g.kisenkov@gmail.com>
Thu, 12 Jan 2023 17:56:29 +0000 (18:56 +0100)
committerGleb Kisenkov <g.kisenkov@gmail.com>
Thu, 12 Jan 2023 17:56:29 +0000 (18:56 +0100)
lib/sqlalchemy/sql/operators.py
test/sql/test_operators.py

index 2bc209eaf6691b681c04a2575113e3356920c75b..a5c7860e8620ea430e7c72c549c877b21f1423b3 100644 (file)
@@ -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
 
index ce71eba704bb3ad6a930eba56d500dd0fef2204d..305b74f6776cf0d1fc8d2ed6b4d5f98de80ed5b5 100644 (file)
@@ -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",
         )