From: Federico Caselli Date: Mon, 23 Mar 2020 20:17:01 +0000 (+0100) Subject: Improve the method ``__str__`` of :class:`ColumnCollection` X-Git-Tag: rel_1_4_0b1~446^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f863344b96c945c56791d31b15a302c2ddd800f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Improve the method ``__str__`` of :class:`ColumnCollection` The change avoids confusing a :class:`ColumnCollection` with a python list since the previous string representation was the same. Fixes: #5191 Change-Id: Icdbc08f9991d31ce86372505e3614740eaee56e2 --- diff --git a/doc/build/changelog/unreleased_14/5191.rst b/doc/build/changelog/unreleased_14/5191.rst new file mode 100644 index 0000000000..7d86f53112 --- /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 77222706a6..f093cad909 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 4392df0137..662baa38a4 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):