From: Denis Laxalde Date: Fri, 5 Nov 2021 11:05:58 +0000 (+0100) Subject: Adjust types in test_connection*::test_row_factory() X-Git-Tag: 3.0.3~3^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e8ca377159a8b7948559169872f027c6e9b5d272;p=thirdparty%2Fpsycopg.git Adjust types in test_connection*::test_row_factory() 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. --- diff --git a/tests/test_connection.py b/tests/test_connection.py index b0b74f65a..42c5c849a 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -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): diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 662f7bcd6..650e5f4ea 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -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): diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 4f8219bdd..e850a2cb3 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -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]