]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: drop string TypeAlias definitions from public modules
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 1 Jul 2024 22:10:54 +0000 (00:10 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 2 Jul 2024 09:07:48 +0000 (11:07 +0200)
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.

docs/news.rst
psycopg/psycopg/abc.py
psycopg/psycopg/pq/abc.py
psycopg/psycopg/rows.py
psycopg_pool/psycopg_pool/abc.py

index 06988f9bac84b73f1aac85a115be0574fdea7530..d69f6a2aa780a5db2a03421d2d97b90b486dcf91 100644 (file)
@@ -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
 ---------------
 
index edac47c2a7a0140e704cbe18d00966ab0650324f..b1e403183184a9c514939f8d0371c325da57f128 100644 (file)
@@ -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]
 
 
index e7d617f38f2cf76b0808d86305690cf1b2b0f43f..8e91fd2f57ee9e27d3535d25a8a861c811bfdb6a 100644 (file)
@@ -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):
index db6f5c86aba9c826fc8234785ec720b527096631..fe97b7d567f04c78ace55baf629111ed07eba1b7 100644 (file)
@@ -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()`
 
index 492917dc0f1a852fd4705e8a38470834dabecc8e..1c13cad1d05e4e0a035ad0b2f98cd38acab60443 100644 (file)
@@ -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]],
+]