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]
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):
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]
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):
import pickle
import weakref
import datetime as dt
-from typing import List
+from typing import List, Union
import pytest
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
), 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]