]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix mypy errors in test_errors.py
authorDenis Laxalde <denis.laxalde@dalibo.com>
Fri, 5 Nov 2021 08:41:39 +0000 (09:41 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Nov 2021 01:57:39 +0000 (02:57 +0100)
Declare a couple a dynamic variables and check for None value before
checking optional properties' values.

pyproject.toml
tests/test_errors.py

index b38ceb66f8a61a8f8a5c4fdcfec1672d9efce42e..7bce34d6b6e4c78f25d57b07aabab2f124fee702 100644 (file)
@@ -25,6 +25,7 @@ files = [
     "tests/scripts",
     "tests/test_conninfo.py",
     "tests/test_dns*",
+    "tests/test_errors.py",
     "tests/test_prepared*.py",
     "tests/test_psycopg_dbapi20.py",
     "tests/test_sql.py",
index b6065c601eab48ad87ed48ccfa4f5ba8c6a1c347..892b1f6862d01885b4afe51dd0fb37b4f96d9b2a 100644 (file)
@@ -1,4 +1,5 @@
 import pickle
+from typing import List
 from weakref import ref
 
 import pytest
@@ -34,7 +35,8 @@ def test_diag_right_attr(pgconn, monkeypatch):
     res = pgconn.make_empty_result(pq.ExecStatus.NONFATAL_ERROR)
     diag = e.Diagnostic(res)
 
-    checked = []
+    to_check: pq.DiagnosticField
+    checked: List[pq.DiagnosticField] = []
 
     def check_val(self, v):
         nonlocal to_check
@@ -62,7 +64,7 @@ def test_diag_attr_values(conn):
         cur.execute("insert into test_exc values(2)")
     diag = exc.value.diag
     assert diag.sqlstate == "23514"
-    assert diag.schema_name[:7] == "pg_temp"
+    assert diag.schema_name and diag.schema_name[:7] == "pg_temp"
     assert diag.table_name == "test_exc"
     assert diag.constraint_name == "chk_eq1"
     if conn.pgconn.server_version >= 90600:
@@ -97,7 +99,7 @@ def test_error_encoding(conn, enc):
         )
 
     diag = excinfo.value.diag
-    assert f'"{eur}"' in diag.message_primary
+    assert diag.message_primary and f'"{eur}"' in diag.message_primary
     assert diag.sqlstate == "42P01"
 
 
@@ -235,6 +237,7 @@ def test_query_context(conn):
 
     s = str(exc.value)
     assert "from wat" in s, s
+    assert exc.value.diag.message_primary
     assert exc.value.diag.message_primary in s
     assert "ERROR" not in s
     assert not s.endswith("\n")