summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/build/changelog/unreleased_14/5191.rst6
-rw-r--r--lib/sqlalchemy/sql/base.py9
-rw-r--r--test/base/test_utils.py11
3 files changed, 23 insertions, 3 deletions
diff --git a/doc/build/changelog/unreleased_14/5191.rst b/doc/build/changelog/unreleased_14/5191.rst
new file mode 100644
index 000000000..7d86f5311
--- /dev/null
+++ b/doc/build/changelog/unreleased_14/5191.rst
@@ -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.
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py
index 77222706a..f093cad90 100644
--- a/lib/sqlalchemy/sql/base.py
+++ b/lib/sqlalchemy/sql/base.py
@@ -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()
diff --git a/test/base/test_utils.py b/test/base/test_utils.py
index 4392df013..662baa38a 100644
--- a/test/base/test_utils.py
+++ b/test/base/test_utils.py
@@ -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):