summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-03-17 08:39:45 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2021-03-17 12:30:22 -0400
commitaabc72bd33ba445c0a207432acf0aa1cf25263cb (patch)
treef7c4608bb4a9a6e8516b4bcc24935bb055711f39 /lib/sqlalchemy
parentecb392c5f927ab117f9704ce373bf2af1dbe5b69 (diff)
downloadsqlalchemy-aabc72bd33ba445c0a207432acf0aa1cf25263cb.tar.gz
Provide special row proxies for count and index
The Python ``namedtuple()`` has the behavior such that the names ``count`` and ``index`` will be served as tuple values if the named tuple includes those names; if they are absent, then their behavior as methods of ``collections.abc.Sequence`` is maintained. Therefore the :class:`_result.Row` and :class:`_result.LegacyRow` classes have been fixed so that they work in this same way, maintaining the expected behavior for database rows that have columns named "index" or "count". Fixes: #6074 Change-Id: I49a093da02f33f231d22ed5999c09fcaa3a68601
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/engine/row.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/sqlalchemy/engine/row.py b/lib/sqlalchemy/engine/row.py
index ac65d1b18..b870e6534 100644
--- a/lib/sqlalchemy/engine/row.py
+++ b/lib/sqlalchemy/engine/row.py
@@ -220,6 +220,27 @@ class Row(BaseRow, collections_abc.Sequence):
self._data,
)
+ def _special_name_accessor(name):
+ """Handle ambiguous names such as "count" and "index" """
+
+ @property
+ def go(self):
+ if self._parent._has_key(name):
+ return self.__getattr__(name)
+ else:
+
+ def meth(*arg, **kw):
+ return getattr(collections_abc.Sequence, name)(
+ self, *arg, **kw
+ )
+
+ return meth
+
+ return go
+
+ count = _special_name_accessor("count")
+ index = _special_name_accessor("index")
+
def __contains__(self, key):
return key in self._data