]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: use TypeAlias
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 26 Mar 2022 02:28:19 +0000 (03:28 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 26 Mar 2022 02:30:53 +0000 (03:30 +0100)
Upgrade mypy min requirements to 0.940, which is the first supporting
TypeAlias after a few broken releases.

18 files changed:
psycopg/psycopg/_compat.py
psycopg/psycopg/_preparing.py
psycopg/psycopg/_struct.py
psycopg/psycopg/_transform.py
psycopg/psycopg/_typeinfo.py
psycopg/psycopg/abc.py
psycopg/psycopg/connection.py
psycopg/psycopg/cursor_async.py
psycopg/psycopg/errors.py
psycopg/psycopg/pq/abc.py
psycopg/psycopg/rows.py
psycopg/psycopg/types/hstore.py
psycopg/psycopg/types/net.py
psycopg/setup.cfg
psycopg/setup.py
psycopg_pool/psycopg_pool/_compat.py
psycopg_pool/setup.cfg
tests/constraints.txt

index a09dd5cecc2eb3e429345b7169b75c1bf7ee3128..25804c8b58660c98d0f5680d2dd6da3a47f3a26a 100644 (file)
@@ -14,7 +14,7 @@ else:
     from typing_extensions import Protocol
 
 T = TypeVar("T")
-FutureT = Union["asyncio.Future[T]", Generator[Any, None, T], Awaitable[T]]
+FutureT: "TypeAlias" = Union["asyncio.Future[T]", Generator[Any, None, T], Awaitable[T]]
 
 if sys.version_info >= (3, 8):
     create_task = asyncio.create_task
@@ -35,14 +35,15 @@ else:
     from typing import Counter, Deque
 
 if sys.version_info >= (3, 10):
-    from typing import TypeGuard
+    from typing import TypeAlias, TypeGuard
 else:
-    from typing_extensions import TypeGuard
+    from typing_extensions import TypeAlias, TypeGuard
 
 __all__ = [
     "Counter",
     "Deque",
     "Protocol",
+    "TypeAlias",
     "TypeGuard",
     "ZoneInfo",
     "create_task",
index 3e409074e80cf0641fb0651fa88048554783a8ab..5cd35d7aa88d9283a68cc355acb952f3f5c4288c 100644 (file)
@@ -9,12 +9,14 @@ from typing import Iterator, Optional, Sequence, Tuple, TYPE_CHECKING
 from collections import OrderedDict
 
 from .pq import ExecStatus
-from ._compat import Deque
+from ._compat import Deque, TypeAlias
 from ._queries import PostgresQuery
 
 if TYPE_CHECKING:
     from .pq.abc import PGresult
 
+Key: TypeAlias = Tuple[bytes, Tuple[int, ...]]
+
 
 class Prepare(IntEnum):
     NO = auto()
@@ -22,9 +24,6 @@ class Prepare(IntEnum):
     SHOULD = auto()
 
 
-Key = Tuple[bytes, Tuple[int, ...]]
-
-
 class PrepareManager:
     # Number of times a query is executed before it is prepared.
     prepare_threshold: Optional[int] = 5
index 520f38e35f821e5ff23591620fa9dbd007b1e27b..ba51164a4c2c628df558cedb5ab487469dea00b1 100644 (file)
@@ -8,12 +8,12 @@ import struct
 from typing import Callable, cast, Optional, Tuple
 
 from .abc import Buffer
-from ._compat import Protocol
+from ._compat import Protocol, TypeAlias
 
-PackInt = Callable[[int], bytes]
-UnpackInt = Callable[[bytes], Tuple[int]]
-PackFloat = Callable[[float], bytes]
-UnpackFloat = Callable[[bytes], Tuple[float]]
+PackInt: TypeAlias = Callable[[int], bytes]
+UnpackInt: TypeAlias = Callable[[bytes], Tuple[int]]
+PackFloat: TypeAlias = Callable[[float], bytes]
+UnpackFloat: TypeAlias = Callable[[bytes], Tuple[float]]
 
 
 class UnpackLen(Protocol):
index 4be169dbffb304f6d37d3dded6f292744625a4bc..11194558be4de71c894dacd78c8a2f4902e66735 100644 (file)
@@ -13,6 +13,7 @@ from . import postgres
 from . import errors as e
 from .abc import Buffer, LoadFunc, AdaptContext, PyFormat, DumperKey
 from .rows import Row, RowMaker
+from ._compat import TypeAlias
 from .postgres import INVALID_OID
 
 if TYPE_CHECKING:
@@ -22,9 +23,9 @@ if TYPE_CHECKING:
     from .connection import BaseConnection
 
 NoneType: Type[None] = type(None)
-DumperCache = Dict[DumperKey, "Dumper"]
-OidDumperCache = Dict[int, "Dumper"]
-LoaderCache = Dict[int, "Loader"]
+DumperCache: TypeAlias = Dict[DumperKey, "Dumper"]
+OidDumperCache: TypeAlias = Dict[int, "Dumper"]
+LoaderCache: TypeAlias = Dict[int, "Loader"]
 
 
 class Transformer(AdaptContext):
index 853247183840abaaec3686623cb6379944d4a65e..187dc795cf7ae2472dfa90cd6469e0ec844bc353 100644 (file)
@@ -13,6 +13,7 @@ from typing import Sequence, Tuple, Type, TypeVar, Union, TYPE_CHECKING
 from . import errors as e
 from .abc import AdaptContext
 from .rows import dict_row
+from ._compat import TypeAlias
 
 if TYPE_CHECKING:
     from .connection import Connection
@@ -20,6 +21,7 @@ if TYPE_CHECKING:
     from .sql import Identifier
 
 T = TypeVar("T", bound="TypeInfo")
+RegistryKey: TypeAlias = Union[str, int, Tuple[type, int]]
 
 
 class TypeInfo:
@@ -283,9 +285,6 @@ WHERE t.oid = %(name)s::regtype
 """
 
 
-RegistryKey = Union[str, int, Tuple[type, int]]
-
-
 class TypesRegistry:
     """
     Container for the information about types in a database.
index 7747515dc748605620c75a31085a5409c06ed075..227287a0583709ceffd3e49fe388d370080b802b 100644 (file)
@@ -10,7 +10,7 @@ from typing import TYPE_CHECKING
 
 from . import pq
 from ._enums import PyFormat as PyFormat
-from ._compat import Protocol
+from ._compat import Protocol, TypeAlias
 
 if TYPE_CHECKING:
     from .sql import Composable
@@ -21,35 +21,35 @@ if TYPE_CHECKING:
     from ._adapters_map import AdaptersMap
 
 # An object implementing the buffer protocol
-Buffer = Union[bytes, bytearray, memoryview]
+Buffer: TypeAlias = Union[bytes, bytearray, memoryview]
 
-Query = Union[str, bytes, "Composable"]
-Params = Union[Sequence[Any], Mapping[str, Any]]
+Query: TypeAlias = Union[str, bytes, "Composable"]
+Params: TypeAlias = Union[Sequence[Any], Mapping[str, Any]]
 ConnectionType = TypeVar("ConnectionType", bound="BaseConnection[Any]")
 
 # TODO: make it recursive when mypy will support it
-# DumperKey = Union[type, Tuple[Union[type, "DumperKey"]]]
-DumperKey = Union[type, Tuple[type, ...]]
+# DumperKey: TypeAlias = Union[type, Tuple[Union[type, "DumperKey"]]]
+DumperKey: TypeAlias = Union[type, Tuple[type, ...]]
 
 # Waiting protocol types
 
 RV = TypeVar("RV")
 
-PQGenConn = Generator[Tuple[int, "Wait"], "Ready", RV]
+PQGenConn: TypeAlias = Generator[Tuple[int, "Wait"], "Ready", RV]
 """Generator for processes where the connection file number can change.
 
 This can happen in connection and reset, but not in normal querying.
 """
 
-PQGen = Generator["Wait", "Ready", RV]
+PQGen: TypeAlias = Generator["Wait", "Ready", RV]
 """Generator for processes where the connection file number won't change.
 """
 
 
 # Adaptation types
 
-DumpFunc = Callable[[Any], bytes]
-LoadFunc = Callable[[bytes], Any]
+DumpFunc: TypeAlias = Callable[[Any], bytes]
+LoadFunc: TypeAlias = Callable[[bytes], Any]
 
 
 class AdaptContext(Protocol):
index 95ba5d76cb5803e2baf6cbb9efbbf31d83141d1e..a7abef9aa226af64ba6f85617f96aa4cea411218 100644 (file)
@@ -28,6 +28,7 @@ from .rows import Row, RowFactory, tuple_row, TupleRow, args_row
 from .adapt import AdaptersMap
 from ._enums import IsolationLevel
 from .cursor import Cursor
+from ._compat import TypeAlias
 from ._cmodule import _psycopg
 from .conninfo import make_conninfo, conninfo_to_dict, ConnectionInfo
 from .generators import notifies
@@ -75,8 +76,8 @@ class Notify(NamedTuple):
 
 Notify.__module__ = "psycopg"
 
-NoticeHandler = Callable[[e.Diagnostic], None]
-NotifyHandler = Callable[[Notify], None]
+NoticeHandler: TypeAlias = Callable[[e.Diagnostic], None]
+NotifyHandler: TypeAlias = Callable[[Notify], None]
 
 
 class BaseConnection(Generic[Row]):
index 1fef161fa4fca23729f136f2eb90c2f136bef9f5..680cac8aefec254c78d89e4898953b244fc5b28e 100644 (file)
@@ -10,7 +10,6 @@ from typing import Optional, Type, TypeVar, TYPE_CHECKING
 from contextlib import asynccontextmanager
 
 from . import errors as e
-
 from .abc import Query, Params
 from .copy import AsyncCopy
 from .rows import Row, RowMaker, AsyncRowFactory
index 9ac5ae4137d07de98aef491b00c71b64a88bf004..28c678677cb5186f6b7d9088fe3046704ba2aa83 100644 (file)
@@ -22,9 +22,9 @@ from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union
 
 from .pq.abc import PGconn, PGresult
 from .pq._enums import DiagnosticField
-from ._compat import TypeGuard
+from ._compat import TypeAlias, TypeGuard
 
-ErrorInfo = Union[None, PGresult, Dict[int, Optional[bytes]]]
+ErrorInfo: TypeAlias = Union[None, PGresult, Dict[int, Optional[bytes]]]
 
 _sqlcodes: Dict[str, "Type[Error]"] = {}
 
index 20da0a3a042c83378b6ecbcb434c13d09d030ae6..c159fc12581e0e89706bb4ac8248bede62287cbe 100644 (file)
@@ -8,13 +8,13 @@ from typing import Any, Callable, List, Optional, Sequence, Tuple
 from typing import Union, TYPE_CHECKING
 
 from ._enums import Format, Trace
-from .._compat import Protocol
+from .._compat import Protocol, TypeAlias
 
 if TYPE_CHECKING:
     from .misc import PGnotify, ConninfoOption, PGresAttDesc
 
 # An object implementing the buffer protocol (ish)
-Buffer = Union[bytes, bytearray, memoryview]
+Buffer: TypeAlias = Union[bytes, bytearray, memoryview]
 
 
 class PGconn(Protocol):
index d7acac4703e3e02605225b9bb5babb0f18b26050..99194cd97f117fdc7570b093721f1eb2a15b07b3 100644 (file)
@@ -11,7 +11,7 @@ from typing import TYPE_CHECKING, Type, TypeVar
 from collections import namedtuple
 
 from . import errors as e
-from ._compat import Protocol
+from ._compat import Protocol, TypeAlias
 
 if TYPE_CHECKING:
     from .cursor import BaseCursor, Cursor
@@ -77,13 +77,13 @@ class BaseRowFactory(Protocol[Row]):
         ...
 
 
-TupleRow = Tuple[Any, ...]
+TupleRow: TypeAlias = Tuple[Any, ...]
 """
 An alias for the type returned by `tuple_row()` (i.e. a tuple of any content).
 """
 
 
-DictRow = Dict[str, Any]
+DictRow: TypeAlias = Dict[str, Any]
 """
 An alias for the type returned by `dict_row()`
 
index 8c59931b776d265e435128dff3453c62074bee1d..ed47d0d2dd154f0d464747df0637bb470bf24b98 100644 (file)
@@ -11,6 +11,7 @@ from .. import errors as e
 from .. import postgres
 from ..abc import Buffer, AdaptContext
 from ..adapt import PyFormat, RecursiveDumper, RecursiveLoader
+from .._compat import TypeAlias
 from ..postgres import TEXT_OID
 from .._typeinfo import TypeInfo
 
@@ -34,7 +35,7 @@ _re_hstore = re.compile(
 )
 
 
-Hstore = Dict[str, Optional[str]]
+Hstore: TypeAlias = Dict[str, Optional[str]]
 
 
 class BaseHstoreDumper(RecursiveDumper):
index ab5bb719fcacc17392d30dfebd7fc5326ebad3ec..1fdad84fc9a0f15aa4d469aa3df946bdb066b05a 100644 (file)
@@ -10,13 +10,14 @@ from .. import postgres
 from ..pq import Format
 from ..abc import AdaptContext
 from ..adapt import Buffer, Dumper, Loader
+from .._compat import TypeAlias
 
 if TYPE_CHECKING:
     import ipaddress
 
-Address = Union["ipaddress.IPv4Address", "ipaddress.IPv6Address"]
-Interface = Union["ipaddress.IPv4Interface", "ipaddress.IPv6Interface"]
-Network = Union["ipaddress.IPv4Network", "ipaddress.IPv6Network"]
+Address: TypeAlias = Union["ipaddress.IPv4Address", "ipaddress.IPv6Address"]
+Interface: TypeAlias = Union["ipaddress.IPv4Interface", "ipaddress.IPv6Interface"]
+Network: TypeAlias = Union["ipaddress.IPv4Network", "ipaddress.IPv6Network"]
 
 # These objects will be imported lazily
 ip_address: Callable[[str], Address] = None  # type: ignore[assignment]
index af59b76fd5db42c4d9cb9f60f7ee01dc4c0baf94..fbf87e4fe542be87951d149f2fadeaffb16a96d8 100644 (file)
@@ -39,7 +39,7 @@ packages = find:
 zip_safe = False
 install_requires =
     backports.zoneinfo >= 0.2.0; python_version < "3.9"
-    typing_extensions >= 3.10; python_version < "3.10"
+    typing-extensions >= 3.10; python_version < "3.10"
     tzdata; sys_platform == "win32"
 
 [options.package_data]
index b9c7897e8d8603646615cf6eb0f1b6374dc8aa9c..068694a96bf692c648d8a7d68367f2e94bd768e8 100644 (file)
@@ -37,7 +37,7 @@ extras_require = {
     ],
     # Requirements to run the test suite
     "test": [
-        "mypy >= 0.920, != 0.930, != 0.931",
+        "mypy >= 0.940",
         "pproxy >= 2.7",
         "pytest >= 6.2.5",
         "pytest-asyncio >= 0.17",
@@ -49,7 +49,7 @@ extras_require = {
         "black >= 22.1.0",
         "dnspython >= 2.1",
         "flake8 >= 4.0",
-        "mypy >= 0.920, != 0.930, != 0.931",
+        "mypy >= 0.940",
         "types-setuptools >= 57.4",
         "wheel >= 0.37",
     ],
index c1b14f2fe414c94ccb3d9f1170a42c642d35da92..10c570e7f8b9fb567f2cf38e011bf73c86272632 100644 (file)
@@ -11,7 +11,7 @@ from typing import Any, Awaitable, Generator, Optional, Union, Type, TypeVar
 import psycopg.errors as e
 
 T = TypeVar("T")
-FutureT = Union["asyncio.Future[T]", Generator[Any, None, T], Awaitable[T]]
+FutureT: "TypeAlias" = Union["asyncio.Future[T]", Generator[Any, None, T], Awaitable[T]]
 
 if sys.version_info >= (3, 8):
     create_task = asyncio.create_task
@@ -31,10 +31,16 @@ if sys.version_info >= (3, 9):
 else:
     from typing import Counter, Deque
 
+if sys.version_info >= (3, 10):
+    from typing import TypeAlias
+else:
+    from typing_extensions import TypeAlias
+
 __all__ = [
     "Counter",
     "Deque",
     "Task",
+    "TypeAlias",
     "create_task",
 ]
 
index 8d142b5cfb6a36eb884de7055d5009dfaff4b232..569c70502743fe427f3814f09a050bc4e3f618b3 100644 (file)
@@ -37,9 +37,8 @@ license_file = LICENSE.txt
 python_requires = >= 3.7
 packages = find:
 zip_safe = False
-# Maybe it can be useful after release, now it gets in the way.
-# install_requires =
-#     psycopg >= 3, < 4
+install_requires =
+    typing-extensions >= 3.10; python_version < "3.10"
 
 [options.package_data]
 psycopg_pool = py.typed
index b560cc520853a97c687f4f2d028588f991bc32d0..0466350f350df9f82e810d83b98d7679b5580eb2 100644 (file)
@@ -5,10 +5,10 @@
 
 # From install_requires
 backports.zoneinfo == 0.2.0
-typing_extensions == 3.10.0.0
+typing-extensions == 3.10.0.0
 
 # From the 'test' extra
-mypy == 0.920
+mypy == 0.940
 pproxy == 2.7.0
 pytest == 6.2.5
 pytest-asyncio == 0.17.0
@@ -19,7 +19,7 @@ pytest-randomly == 3.10.0
 black == 22.1.0
 dnspython == 2.1.0
 flake8 == 4.0.0
-mypy == 0.920
+mypy == 0.940
 types-setuptools == 57.4.0
 wheel == 0.37