From: Iuri de Silvio Date: Mon, 9 Oct 2023 12:21:01 +0000 (-0400) Subject: repair / test repr() for DDL X-Git-Tag: rel_2_0_22~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3a42375d858bdc2654f396be5cb0caf4fc95904f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git repair / test repr() for DDL 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 --- diff --git a/doc/build/changelog/unreleased_20/10443.rst b/doc/build/changelog/unreleased_20/10443.rst new file mode 100644 index 0000000000..e0567ed2c8 --- /dev/null +++ b/doc/build/changelog/unreleased_20/10443.rst @@ -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. diff --git a/lib/sqlalchemy/sql/ddl.py b/lib/sqlalchemy/sql/ddl.py index 51d2cdcf97..06bbcae2e4 100644 --- a/lib/sqlalchemy/sql/ddl.py +++ b/lib/sqlalchemy/sql/ddl.py @@ -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), ) diff --git a/test/engine/test_ddlevents.py b/test/engine/test_ddlevents.py index fd7e5a8172..d0c9b42883 100644 --- a/test/engine/test_ddlevents.py +++ b/test/engine/test_ddlevents.py @@ -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"") + else: + ddl = DDL(sql) + eq_(repr(ddl), f"")