# Copyright (C) 2020 The Psycopg Team
import codecs
+from typing import Dict, Tuple
from functools import partial
from . import exceptions as exc
class Adapter:
- globals = {}
+ globals: Dict[Tuple[type, Format], "Adapter"] = {} # TODO: incomplete type
def __init__(self, cls, conn):
self.cls = cls
class Typecaster:
- globals = {}
+ # TODO: incomplete type
+ globals: Dict[Tuple[type, Format], "Typecaster"] = {}
def __init__(self, oid, conn):
self.oid = oid
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.
class PGconn_struct(Structure):
- _fields_ = []
+ _fields_: List[Tuple[str, type]] = []
class PGresult_struct(Structure):
- _fields_ = []
+ _fields_: List[Tuple[str, type]] = []
class PQconninfoOption_struct(Structure):
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"
[tox]
-envlist = py{36,37,38}, black, flake8
+envlist = py{36,37,38}, black, flake8, mypy
[testenv]
commands = pytest {posargs}
commands = flake8
deps = flake8 >= 3.7
+[testenv:mypy]
+commands = mypy psycopg3
+deps = mypy >= 0.770
+
[flake8]
max-line-length = 85
exclude = env, .tox