]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: change signature of SQL.join() to allow a sequence of Any
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 20 May 2025 15:06:06 +0000 (17:06 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 5 Jul 2025 17:16:35 +0000 (19:16 +0200)
We declared accepting a sequence of Composable, but, because we passed
the joined values to `Composite`, actually non-composable were
interpreted as `Literal`.

Make this behaviour explicit by testing it and fixing the signature.

psycopg/psycopg/sql.py
tests/test_sql.py

index 172110afec099ec2aa58caaac2e70038ee37122f..8e025513cb62e2076bdc41e66a3a9c3ef8824a8b 100644 (file)
@@ -288,12 +288,12 @@ class SQL(Composable):
 
         return Composed(rv)
 
-    def join(self, seq: Iterable[Composable]) -> Composed:
+    def join(self, seq: Iterable[Any]) -> Composed:
         """
         Join a sequence of `Composable`.
 
-        :param seq: the elements to join.
-        :type seq: iterable of `!Composable`
+        :param seq: the elements to join. Elements that are not `Composable`
+            will be considered `Literal`.
 
         Use the `!SQL` object's string to separate the elements in `!seq`.
         Note that `Composed` objects are iterable too, so they can be used as
index a5807f0a0c24ea246418ae8d50acc6e87213254a..1e9c8c342cd4dc05ad50fa70710ad8f8ccfb2c93 100644 (file)
@@ -493,6 +493,10 @@ class TestSQL:
         assert isinstance(obj, sql.Composed)
         assert obj.as_string(conn) == '"foo", bar, 42'
 
+        obj = sql.SQL(", ").join(["foo", "bar", 42])
+        assert isinstance(obj, sql.Composed)
+        assert obj.as_string(conn) == """'foo', 'bar', 42"""
+
         obj = sql.SQL(", ").join([])
         assert obj == sql.Composed([])