From: Daniele Varrazzo Date: Mon, 1 Jul 2024 22:10:54 +0000 (+0200) Subject: fix: drop string TypeAlias definitions from public modules X-Git-Tag: 3.2.2~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ad4ea6d0bd6ae0e1a3394e03a385dee2198662d;p=thirdparty%2Fpsycopg.git fix: drop string TypeAlias definitions from public modules The definition of the aliases as strings breaks composition with other types. For instance `DictRow | tuple` is not valid. We could ask people to use strings on their own, but this is a regression anyway. In this changesets only the types reasonably exposed to the public are fixed. Others are internal and I don't think would affect anyone (or, at least, we can ditch the blame and put it on the user...) Modules considered to clean from string typedefs are: - psycopg - psycopg.abc - psycopg.pq - psycopg.pq.abc - psycopg.rows - psycopg_pool - psycopg_pool.abc Close #860. --- diff --git a/docs/news.rst b/docs/news.rst index 06988f9ba..d69f6a2aa 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -7,6 +7,16 @@ ``psycopg`` release notes ========================= +Future releases +--------------- + +Psycopg 3.2.2 (unreleased) +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Drop `!TypeDef` specifications as string from public modules, as they cannot + be composed by users as `!typing` objects previously could (:ticket:`#860`). + + Current release --------------- diff --git a/psycopg/psycopg/abc.py b/psycopg/psycopg/abc.py index edac47c2a..b1e403183 100644 --- a/psycopg/psycopg/abc.py +++ b/psycopg/psycopg/abc.py @@ -8,32 +8,32 @@ from __future__ import annotations from typing import Any, Callable, Generator, Mapping from typing import Protocol, Sequence, TYPE_CHECKING +from typing import Dict, Union # drop with Python 3.8 from . import pq from ._enums import PyFormat as PyFormat -from ._compat import TypeAlias, TypeVar +from ._compat import LiteralString, TypeAlias, TypeVar if TYPE_CHECKING: from . import sql # noqa: F401 from .rows import Row, RowMaker from .pq.abc import PGresult from .waiting import Wait, Ready # noqa: F401 - from ._compat import LiteralString # noqa: F401 from ._adapters_map import AdaptersMap from ._connection_base import BaseConnection NoneType: type = type(None) # An object implementing the buffer protocol -Buffer: TypeAlias = "bytes | bytearray | memoryview" +Buffer: TypeAlias = Union[bytes, bytearray, memoryview] -Query: TypeAlias = "LiteralString | bytes | sql.SQL | sql.Composed" -Params: TypeAlias = "Sequence[Any] | Mapping[str, Any]" +Query: TypeAlias = Union[LiteralString, bytes, "sql.SQL", "sql.Composed"] +Params: TypeAlias = Union[Sequence[Any], Mapping[str, Any]] ConnectionType = TypeVar("ConnectionType", bound="BaseConnection[Any]") PipelineCommand: TypeAlias = Callable[[], None] -DumperKey: TypeAlias = "type | tuple[DumperKey, ...]" -ConnParam: TypeAlias = "str | int | None" -ConnDict: TypeAlias = "dict[str, ConnParam]" +DumperKey: TypeAlias = Union[type, "tuple[DumperKey, ...]"] +ConnParam: TypeAlias = Union[str, int, None] +ConnDict: TypeAlias = Dict[str, ConnParam] ConnMapping: TypeAlias = Mapping[str, ConnParam] diff --git a/psycopg/psycopg/pq/abc.py b/psycopg/psycopg/pq/abc.py index e7d617f38..8e91fd2f5 100644 --- a/psycopg/psycopg/pq/abc.py +++ b/psycopg/psycopg/pq/abc.py @@ -7,6 +7,7 @@ Protocol objects to represent objects exposed by different pq implementations. from __future__ import annotations from typing import Any, Callable, Protocol, Sequence, TYPE_CHECKING +from typing import Union # drop with Python 3.8 from ._enums import Format, Trace from .._compat import Self, TypeAlias @@ -15,7 +16,7 @@ if TYPE_CHECKING: from .misc import PGnotify, ConninfoOption, PGresAttDesc # An object implementing the buffer protocol (ish) -Buffer: TypeAlias = "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 db6f5c86a..fe97b7d56 100644 --- a/psycopg/psycopg/rows.py +++ b/psycopg/psycopg/rows.py @@ -9,6 +9,7 @@ from __future__ import annotations import functools from typing import Any, Callable, NamedTuple, NoReturn from typing import TYPE_CHECKING, Protocol, Sequence +from typing import Dict, Tuple # drop with Python 3.8 from collections import namedtuple from . import pq @@ -82,13 +83,13 @@ class BaseRowFactory(Protocol[Row]): def __call__(self, __cursor: BaseCursor[Any, Any]) -> RowMaker[Row]: ... -TupleRow: TypeAlias = "tuple[Any, ...]" +TupleRow: TypeAlias = Tuple[Any, ...] """ An alias for the type returned by `tuple_row()` (i.e. a tuple of any content). """ -DictRow: TypeAlias = "dict[str, Any]" +DictRow: TypeAlias = Dict[str, Any] """ An alias for the type returned by `dict_row()` diff --git a/psycopg_pool/psycopg_pool/abc.py b/psycopg_pool/psycopg_pool/abc.py index 492917dc0..1c13cad1d 100644 --- a/psycopg_pool/psycopg_pool/abc.py +++ b/psycopg_pool/psycopg_pool/abc.py @@ -7,6 +7,7 @@ Types used in the psycopg_pool package from __future__ import annotations from typing import Awaitable, Callable, TYPE_CHECKING +from typing import Union # drop with Python 3.8 from ._compat import TypeAlias, TypeVar @@ -26,8 +27,8 @@ ConnectionCB: TypeAlias = Callable[[CT], None] AsyncConnectionCB: TypeAlias = Callable[[ACT], Awaitable[None]] # Callbacks to pass the pool to on connection failure -ConnectFailedCB: TypeAlias = "Callable[[ConnectionPool[Any]], None]" -AsyncConnectFailedCB: TypeAlias = ( - "Callable[[AsyncConnectionPool[Any]], None]" - "| Callable[[AsyncConnectionPool[Any]], Awaitable[None]]" -) +ConnectFailedCB: TypeAlias = Callable[["ConnectionPool[Any]"], None] +AsyncConnectFailedCB: TypeAlias = Union[ + Callable[["AsyncConnectionPool[Any]"], None], + Callable[["AsyncConnectionPool[Any]"], Awaitable[None]], +]