]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Improve the method ``__str__`` of :class:`ColumnCollection`
authorFederico Caselli <cfederico87@gmail.com>
Mon, 23 Mar 2020 20:17:01 +0000 (21:17 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 24 Mar 2020 18:59:52 +0000 (19:59 +0100)
The change avoids confusing a :class:`ColumnCollection` with a python list since the
previous string representation was the same.

Fixes: #5191
Change-Id: Icdbc08f9991d31ce86372505e3614740eaee56e2

doc/build/changelog/unreleased_14/5191.rst [new file with mode: 0644]
lib/sqlalchemy/sql/base.py
test/base/test_utils.py

diff --git a/doc/build/changelog/unreleased_14/5191.rst b/doc/build/changelog/unreleased_14/5191.rst
new file mode 100644 (file)
index 0000000..7d86f53
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: sql, usecase
+    :tickets: 5191
+
+    Change the method ``__str`` of :class:`ColumnCollection` to avoid
+    confusing it with a python list of string.
index 77222706a64c6c9eb6619786391bc03378f70aba..f093cad9093ee0a96dce095fcd7b960244882400 100644 (file)
@@ -676,7 +676,7 @@ class ColumnCollection(object):
         >>> from sqlalchemy import Column, Integer
         >>> from sqlalchemy.sql import ColumnCollection
         >>> x, y = Column('x', Integer), Column('y', Integer)
-        >>> cc = ColumnCollection(columns=[x, y])
+        >>> cc = ColumnCollection(columns=[(x.name, x), (y.name, y)])
         >>> cc.x
         Column('x', Integer(), table=None)
         >>> cc.y
@@ -707,7 +707,7 @@ class ColumnCollection(object):
     returned by key  access is **arbitrary**::
 
         >>> x1, x2 = Column('x', Integer), Column('x', Integer)
-        >>> cc = ColumnCollection(columns=[x1, x2])
+        >>> cc = ColumnCollection(columns=[(x1.name, x1), (x2.name, x2)])
         >>> list(cc)
         [Column('x', Integer(), table=None),
          Column('x', Integer(), table=None)]
@@ -808,7 +808,10 @@ class ColumnCollection(object):
             return default
 
     def __str__(self):
-        return repr([str(c) for c in self])
+        return "%s(%s)" % (
+            self.__class__.__name__,
+            ", ".join(str(c) for c in self),
+        )
 
     def __setitem__(self, key, value):
         raise NotImplementedError()
index 4392df0137e4027a5f7663bd1e16993e89974ff4..662baa38a46399f292db508e807a4121ccfc6a77 100644 (file)
@@ -537,6 +537,17 @@ class ColumnCollectionCommon(testing.AssertsCompiledSQL):
             ).compare(self._column_collection([("col1", c1), ("col2", c2)]))
         )
 
+    def test_str(self):
+        c1 = sql.column("col1")
+        c2 = c1.label("col2")
+        c3 = sql.column("col3")
+        cc = self._column_collection(
+            [("col1", c1), ("col2", c2), ("col3", c3)]
+        )
+
+        eq_(str(cc), "%s(%s, %s, %s)" % (type(cc).__name__, c1, c2, c3))
+        eq_(repr(cc), object.__repr__(cc))
+
 
 class ColumnCollectionTest(ColumnCollectionCommon, fixtures.TestBase):
     def _column_collection(self, columns=None):