]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test: add test to show lack of generic connect typing info
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 May 2022 08:39:49 +0000 (10:39 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 May 2022 08:58:15 +0000 (10:58 +0200)
See #308.

tests/test_typing.py

index e4e55f639aa273792e5fa5e7d34d69d09e70bbd1..fff9cec25b577bae8499b7d10c7bc79222d2dcfa 100644 (file)
@@ -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