]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
repair / test repr() for DDL
authorIuri de Silvio <iuri.desilvio@channable.com>
Mon, 9 Oct 2023 12:21:01 +0000 (08:21 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 9 Oct 2023 12:26:32 +0000 (08:26 -0400)
Fixed 2.0 regression where the :class:`.DDL` construct would no longer
``__repr__()`` due to the removed ``on`` attribute not being accommodated.
Pull request courtesy Iuri de Silvio.

Fixes: #10443
Closes: #10442
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10442
Pull-request-sha: 76f0484091b6a35e5953b8d13f3c66c0b1baa9b0

Change-Id: Id49afd681a04e0f05014105b8f15fdb66e6594a0

doc/build/changelog/unreleased_20/10443.rst [new file with mode: 0644]
lib/sqlalchemy/sql/ddl.py
test/engine/test_ddlevents.py

diff --git a/doc/build/changelog/unreleased_20/10443.rst b/doc/build/changelog/unreleased_20/10443.rst
new file mode 100644 (file)
index 0000000..e0567ed
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 10443
+
+    Fixed 2.0 regression where the :class:`.DDL` construct would no longer
+    ``__repr__()`` due to the removed ``on`` attribute not being accommodated.
+    Pull request courtesy Iuri de Silvio.
index 51d2cdcf97070d2c3c5c348d9a270de7a49df14d..06bbcae2e4b58e01cfbd2be8d6b7c9da408bd61b 100644 (file)
@@ -403,17 +403,14 @@ class DDL(ExecutableDDLElement):
         self.context = context or {}
 
     def __repr__(self):
+        parts = [repr(self.statement)]
+        if self.context:
+            parts.append(f"context={self.context}")
+
         return "<%s@%s; %s>" % (
             type(self).__name__,
             id(self),
-            ", ".join(
-                [repr(self.statement)]
-                + [
-                    "%s=%r" % (key, getattr(self, key))
-                    for key in ("on", "context")
-                    if getattr(self, key)
-                ]
-            ),
+            ", ".join(parts),
         )
 
 
index fd7e5a81725a2eacb171d09b7dabe62b5ce18388..d0c9b428830dee90d118225556c073e3f41a8b43 100644 (file)
@@ -1027,3 +1027,15 @@ class DDLTest(fixtures.TestBase, AssertsCompiledSQL):
             )
             ._should_execute(tbl, cx)
         )
+
+    @testing.variation("include_context", [True, False])
+    def test_repr(self, include_context):
+        sql = "SELECT :foo"
+
+        if include_context:
+            context = {"foo": 1}
+            ddl = DDL(sql, context=context)
+            eq_(repr(ddl), f"<DDL@{id(ddl)}; '{sql}', context={context}>")
+        else:
+            ddl = DDL(sql)
+            eq_(repr(ddl), f"<DDL@{id(ddl)}; '{sql}'>")