From: Aramís Segovia Date: Mon, 12 May 2025 20:43:47 +0000 (-0400) Subject: Add `__rlshift__` and `__rrshift__` for symmetry X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e06cfdd15b455c407974e5e3fce8c43c2a99438f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add `__rlshift__` and `__rrshift__` for symmetry --- diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index d4486f5e55..737d67b6b5 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -916,6 +916,14 @@ class SQLCoreOperations(Generic[_T_co], ColumnOperators, TypingOnly): def __lshift__(self, other: Any) -> ColumnElement[Any]: ... + @overload + def __rlshift__(self: _SQO[int], other: Any) -> ColumnElement[int]: ... + + @overload + def __rlshift__(self, other: Any) -> ColumnElement[Any]: ... + + def __rlshift__(self, other: Any) -> ColumnElement[Any]: ... + @overload def __rshift__(self: _SQO[int], other: Any) -> ColumnElement[int]: ... @@ -924,6 +932,14 @@ class SQLCoreOperations(Generic[_T_co], ColumnOperators, TypingOnly): def __rshift__(self, other: Any) -> ColumnElement[Any]: ... + @overload + def __rrshift__(self: _SQO[int], other: Any) -> ColumnElement[int]: ... + + @overload + def __rrshift__(self, other: Any) -> ColumnElement[Any]: ... + + def __rrshift__(self, other: Any) -> ColumnElement[Any]: ... + def __matmul__(self, other: Any) -> ColumnElement[Any]: ... def __rmatmul__(self, other: Any) -> ColumnElement[Any]: ... diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index 1a3e24711b..7c36bdc469 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -663,7 +663,7 @@ class ColumnOperators(Operators): return self.operate(getitem, index) def __lshift__(self, other: Any) -> ColumnOperators: - """implement the << operator. + """Implement the ``<<`` operator. Not used by SQLAlchemy core, this is provided for custom operator systems which want to use @@ -671,8 +671,17 @@ class ColumnOperators(Operators): """ return self.operate(lshift, other) + def __rlshift__(self, other: Any) -> ColumnOperators: + """Implement the ``<<`` operator. + + Not used by SQLAlchemy core, this is provided + for custom operator systems which want to use + << as an extension point. + """ + return self.reverse_operate(lshift, other) + def __rshift__(self, other: Any) -> ColumnOperators: - """implement the >> operator. + """Implement the ``>>`` operator. Not used by SQLAlchemy core, this is provided for custom operator systems which want to use @@ -680,6 +689,15 @@ class ColumnOperators(Operators): """ return self.operate(rshift, other) + def __rrshift__(self, other: Any) -> ColumnOperators: + """Implement the ``>>`` operator in reverse. + + Not used by SQLAlchemy core, this is provided + for custom operator systems which want to use + >> as an extension point. + """ + return self.reverse_operate(rshift, other) + def __matmul__(self, other: Any) -> ColumnOperators: """Implement the ``@`` operator. diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index be54cec8e5..b78b3ac1f7 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -967,6 +967,16 @@ class ExtensionOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL): self.assert_compile(Column("x", MyType()) << 5, "x -> :x_1") + def test_rlshift(self): + class MyType(UserDefinedType): + cache_ok = True + + class comparator_factory(UserDefinedType.Comparator): + def __rlshift__(self, other): + return self.op("->")(other) + + self.assert_compile(5 << Column("x", MyType()), "x -> :x_1") + def test_rshift(self): class MyType(UserDefinedType): cache_ok = True @@ -977,6 +987,16 @@ class ExtensionOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL): self.assert_compile(Column("x", MyType()) >> 5, "x -> :x_1") + def test_rrshift(self): + class MyType(UserDefinedType): + cache_ok = True + + class comparator_factory(UserDefinedType.Comparator): + def __rrshift__(self, other): + return self.op("->")(other) + + self.assert_compile(5 >> Column("x", MyType()), "x -> :x_1") + def test_matmul(self): class MyType(UserDefinedType): cache_ok = True