]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Adjust types in test_connection*::test_row_factory()
authorDenis Laxalde <denis.laxalde@dalibo.com>
Fri, 5 Nov 2021 11:05:58 +0000 (12:05 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Nov 2021 01:57:39 +0000 (02:57 +0100)
We avoid using the same variable name for different types. At the end,
there seems to be something wrong, thus add a TODO for later.

tests/test_connection.py
tests/test_connection_async.py
tests/test_cursor.py

index b0b74f65ac50d0b08d53187452bde9e1579b5642..42c5c849ab5a40698eff7f9026d54dfeffeb1879 100644 (file)
@@ -464,8 +464,8 @@ def test_execute_binary(conn):
 
 
 def test_row_factory(dsn):
-    conn = Connection.connect(dsn)
-    assert conn.row_factory is tuple_row  # type: ignore[comparison-overlap]
+    defaultconn = Connection.connect(dsn)
+    assert defaultconn.row_factory is tuple_row  # type: ignore[comparison-overlap]
 
     conn = Connection.connect(dsn, row_factory=my_row_factory)
     assert conn.row_factory is my_row_factory  # type: ignore[comparison-overlap]
@@ -473,17 +473,19 @@ def test_row_factory(dsn):
     cur = conn.execute("select 'a' as ve")
     assert cur.fetchone() == ["Ave"]
 
-    with conn.cursor(row_factory=lambda c: set) as cur:
-        cur.execute("select 1, 1, 2")
-        assert cur.fetchall() == [{1, 2}]
+    with conn.cursor(row_factory=lambda c: set) as cur1:
+        cur1.execute("select 1, 1, 2")
+        assert cur1.fetchall() == [{1, 2}]
 
-    with conn.cursor(row_factory=tuple_row) as cur:
-        cur.execute("select 1, 1, 2")
-        assert cur.fetchall() == [(1, 1, 2)]
+    with conn.cursor(row_factory=tuple_row) as cur2:
+        cur2.execute("select 1, 1, 2")
+        assert cur2.fetchall() == [(1, 1, 2)]
 
-    conn.row_factory = tuple_row
-    cur = conn.execute("select 'vale'")
-    assert cur.fetchone() == ("vale",)
+    # TODO: maybe fix something to get rid of 'type: ignore' below.
+    conn.row_factory = tuple_row  # type: ignore[assignment]
+    cur3 = conn.execute("select 'vale'")
+    r = cur3.fetchone()
+    assert r and r == ("vale",)  # type: ignore[comparison-overlap]
 
 
 def test_str(conn):
index 662f7bcd6c863ef01034e52c1e520656c66c5fc9..650e5f4ea05a7d83c49cb81d9d2335d66b549a70 100644 (file)
@@ -473,8 +473,8 @@ async def test_execute_binary(aconn):
 
 
 async def test_row_factory(dsn):
-    conn = await AsyncConnection.connect(dsn)
-    assert conn.row_factory is tuple_row  # type: ignore[comparison-overlap]
+    defaultconn = await AsyncConnection.connect(dsn)
+    assert defaultconn.row_factory is tuple_row  # type: ignore[comparison-overlap]
 
     conn = await AsyncConnection.connect(dsn, row_factory=my_row_factory)
     assert conn.row_factory is my_row_factory  # type: ignore[comparison-overlap]
@@ -482,17 +482,19 @@ async def test_row_factory(dsn):
     cur = await conn.execute("select 'a' as ve")
     assert await cur.fetchone() == ["Ave"]
 
-    async with conn.cursor(row_factory=lambda c: set) as cur:
-        await cur.execute("select 1, 1, 2")
-        assert await cur.fetchall() == [{1, 2}]
+    async with conn.cursor(row_factory=lambda c: set) as cur1:
+        await cur1.execute("select 1, 1, 2")
+        assert await cur1.fetchall() == [{1, 2}]
 
-    async with conn.cursor(row_factory=tuple_row) as cur:
-        await cur.execute("select 1, 1, 2")
-        assert await cur.fetchall() == [(1, 1, 2)]
+    async with conn.cursor(row_factory=tuple_row) as cur2:
+        await cur2.execute("select 1, 1, 2")
+        assert await cur2.fetchall() == [(1, 1, 2)]
 
-    conn.row_factory = tuple_row
-    cur = await conn.execute("select 'vale'")
-    assert await cur.fetchone() == ("vale",)
+    # TODO: maybe fix something to get rid of 'type: ignore' below.
+    conn.row_factory = tuple_row  # type: ignore[assignment]
+    cur3 = await conn.execute("select 'vale'")
+    r = await cur3.fetchone()
+    assert r and r == ("vale",)  # type: ignore[comparison-overlap]
 
 
 async def test_str(aconn):
index 4f8219bdd5992b72b8ec395698ef1384ac6483ac..e850a2cb3f318d763c22646deff75dba6580ce0f 100644 (file)
@@ -2,7 +2,7 @@ import gc
 import pickle
 import weakref
 import datetime as dt
-from typing import List
+from typing import List, Union
 
 import pytest
 
@@ -10,6 +10,7 @@ import psycopg
 from psycopg import pq, sql, rows
 from psycopg.adapt import PyFormat
 from psycopg.postgres import types as builtins
+from psycopg.rows import RowMaker
 
 from .utils import gc_collect
 
@@ -746,7 +747,9 @@ def test_leak(dsn, faker, fmt, fmt_out, fetch, row_factory, retries):
             ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
 
 
-def my_row_factory(cursor):
+def my_row_factory(
+    cursor: Union[psycopg.Cursor[List[str]], psycopg.AsyncCursor[List[str]]]
+) -> RowMaker[List[str]]:
     if cursor.description is not None:
         titles = [c.name for c in cursor.description]