]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Harmless refactoring to server-side cursors
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 30 Oct 2021 16:30:17 +0000 (18:30 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 30 Oct 2021 16:44:23 +0000 (18:44 +0200)
It's cool that mypy finds the common base of the two expressions in the
ternary operator.

psycopg/psycopg/server_cursor.py

index 2f4ee7ba4fbc44e709e7582bcb4df127578b2522..f89f470fb11d72b6a101de336444c62c171f24ed 100644 (file)
@@ -45,15 +45,10 @@ class ServerCursorHelper(Generic[ConnectionType, Row]):
         self._format = pq.Format.TEXT
 
     def _repr(self, cur: BaseCursor[ConnectionType, Row]) -> str:
-        cls = f"{cur.__class__.__module__}.{cur.__class__.__qualname__}"
-        info = pq.misc.connection_summary(cur._conn.pgconn)
-        if cur.closed:
-            status = "closed"
-        elif not cur.pgresult:
-            status = "no result"
-        else:
-            status = pq.ExecStatus(cur.pgresult.status).name
-        return f"<{cls} {self.name!r} [{status}] {info} at 0x{id(cur):x}>"
+        # Insert the name as the second word
+        parts = parts = BaseCursor.__repr__(cur).split(None, 1)
+        parts.insert(1, f"{self.name!r}")
+        return " ".join(parts)
 
     def _declare_gen(
         self,
@@ -133,13 +128,9 @@ class ServerCursorHelper(Generic[ConnectionType, Row]):
             yield from cur._start_query()
             yield from self._describe_gen(cur)
 
-        if num is not None:
-            howmuch: sql.Composable = sql.Literal(num)
-        else:
-            howmuch = sql.SQL("ALL")
-
         query = sql.SQL("FETCH FORWARD {} FROM {}").format(
-            howmuch, sql.Identifier(self.name)
+            sql.SQL("ALL") if num is None else sql.Literal(num),
+            sql.Identifier(self.name),
         )
         res = yield from cur._conn._exec_command(
             query, result_format=self._format