]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make test_adapt.py mypy-clean
authorDenis Laxalde <denis.laxalde@dalibo.com>
Fri, 5 Nov 2021 09:51:33 +0000 (10:51 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Nov 2021 01:57:39 +0000 (02:57 +0100)
Declare dynamically filled variables; add assertions about value
returned by Transformer.get_dumper() when a ListDumper is expected
(because of 'sub_dumper' attribute only defined there); check None value
for optional attributes (e.g. sub_dumper); add '# type: ignore' as a
last resort.

pyproject.toml
tests/test_adapt.py

index ac7d5d48429451440a8d4db9f6afd236b27991c8..149c32cde8db10f0fdaf3e2b9dba972d46813bfa 100644 (file)
@@ -24,6 +24,7 @@ files = [
     "tests/pool",
     "tests/pq",
     "tests/scripts",
+    "tests/test_adapt.py",
     "tests/test_conninfo.py",
     "tests/test_dns*",
     "tests/test_errors.py",
index fa9b5e8f5401b3d6ed8075c4c58d32742307157c..499e8ea4eaf6e6dcd95420b5541c1f49039f9c7b 100644 (file)
@@ -1,5 +1,6 @@
 import datetime as dt
 from types import ModuleType
+from typing import Any, List
 
 import pytest
 
@@ -9,6 +10,7 @@ from psycopg import errors as e
 from psycopg.adapt import Transformer, PyFormat, Dumper, Loader
 from psycopg._cmodule import _psycopg
 from psycopg.postgres import types as builtins, TEXT_OID
+from psycopg.types.array import ListDumper, ListBinaryDumper
 
 
 @pytest.mark.parametrize(
@@ -294,19 +296,23 @@ def test_array_dumper(conn, fmt_out):
     fmt_in = PyFormat.from_pq(fmt_out)
     dint = t.get_dumper([0], fmt_in)
     if fmt_out == pq.Format.BINARY:
+        assert isinstance(dint, ListBinaryDumper)
         assert dint.oid == builtins["int2"].array_oid
-        assert dint.sub_dumper.oid == builtins["int2"].oid
+        assert dint.sub_dumper and dint.sub_dumper.oid == builtins["int2"].oid
     else:
+        assert isinstance(dint, ListDumper)
         assert dint.oid == builtins["numeric"].array_oid
         assert dint.sub_dumper is None
 
     dstr = t.get_dumper([""], fmt_in)
     if fmt_in == PyFormat.BINARY:
+        assert isinstance(dstr, ListBinaryDumper)
         assert dstr.oid == builtins["text"].array_oid
-        assert dstr.sub_dumper.oid == builtins["text"].oid
+        assert dstr.sub_dumper and dstr.sub_dumper.oid == builtins["text"].oid
     else:
+        assert isinstance(dstr, ListDumper)
         assert dstr.oid == 0
-        assert dstr.sub_dumper.oid == 0
+        assert dstr.sub_dumper and dstr.sub_dumper.oid == 0
 
     assert dstr is not dint
 
@@ -318,7 +324,7 @@ def test_array_dumper(conn, fmt_out):
     assert dempty.oid == 0
     assert dempty.dump([]) == b"{}"
 
-    L = []
+    L: List[List[Any]] = []
     L.append(L)
     with pytest.raises(psycopg.DataError):
         assert t.get_dumper(L, fmt_in)
@@ -394,27 +400,31 @@ def test_optimised_adapters():
         obj = getattr(_psycopg, n)
         if not isinstance(obj, type):
             continue
-        if not issubclass(obj, (_psycopg.CDumper, _psycopg.CLoader)):
+        if not issubclass(
+            obj,
+            (_psycopg.CDumper, _psycopg.CLoader),  # type: ignore[attr-defined]
+        ):
             continue
         c_adapters[n] = obj
 
     # All the registered adapters
     reg_adapters = set()
     adapters = (
-        list(postgres.adapters._dumpers.values()) + postgres.adapters._loaders
+        list(postgres.adapters._dumpers.values())
+        + postgres.adapters._loaders  # type: ignore[operator]
     )
     assert len(adapters) == 5
     for m in adapters:
         reg_adapters |= set(m.values())
 
     # Check that the registered adapters are the optimised one
-    n = 0
+    i = 0
     for cls in reg_adapters:
         if cls.__name__ in c_adapters:
             assert cls is c_adapters[cls.__name__]
-            n += 1
+            i += 1
 
-    assert n >= 10
+    assert i >= 10
 
     # Check that every optimised adapter is the optimised version of a Py one
     for n in dir(psycopg.types):