]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
support slice access for .c
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 22 Apr 2023 13:41:49 +0000 (09:41 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 22 Apr 2023 16:20:43 +0000 (12:20 -0400)
Added support for slice access with :class:`.ColumnCollection`, e.g.
``table.c[0:5]``, ``subquery.c[:-1]`` etc. Slice access returns a sub
:class:`.ColumnCollection` in the same way as passing a tuple of keys. This
is a natural continuation of the key-tuple access added for :ticket:`8285`,
which it appears to be an oversight that this usage was omitted.

Change-Id: I6378642f39501ffbbae4acadf1dc38a43c39d722
References: #8285
References: #9690

doc/build/changelog/unreleased_20/c_slice.rst [new file with mode: 0644]
lib/sqlalchemy/sql/base.py
test/ext/mypy/plain_files/selectables.py [new file with mode: 0644]
test/sql/test_select.py

diff --git a/doc/build/changelog/unreleased_20/c_slice.rst b/doc/build/changelog/unreleased_20/c_slice.rst
new file mode 100644 (file)
index 0000000..c3e2e4b
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: usecase, sql
+    :tickets: 8285
+
+    Added support for slice access with :class:`.ColumnCollection`, e.g.
+    ``table.c[0:5]``, ``subquery.c[:-1]`` etc. Slice access returns a sub
+    :class:`.ColumnCollection` in the same way as passing a tuple of keys. This
+    is a natural continuation of the key-tuple access added for :ticket:`8285`,
+    which it appears to be an oversight that this usage was omitted.
index 2539277702bc1c15a3d44302597f487b5faaa801..309555338e05e533ff415a06ead18e3355e03d97 100644 (file)
@@ -1591,14 +1591,26 @@ class ColumnCollection(Generic[_COLKEY, _COL_co]):
     ) -> ReadOnlyColumnCollection[_COLKEY, _COL_co]:
         ...
 
+    @overload
+    def __getitem__(
+        self, key: slice
+    ) -> ReadOnlyColumnCollection[_COLKEY, _COL_co]:
+        ...
+
     def __getitem__(
-        self, key: Union[str, int, Tuple[Union[str, int], ...]]
+        self, key: Union[str, int, slice, Tuple[Union[str, int], ...]]
     ) -> Union[ReadOnlyColumnCollection[_COLKEY, _COL_co], _COL_co]:
         try:
-            if isinstance(key, tuple):
-                return ColumnCollection(  # type: ignore
-                    [self._index[sub_key] for sub_key in key]
-                ).as_readonly()
+            if isinstance(key, (tuple, slice)):
+                if isinstance(key, slice):
+                    cols = (
+                        (sub_key, col)
+                        for (sub_key, col, _) in self._collection[key]
+                    )
+                else:
+                    cols = (self._index[sub_key] for sub_key in key)
+
+                return ColumnCollection(cols).as_readonly()
             else:
                 return self._index[key][1]
         except KeyError as err:
diff --git a/test/ext/mypy/plain_files/selectables.py b/test/ext/mypy/plain_files/selectables.py
new file mode 100644 (file)
index 0000000..7d31124
--- /dev/null
@@ -0,0 +1,17 @@
+from sqlalchemy import column
+from sqlalchemy import table
+
+
+def test_col_accessors() -> None:
+    t = table("t", column("a"), column("b"), column("c"))
+
+    t.c.a
+    t.c["a"]
+
+    t.c[2]
+    t.c[0, 1]
+    t.c[0, 1, "b", "c"]
+    t.c[(0, 1, "b", "c")]
+
+    t.c[:-1]
+    t.c[0:2]
index 7979fd200ce9eb44478c12f1f0a4d1f7a4d3f64a..5268c41dca77c1bf39f7672181e63413c37dc691 100644 (file)
@@ -528,6 +528,22 @@ class ColumnCollectionAsSelectTest(fixtures.TestBase, AssertsCompiledSQL):
 
         eq_(list(coll), [table1.c.description, table1.c.myid])
 
+    def test_c_sub_collection_positive_slice(self):
+        coll = table1.c[0:2]
+
+        is_(coll.myid, table1.c.myid)
+        is_(coll.name, table1.c.name)
+
+        eq_(list(coll), [table1.c.myid, table1.c.name])
+
+    def test_c_sub_collection_negative_slice(self):
+        coll = table1.c[-2:]
+
+        is_(coll.name, table1.c.name)
+        is_(coll.description, table1.c.description)
+
+        eq_(list(coll), [table1.c.name, table1.c.description])
+
     def test_missing_key(self):
 
         with expect_raises_message(KeyError, "unknown"):