From: Daniele Varrazzo Date: Sat, 26 Mar 2022 02:28:19 +0000 (+0100) Subject: refactor: use TypeAlias X-Git-Tag: 3.1~160 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df0da90866f56db892d6957616f7297a387f6df3;p=thirdparty%2Fpsycopg.git refactor: use TypeAlias Upgrade mypy min requirements to 0.940, which is the first supporting TypeAlias after a few broken releases. --- diff --git a/psycopg/psycopg/_compat.py b/psycopg/psycopg/_compat.py index a09dd5cec..25804c8b5 100644 --- a/psycopg/psycopg/_compat.py +++ b/psycopg/psycopg/_compat.py @@ -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", diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 3e409074e..5cd35d7aa 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -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 diff --git a/psycopg/psycopg/_struct.py b/psycopg/psycopg/_struct.py index 520f38e35..ba51164a4 100644 --- a/psycopg/psycopg/_struct.py +++ b/psycopg/psycopg/_struct.py @@ -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): diff --git a/psycopg/psycopg/_transform.py b/psycopg/psycopg/_transform.py index 4be169dbf..11194558b 100644 --- a/psycopg/psycopg/_transform.py +++ b/psycopg/psycopg/_transform.py @@ -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): diff --git a/psycopg/psycopg/_typeinfo.py b/psycopg/psycopg/_typeinfo.py index 853247183..187dc795c 100644 --- a/psycopg/psycopg/_typeinfo.py +++ b/psycopg/psycopg/_typeinfo.py @@ -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. diff --git a/psycopg/psycopg/abc.py b/psycopg/psycopg/abc.py index 7747515dc..227287a05 100644 --- a/psycopg/psycopg/abc.py +++ b/psycopg/psycopg/abc.py @@ -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): diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index 95ba5d76c..a7abef9aa 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -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]): diff --git a/psycopg/psycopg/cursor_async.py b/psycopg/psycopg/cursor_async.py index 1fef161fa..680cac8ae 100644 --- a/psycopg/psycopg/cursor_async.py +++ b/psycopg/psycopg/cursor_async.py @@ -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 diff --git a/psycopg/psycopg/errors.py b/psycopg/psycopg/errors.py index 9ac5ae413..28c678677 100644 --- a/psycopg/psycopg/errors.py +++ b/psycopg/psycopg/errors.py @@ -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]"] = {} diff --git a/psycopg/psycopg/pq/abc.py b/psycopg/psycopg/pq/abc.py index 20da0a3a0..c159fc125 100644 --- a/psycopg/psycopg/pq/abc.py +++ b/psycopg/psycopg/pq/abc.py @@ -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): diff --git a/psycopg/psycopg/rows.py b/psycopg/psycopg/rows.py index d7acac470..99194cd97 100644 --- a/psycopg/psycopg/rows.py +++ b/psycopg/psycopg/rows.py @@ -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()` diff --git a/psycopg/psycopg/types/hstore.py b/psycopg/psycopg/types/hstore.py index 8c59931b7..ed47d0d2d 100644 --- a/psycopg/psycopg/types/hstore.py +++ b/psycopg/psycopg/types/hstore.py @@ -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): diff --git a/psycopg/psycopg/types/net.py b/psycopg/psycopg/types/net.py index ab5bb719f..1fdad84fc 100644 --- a/psycopg/psycopg/types/net.py +++ b/psycopg/psycopg/types/net.py @@ -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] diff --git a/psycopg/setup.cfg b/psycopg/setup.cfg index af59b76fd..fbf87e4fe 100644 --- a/psycopg/setup.cfg +++ b/psycopg/setup.cfg @@ -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] diff --git a/psycopg/setup.py b/psycopg/setup.py index b9c7897e8..068694a96 100644 --- a/psycopg/setup.py +++ b/psycopg/setup.py @@ -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", ], diff --git a/psycopg_pool/psycopg_pool/_compat.py b/psycopg_pool/psycopg_pool/_compat.py index c1b14f2fe..10c570e7f 100644 --- a/psycopg_pool/psycopg_pool/_compat.py +++ b/psycopg_pool/psycopg_pool/_compat.py @@ -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", ] diff --git a/psycopg_pool/setup.cfg b/psycopg_pool/setup.cfg index 8d142b5cf..569c70502 100644 --- a/psycopg_pool/setup.cfg +++ b/psycopg_pool/setup.cfg @@ -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 diff --git a/tests/constraints.txt b/tests/constraints.txt index b560cc520..0466350f3 100644 --- a/tests/constraints.txt +++ b/tests/constraints.txt @@ -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