From: Daniele Varrazzo Date: Tue, 24 May 2022 08:39:49 +0000 (+0200) Subject: test: add test to show lack of generic connect typing info X-Git-Tag: 3.1~71 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f536081e5529baa631af3494c6cae87532c41544;p=thirdparty%2Fpsycopg.git test: add test to show lack of generic connect typing info See #308. --- diff --git a/tests/test_typing.py b/tests/test_typing.py index e4e55f639..fff9cec25 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -407,3 +407,43 @@ reveal_type(ref) assert len(out) == 2, "\n".join(out) got, want = [mypy.get_revealed(line) for line in out] assert got == want + + +@pytest.mark.xfail(reason="https://github.com/psycopg/psycopg/issues/308") +@pytest.mark.parametrize( + "conn, type", + [ + ( + "MyConnection.connect()", + "MyConnection[Tuple[Any, ...]]", + ), + ( + "MyConnection.connect(row_factory=rows.tuple_row)", + "MyConnection[Tuple[Any, ...]]", + ), + ( + "MyConnection.connect(row_factory=rows.dict_row)", + "MyConnection[Dict[str, Any]]", + ), + ], +) +def test_generic_connect(conn, type, mypy): + src = f""" +from typing import Any, Dict, Tuple +import psycopg +from psycopg import rows + +class MyConnection(psycopg.Connection[rows.Row]): + pass + +obj = {conn} +reveal_type(obj) + +ref: {type} = None # type: ignore[assignment] +reveal_type(ref) +""" + cp = mypy.run_on_source(src) + out = cp.stdout.decode("utf8", "replace").splitlines() + assert len(out) == 2, "\n".join(out) + got, want = [mypy.get_revealed(line) for line in out] + assert got == want