From: Daniele Varrazzo Date: Sun, 29 Mar 2020 05:03:07 +0000 (+1300) Subject: First pass of mypy annotation X-Git-Tag: 3.0.dev0~650 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3e8fc6d79c463d779ff1be0661ff50b0dbfdb17b;p=thirdparty%2Fpsycopg.git First pass of mypy annotation Just fix what doesn't make mypy happy --- diff --git a/psycopg3/adaptation.py b/psycopg3/adaptation.py index 3a5d61534..710c30f2f 100644 --- a/psycopg3/adaptation.py +++ b/psycopg3/adaptation.py @@ -5,6 +5,7 @@ Entry point into the adaptation system. # Copyright (C) 2020 The Psycopg Team import codecs +from typing import Dict, Tuple from functools import partial from . import exceptions as exc @@ -15,7 +16,7 @@ from .connection import BaseConnection class Adapter: - globals = {} + globals: Dict[Tuple[type, Format], "Adapter"] = {} # TODO: incomplete type def __init__(self, cls, conn): self.cls = cls @@ -62,7 +63,8 @@ class Adapter: class Typecaster: - globals = {} + # TODO: incomplete type + globals: Dict[Tuple[type, Format], "Typecaster"] = {} def __init__(self, oid, conn): self.oid = oid diff --git a/psycopg3/pq/_pq_ctypes.py b/psycopg3/pq/_pq_ctypes.py index 16177776c..31887b241 100644 --- a/psycopg3/pq/_pq_ctypes.py +++ b/psycopg3/pq/_pq_ctypes.py @@ -8,10 +8,15 @@ import ctypes import ctypes.util from ctypes import Structure, POINTER from ctypes import c_char, c_char_p, c_int, c_uint, c_void_p +from typing import List, Tuple from psycopg3.exceptions import NotSupportedError -pq = ctypes.pydll.LoadLibrary(ctypes.util.find_library("pq")) +libname = ctypes.util.find_library("pq") +if libname is None: + raise ImportError("libpq library not found") + +pq = ctypes.pydll.LoadLibrary(libname) # Get the libpq version to define what functions are available. @@ -29,11 +34,11 @@ Oid = c_uint class PGconn_struct(Structure): - _fields_ = [] + _fields_: List[Tuple[str, type]] = [] class PGresult_struct(Structure): - _fields_ = [] + _fields_: List[Tuple[str, type]] = [] class PQconninfoOption_struct(Structure): @@ -131,13 +136,18 @@ PQhost = pq.PQhost PQhost.argtypes = [PGconn_ptr] PQhost.restype = c_char_p +_PQhostaddr = None + if libpq_version >= 120000: - PQhostaddr = pq.PQhostaddr - PQhostaddr.argtypes = [PGconn_ptr] - PQhostaddr.restype = c_char_p -else: + _PQhostaddr = pq.PQhostaddr + _PQhostaddr.argtypes = [PGconn_ptr] + _PQhostaddr.restype = c_char_p + - def PQhostaddr(pgconn): +def PQhostaddr(pgconn): + if _PQhostaddr is not None: + return _PQhostaddr(pgconn) + else: raise NotSupportedError( f"PQhostaddr requires libpq from PostgreSQL 12," f" {libpq_version} available instead" diff --git a/tox.ini b/tox.ini index 95334dd6e..4ccd7e941 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py{36,37,38}, black, flake8 +envlist = py{36,37,38}, black, flake8, mypy [testenv] commands = pytest {posargs} @@ -14,6 +14,10 @@ deps = black commands = flake8 deps = flake8 >= 3.7 +[testenv:mypy] +commands = mypy psycopg3 +deps = mypy >= 0.770 + [flake8] max-line-length = 85 exclude = env, .tox