From a9747467a8b6e8212f758aaceffdc96f087e15bb Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 7 Aug 2024 12:18:25 -0400 Subject: [PATCH] restore generative to with_statement_hint Fixed regression in :meth:`_sql.Select.with_statement_hint` and others where the generative behavior of the method stopped producing a copy of the object. Fixes: #11703 Change-Id: Ia4482f91f76fae9982dc6b075bf5cfec7042ffa6 --- doc/build/changelog/unreleased_20/11703.rst | 7 +++++++ lib/sqlalchemy/sql/selectable.py | 1 + test/sql/test_select.py | 15 +++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 doc/build/changelog/unreleased_20/11703.rst diff --git a/doc/build/changelog/unreleased_20/11703.rst b/doc/build/changelog/unreleased_20/11703.rst new file mode 100644 index 0000000000..5c703138a1 --- /dev/null +++ b/doc/build/changelog/unreleased_20/11703.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, sql, regression + :tickets: 11703 + + Fixed regression in :meth:`_sql.Select.with_statement_hint` and others + where the generative behavior of the method stopped producing a copy of the + object. diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 3c9ca808a3..ad12b00342 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -478,6 +478,7 @@ class HasHints: ("_hints", InternalTraversal.dp_table_hint_list), ] + @_generative def with_statement_hint(self, text: str, dialect_name: str = "*") -> Self: """Add a statement hint to this :class:`_expression.Select` or other selectable object. diff --git a/test/sql/test_select.py b/test/sql/test_select.py index e772c5911d..2bef71dd1e 100644 --- a/test/sql/test_select.py +++ b/test/sql/test_select.py @@ -469,6 +469,21 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): " %(joiner)s SELECT :param_3 AS anon_3" % {"joiner": joiner}, ) + @testing.combinations( + lambda stmt: stmt.with_statement_hint("some hint"), + lambda stmt: stmt.with_hint(table("x"), "some hint"), + lambda stmt: stmt.where(column("q") == 5), + lambda stmt: stmt.having(column("q") == 5), + lambda stmt: stmt.order_by(column("q")), + lambda stmt: stmt.group_by(column("q")), + # TODO: continue + ) + def test_methods_generative(self, testcase): + s1 = select(1) + s2 = testing.resolve_lambda(testcase, stmt=s1) + + assert s1 is not s2 + class ColumnCollectionAsSelectTest(fixtures.TestBase, AssertsCompiledSQL): """tests related to #8285.""" -- 2.47.2