From: Daniele Varrazzo Date: Thu, 30 May 2024 01:34:17 +0000 (+0200) Subject: refactor: drop use of typing.Tuple X-Git-Tag: 3.2.0~19^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=36eeb172114a79201566b6adbff119cced32d798;p=thirdparty%2Fpsycopg.git refactor: drop use of typing.Tuple --- diff --git a/psycopg/psycopg/_connection_base.py b/psycopg/psycopg/_connection_base.py index 98eda8a74..691969b81 100644 --- a/psycopg/psycopg/_connection_base.py +++ b/psycopg/psycopg/_connection_base.py @@ -8,7 +8,7 @@ from __future__ import annotations import sys import logging -from typing import Callable, Generic, NamedTuple, Tuple, TYPE_CHECKING +from typing import Callable, Generic, NamedTuple, TYPE_CHECKING from weakref import ref, ReferenceType from warnings import warn from functools import partial @@ -114,7 +114,7 @@ class BaseConnection(Generic[Row]): self._closed = False # closed by an explicit close() self._prepared: PrepareManager = PrepareManager() - self._tpc: Tuple[Xid, bool] | None = None # xid, prepared + self._tpc: tuple[Xid, bool] | None = None # xid, prepared wself = ref(self) pgconn.notice_handler = partial(BaseConnection._notice_handler, wself) diff --git a/psycopg/psycopg/_copy.py b/psycopg/psycopg/_copy.py index 7cbd98853..1c393a9d6 100644 --- a/psycopg/psycopg/_copy.py +++ b/psycopg/psycopg/_copy.py @@ -11,7 +11,7 @@ from __future__ import annotations from abc import ABC, abstractmethod from types import TracebackType -from typing import Any, Iterator, Tuple, Sequence, TYPE_CHECKING +from typing import Any, Iterator, Sequence, TYPE_CHECKING from . import pq from . import errors as e @@ -95,7 +95,7 @@ class Copy(BaseCopy["Connection[Any]"]): """ return self.connection.wait(self._read_gen()) - def rows(self) -> Iterator[Tuple[Any, ...]]: + def rows(self) -> Iterator[tuple[Any, ...]]: """ Iterate on the result of a :sql:`COPY TO` operation record by record. @@ -108,7 +108,7 @@ class Copy(BaseCopy["Connection[Any]"]): break yield record - def read_row(self) -> Tuple[Any, ...] | None: + def read_row(self) -> tuple[Any, ...] | None: """ Read a parsed row of data from a table after a :sql:`COPY TO` operation. diff --git a/psycopg/psycopg/_copy_async.py b/psycopg/psycopg/_copy_async.py index 8f3900542..2c630da9b 100644 --- a/psycopg/psycopg/_copy_async.py +++ b/psycopg/psycopg/_copy_async.py @@ -8,7 +8,7 @@ from __future__ import annotations from abc import ABC, abstractmethod from types import TracebackType -from typing import Any, AsyncIterator, Tuple, Sequence, TYPE_CHECKING +from typing import Any, AsyncIterator, Sequence, TYPE_CHECKING from . import pq from . import errors as e @@ -92,7 +92,7 @@ class AsyncCopy(BaseCopy["AsyncConnection[Any]"]): """ return await self.connection.wait(self._read_gen()) - async def rows(self) -> AsyncIterator[Tuple[Any, ...]]: + async def rows(self) -> AsyncIterator[tuple[Any, ...]]: """ Iterate on the result of a :sql:`COPY TO` operation record by record. @@ -105,7 +105,7 @@ class AsyncCopy(BaseCopy["AsyncConnection[Any]"]): break yield record - async def read_row(self) -> Tuple[Any, ...] | None: + async def read_row(self) -> tuple[Any, ...] | None: """ Read a parsed row of data from a table after a :sql:`COPY TO` operation. diff --git a/psycopg/psycopg/_copy_base.py b/psycopg/psycopg/_copy_base.py index 2ae2c5502..354b589e4 100644 --- a/psycopg/psycopg/_copy_base.py +++ b/psycopg/psycopg/_copy_base.py @@ -10,7 +10,7 @@ import re import sys import struct from abc import ABC, abstractmethod -from typing import Any, Generic, Match, Sequence, Tuple, TYPE_CHECKING +from typing import Any, Generic, Match, Sequence, TYPE_CHECKING from . import pq from . import adapt @@ -164,7 +164,7 @@ class BaseCopy(Generic[ConnectionType]): self.cursor._rowcount = nrows if nrows is not None else -1 return memoryview(b"") - def _read_row_gen(self) -> PQGen[Tuple[Any, ...] | None]: + def _read_row_gen(self) -> PQGen[tuple[Any, ...] | None]: data = yield from self._read_gen() if not data: return None @@ -199,7 +199,7 @@ class Formatter(ABC): self._row_mode = False # true if the user is using write_row() @abstractmethod - def parse_row(self, data: Buffer) -> Tuple[Any, ...] | None: ... + def parse_row(self, data: Buffer) -> tuple[Any, ...] | None: ... @abstractmethod def write(self, buffer: Buffer | str) -> Buffer: ... @@ -218,7 +218,7 @@ class TextFormatter(Formatter): super().__init__(transformer) self._encoding = encoding - def parse_row(self, data: Buffer) -> Tuple[Any, ...] | None: + def parse_row(self, data: Buffer) -> tuple[Any, ...] | None: if data: return parse_row_text(data, self.transformer) else: @@ -262,7 +262,7 @@ class BinaryFormatter(Formatter): super().__init__(transformer) self._signature_sent = False - def parse_row(self, data: Buffer) -> Tuple[Any, ...] | None: + def parse_row(self, data: Buffer) -> tuple[Any, ...] | None: if not self._signature_sent: if data[: len(_binary_signature)] != _binary_signature: raise e.DataError( @@ -365,7 +365,7 @@ def _format_row_binary( return out -def _parse_row_text(data: Buffer, tx: Transformer) -> Tuple[Any, ...]: +def _parse_row_text(data: Buffer, tx: Transformer) -> tuple[Any, ...]: if not isinstance(data, bytes): data = bytes(data) fields = data.split(b"\t") @@ -374,7 +374,7 @@ def _parse_row_text(data: Buffer, tx: Transformer) -> Tuple[Any, ...]: return tx.load_sequence(row) -def _parse_row_binary(data: Buffer, tx: Transformer) -> Tuple[Any, ...]: +def _parse_row_binary(data: Buffer, tx: Transformer) -> tuple[Any, ...]: row: list[Buffer | None] = [] nfields = _unpack_int2(data, 0)[0] pos = 2 diff --git a/psycopg/psycopg/_cursor_base.py b/psycopg/psycopg/_cursor_base.py index 3da9ce5b4..6fb08dcfa 100644 --- a/psycopg/psycopg/_cursor_base.py +++ b/psycopg/psycopg/_cursor_base.py @@ -7,7 +7,7 @@ Psycopg BaseCursor object from __future__ import annotations from functools import partial -from typing import Any, Generic, Iterable, NoReturn, Sequence, Tuple +from typing import Any, Generic, Iterable, NoReturn, Sequence from typing import TYPE_CHECKING from . import pq @@ -306,7 +306,7 @@ class BaseCursor(Generic[ConnectionType, Row]): def _get_prepared( self, pgq: PostgresQuery, prepare: bool | None = None - ) -> Tuple[Prepare, bytes]: + ) -> tuple[Prepare, bytes]: return self._conn._prepared.get(pgq, prepare) def _stream_send_gen( diff --git a/psycopg/psycopg/_pipeline.py b/psycopg/psycopg/_pipeline.py index 3f4ad769d..239b619b5 100644 --- a/psycopg/psycopg/_pipeline.py +++ b/psycopg/psycopg/_pipeline.py @@ -8,7 +8,7 @@ from __future__ import annotations import logging from types import TracebackType -from typing import Any, Tuple, TYPE_CHECKING +from typing import Any, TYPE_CHECKING from . import pq from . import errors as e @@ -29,7 +29,7 @@ if TYPE_CHECKING: PendingResult: TypeAlias = ( - Tuple["BaseCursor[Any, Any]", Tuple[Key, Prepare, bytes] | None] | None + tuple[BaseCursor[Any, Any], tuple[Key, Prepare, bytes] | None] | None ) FATAL_ERROR = pq.ExecStatus.FATAL_ERROR diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 9c017e73e..eccf260de 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -7,7 +7,7 @@ Support for prepared statements from __future__ import annotations from enum import IntEnum, auto -from typing import Sequence, Tuple, TYPE_CHECKING +from typing import Sequence, TYPE_CHECKING from collections import OrderedDict from . import pq @@ -20,7 +20,7 @@ if TYPE_CHECKING: from .pq.abc import PGresult from ._connection_base import BaseConnection -Key: TypeAlias = Tuple[bytes, Tuple[int, ...]] +Key: TypeAlias = tuple[bytes, tuple[int, ...]] COMMAND_OK = pq.ExecStatus.COMMAND_OK TUPLES_OK = pq.ExecStatus.TUPLES_OK @@ -57,7 +57,7 @@ class PrepareManager: def get( self, query: PostgresQuery, prepare: bool | None = None - ) -> Tuple[Prepare, bytes]: + ) -> tuple[Prepare, bytes]: """ Check if a query is prepared, tell back whether to prepare it. """ diff --git a/psycopg/psycopg/_py_transformer.py b/psycopg/psycopg/_py_transformer.py index 260d8d30c..3a1c2e5ab 100644 --- a/psycopg/psycopg/_py_transformer.py +++ b/psycopg/psycopg/_py_transformer.py @@ -11,7 +11,7 @@ dependencies problems). from __future__ import annotations -from typing import Any, Sequence, Tuple, DefaultDict, TYPE_CHECKING +from typing import Any, Sequence, DefaultDict, TYPE_CHECKING from collections import defaultdict from . import pq @@ -55,7 +55,7 @@ class Transformer(AdaptContext): _oid_dumpers _oid_types _row_dumpers _row_loaders """.split() - types: Tuple[int, ...] | None + types: tuple[int, ...] | None formats: list[pq.Format] | None _adapters: "AdaptersMap" @@ -81,11 +81,11 @@ class Transformer(AdaptContext): # mapping fmt, oid -> Dumper instance # Not often used, so create it only if needed. - self._oid_dumpers: Tuple[OidDumperCache, OidDumperCache] | None + self._oid_dumpers: tuple[OidDumperCache, OidDumperCache] | None self._oid_dumpers = None # mapping fmt, oid -> Loader instance - self._loaders: Tuple[LoaderCache, LoaderCache] = ({}, {}) + self._loaders: tuple[LoaderCache, LoaderCache] = ({}, {}) self._row_dumpers: list[abc.Dumper] | None = None @@ -333,7 +333,7 @@ class Transformer(AdaptContext): return make_row(record) - def load_sequence(self, record: Sequence[Buffer | None]) -> Tuple[Any, ...]: + def load_sequence(self, record: Sequence[Buffer | None]) -> tuple[Any, ...]: if len(self._row_loaders) != len(record): raise e.ProgrammingError( f"cannot load sequence of {len(record)} items:" diff --git a/psycopg/psycopg/_queries.py b/psycopg/psycopg/_queries.py index 5cbdf00a7..517ed7af9 100644 --- a/psycopg/psycopg/_queries.py +++ b/psycopg/psycopg/_queries.py @@ -8,7 +8,7 @@ from __future__ import annotations import re from typing import Any, Callable, Mapping, Match, NamedTuple -from typing import Sequence, Tuple, TYPE_CHECKING +from typing import Sequence, TYPE_CHECKING from functools import lru_cache from . import pq @@ -47,7 +47,7 @@ class PostgresQuery: self.params: Sequence[Buffer | None] | None = None # these are tuples so they can be used as keys e.g. in prepared stmts - self.types: Tuple[int, ...] = () + self.types: tuple[int, ...] = () # The format requested by the user and the ones to really pass Postgres self._want_formats: list[PyFormat] | None = None @@ -169,13 +169,13 @@ class PostgresQuery: # The type of the _query2pg() and _query2pg_nocache() methods _Query2Pg: TypeAlias = Callable[ - [bytes, str], Tuple[bytes, list[PyFormat], list[str] | None, list[QueryPart]] + [bytes, str], tuple[bytes, list[PyFormat], list[str] | None, list[QueryPart]] ] def _query2pg_nocache( query: bytes, encoding: str -) -> Tuple[bytes, list[PyFormat], list[str] | None, list[QueryPart]]: +) -> tuple[bytes, list[PyFormat], list[str] | None, list[QueryPart]]: """ Convert Python query and params into something Postgres understands. @@ -199,7 +199,7 @@ def _query2pg_nocache( formats.append(part.format) elif isinstance(parts[0].item, str): - seen: dict[str, Tuple[bytes, PyFormat]] = {} + seen: dict[str, tuple[bytes, PyFormat]] = {} order = [] for part in parts[:-1]: assert isinstance(part.item, str) @@ -285,13 +285,13 @@ class PostgresClientQuery(PostgresQuery): _Query2PgClient: TypeAlias = Callable[ - [bytes, str], Tuple[bytes, list[str] | None, list[QueryPart]] + [bytes, str], tuple[bytes, list[str] | None, list[QueryPart]] ] def _query2pg_client_nocache( query: bytes, encoding: str -) -> Tuple[bytes, list[str] | None, list[QueryPart]]: +) -> tuple[bytes, list[str] | None, list[QueryPart]]: """ Convert Python query and params into a template to perform client-side binding """ @@ -306,7 +306,7 @@ def _query2pg_client_nocache( chunks.append(b"%s") elif isinstance(parts[0].item, str): - seen: dict[str, Tuple[bytes, PyFormat]] = {} + seen: dict[str, tuple[bytes, PyFormat]] = {} order = [] for part in parts[:-1]: assert isinstance(part.item, str) @@ -347,7 +347,7 @@ _re_placeholder = re.compile( def _split_query( query: bytes, encoding: str = "ascii", collapse_double_percent: bool = True ) -> list[QueryPart]: - parts: list[Tuple[bytes, Match[bytes] | None]] = [] + parts: list[tuple[bytes, Match[bytes] | None]] = [] cur = 0 # pairs [(fragment, match], with the last match None diff --git a/psycopg/psycopg/_struct.py b/psycopg/psycopg/_struct.py index 6d8da5a12..a09f8b535 100644 --- a/psycopg/psycopg/_struct.py +++ b/psycopg/psycopg/_struct.py @@ -7,20 +7,20 @@ Utility functions to deal with binary structs. from __future__ import annotations import struct -from typing import Callable, cast, Protocol, Tuple +from typing import Callable, cast, Protocol from . import errors as e from .abc import Buffer from ._compat import TypeAlias PackInt: TypeAlias = Callable[[int], bytes] -UnpackInt: TypeAlias = Callable[[Buffer], Tuple[int]] +UnpackInt: TypeAlias = Callable[[Buffer], tuple[int]] PackFloat: TypeAlias = Callable[[float], bytes] -UnpackFloat: TypeAlias = Callable[[Buffer], Tuple[float]] +UnpackFloat: TypeAlias = Callable[[Buffer], tuple[float]] class UnpackLen(Protocol): - def __call__(self, data: Buffer, start: int | None) -> Tuple[int]: ... + def __call__(self, data: Buffer, start: int | None) -> tuple[int]: ... pack_int2 = cast(PackInt, struct.Struct("!h").pack) diff --git a/psycopg/psycopg/abc.py b/psycopg/psycopg/abc.py index 90202f891..6eef15081 100644 --- a/psycopg/psycopg/abc.py +++ b/psycopg/psycopg/abc.py @@ -7,7 +7,7 @@ Protocol objects representing different implementations of the same classes. from __future__ import annotations from typing import Any, Callable, Generator, Mapping -from typing import Protocol, Sequence, Tuple, TYPE_CHECKING +from typing import Protocol, Sequence, TYPE_CHECKING from . import pq from ._enums import PyFormat as PyFormat @@ -30,7 +30,7 @@ Query: TypeAlias = LiteralString | bytes | "sql.SQL" | "sql.Composed" Params: TypeAlias = Sequence[Any] | Mapping[str, Any] ConnectionType = TypeVar("ConnectionType", bound="BaseConnection[Any]") PipelineCommand: TypeAlias = Callable[[], None] -DumperKey: TypeAlias = type | Tuple["DumperKey", ...] +DumperKey: TypeAlias = type | tuple["DumperKey", ...] ConnParam: TypeAlias = str | int | None ConnDict: TypeAlias = dict[str, ConnParam] ConnMapping: TypeAlias = Mapping[str, ConnParam] @@ -40,7 +40,7 @@ ConnMapping: TypeAlias = Mapping[str, ConnParam] RV = TypeVar("RV") -PQGenConn: TypeAlias = Generator[Tuple[int, "Wait"], "Ready" | int, RV] +PQGenConn: TypeAlias = Generator[tuple[int, Wait], Ready | int, RV] """Generator for processes where the connection file number can change. This can happen in connection and reset, but not in normal querying. @@ -201,7 +201,7 @@ class Loader(Protocol): class Transformer(Protocol): - types: Tuple[int, ...] | None + types: tuple[int, ...] | None formats: list[pq.Format] | None def __init__(self, context: AdaptContext | None = None): ... @@ -247,6 +247,6 @@ class Transformer(Protocol): def load_row(self, row: int, make_row: "RowMaker[Row]") -> "Row" | None: ... - def load_sequence(self, record: Sequence[Buffer | None]) -> Tuple[Any, ...]: ... + def load_sequence(self, record: Sequence[Buffer | None]) -> tuple[Any, ...]: ... def get_loader(self, oid: int, format: pq.Format) -> Loader: ... diff --git a/psycopg/psycopg/client_cursor.py b/psycopg/psycopg/client_cursor.py index 48630758b..5a64e5fe7 100644 --- a/psycopg/psycopg/client_cursor.py +++ b/psycopg/psycopg/client_cursor.py @@ -6,7 +6,7 @@ psycopg client-side binding cursors from __future__ import annotations -from typing import Tuple, TYPE_CHECKING +from typing import TYPE_CHECKING from functools import partial from ._queries import PostgresQuery, PostgresClientQuery @@ -79,7 +79,7 @@ class ClientCursorMixin(BaseCursor[ConnectionType, Row]): def _get_prepared( self, pgq: PostgresQuery, prepare: bool | None = None - ) -> Tuple[Prepare, bytes]: + ) -> tuple[Prepare, bytes]: return (Prepare.NO, b"") diff --git a/psycopg/psycopg/errors.py b/psycopg/psycopg/errors.py index e0a82dfda..5d6472bba 100644 --- a/psycopg/psycopg/errors.py +++ b/psycopg/psycopg/errors.py @@ -21,7 +21,7 @@ DBAPI-defined Exceptions are defined in the following hierarchy:: from __future__ import annotations from dataclasses import dataclass, field, fields -from typing import Any, Callable, NoReturn, Sequence, Tuple, TYPE_CHECKING +from typing import Any, Callable, NoReturn, Sequence, TYPE_CHECKING from asyncio import CancelledError from .pq.abc import PGconn, PGresult @@ -298,7 +298,7 @@ class Error(Exception): """ return Diagnostic(self._info, encoding=self._encoding) - def __reduce__(self) -> str | Tuple[Any, ...]: + def __reduce__(self) -> str | tuple[Any, ...]: res = super().__reduce__() if isinstance(res, tuple) and len(res) >= 3: # To make the exception picklable @@ -513,7 +513,7 @@ class Diagnostic: return None - def __reduce__(self) -> str | Tuple[Any, ...]: + def __reduce__(self) -> str | tuple[Any, ...]: res = super().__reduce__() if isinstance(res, tuple) and len(res) >= 3: res[2]["_info"] = _info_to_dict(self._info) diff --git a/psycopg/psycopg/pq/_pq_ctypes.py b/psycopg/psycopg/pq/_pq_ctypes.py index 27a6d1932..f157e1ce0 100644 --- a/psycopg/psycopg/pq/_pq_ctypes.py +++ b/psycopg/psycopg/pq/_pq_ctypes.py @@ -11,7 +11,7 @@ import ctypes import ctypes.util from ctypes import Structure, CFUNCTYPE, POINTER from ctypes import c_char, c_char_p, c_int, c_size_t, c_ubyte, c_uint, c_void_p -from typing import Any, NoReturn, Tuple +from typing import Any, NoReturn from .misc import find_libpq_full_path, version_pretty from ..errors import NotSupportedError @@ -58,11 +58,11 @@ Oid = c_uint class PGconn_struct(Structure): - _fields_: list[Tuple[str, type]] = [] + _fields_: list[tuple[str, type]] = [] class PGresult_struct(Structure): - _fields_: list[Tuple[str, type]] = [] + _fields_: list[tuple[str, type]] = [] class PQconninfoOption_struct(Structure): @@ -86,11 +86,11 @@ class PGnotify_struct(Structure): class PGcancelConn_struct(Structure): - _fields_: list[Tuple[str, type]] = [] + _fields_: list[tuple[str, type]] = [] class PGcancel_struct(Structure): - _fields_: list[Tuple[str, type]] = [] + _fields_: list[tuple[str, type]] = [] class PGresAttDesc_struct(Structure): diff --git a/psycopg/psycopg/pq/abc.py b/psycopg/psycopg/pq/abc.py index 257c56a8f..876a3f408 100644 --- a/psycopg/psycopg/pq/abc.py +++ b/psycopg/psycopg/pq/abc.py @@ -6,7 +6,7 @@ Protocol objects to represent objects exposed by different pq implementations. from __future__ import annotations -from typing import Any, Callable, Protocol, Sequence, Tuple, TYPE_CHECKING +from typing import Any, Callable, Protocol, Sequence, TYPE_CHECKING from ._enums import Format, Trace from .._compat import TypeAlias @@ -193,7 +193,7 @@ class PGconn(Protocol): def put_copy_end(self, error: bytes | None = None) -> int: ... - def get_copy_data(self, async_: int) -> Tuple[int, memoryview]: ... + def get_copy_data(self, async_: int) -> tuple[int, memoryview]: ... def trace(self, fileno: int) -> None: ... diff --git a/psycopg/psycopg/pq/pq_ctypes.py b/psycopg/psycopg/pq/pq_ctypes.py index 7f184fbb8..a4b5c6828 100644 --- a/psycopg/psycopg/pq/pq_ctypes.py +++ b/psycopg/psycopg/pq/pq_ctypes.py @@ -17,7 +17,7 @@ from weakref import ref from ctypes import Array, POINTER, cast, string_at, create_string_buffer, byref from ctypes import addressof, c_char_p, c_int, c_size_t, c_ulong, c_void_p, py_object -from typing import Any, Callable, Sequence, Tuple +from typing import Any, Callable, Sequence from typing import cast as t_cast, TYPE_CHECKING from .. import errors as e @@ -644,7 +644,7 @@ class PGconn: raise e.OperationalError(f"sending copy end failed: {error_message(self)}") return rv - def get_copy_data(self, async_: int) -> Tuple[int, memoryview]: + def get_copy_data(self, async_: int) -> tuple[int, memoryview]: buffer_ptr = c_char_p() nbytes = impl.PQgetCopyData(self._pgconn_ptr, byref(buffer_ptr), async_) if nbytes == -2: diff --git a/psycopg/psycopg/rows.py b/psycopg/psycopg/rows.py index bf5f5fe98..790e9c624 100644 --- a/psycopg/psycopg/rows.py +++ b/psycopg/psycopg/rows.py @@ -8,7 +8,7 @@ from __future__ import annotations import functools from typing import Any, Callable, NamedTuple, NoReturn -from typing import TYPE_CHECKING, Protocol, Sequence, Tuple +from typing import TYPE_CHECKING, Protocol, Sequence from collections import namedtuple from . import pq @@ -81,7 +81,7 @@ 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). """ diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index feb1254b9..7ff09d4f2 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -9,7 +9,7 @@ from __future__ import annotations import re import struct from math import prod -from typing import Any, cast, Callable, Pattern, Set, Tuple +from typing import Any, cast, Callable, Pattern, Set from .. import pq from .. import errors as e @@ -24,10 +24,10 @@ from .._typeinfo import TypeInfo _struct_head = struct.Struct("!III") # ndims, hasnull, elem oid _pack_head = cast(Callable[[int, int, int], bytes], _struct_head.pack) -_unpack_head = cast(Callable[[Buffer], Tuple[int, int, int]], _struct_head.unpack_from) +_unpack_head = cast(Callable[[Buffer], tuple[int, int, int]], _struct_head.unpack_from) _struct_dim = struct.Struct("!II") # dim, lower bound _pack_dim = cast(Callable[[int, int], bytes], _struct_dim.pack) -_unpack_dim = cast(Callable[[Buffer, int], Tuple[int, int]], _struct_dim.unpack_from) +_unpack_dim = cast(Callable[[Buffer, int], tuple[int, int]], _struct_dim.unpack_from) PY_TEXT = PyFormat.TEXT PQ_BINARY = pq.Format.BINARY diff --git a/psycopg/psycopg/types/composite.py b/psycopg/psycopg/types/composite.py index 0f0fca0ef..91cb0e400 100644 --- a/psycopg/psycopg/types/composite.py +++ b/psycopg/psycopg/types/composite.py @@ -10,7 +10,7 @@ import re import struct from collections import namedtuple from typing import Any, Callable, cast, Iterator -from typing import NamedTuple, Sequence, Tuple, TYPE_CHECKING +from typing import NamedTuple, Sequence, TYPE_CHECKING from .. import pq from .. import abc @@ -29,7 +29,7 @@ if TYPE_CHECKING: _struct_oidlen = struct.Struct("!Ii") _pack_oidlen = cast(Callable[[int, int], bytes], _struct_oidlen.pack) _unpack_oidlen = cast( - Callable[[abc.Buffer, int], Tuple[int, int]], _struct_oidlen.unpack_from + Callable[[abc.Buffer, int], tuple[int, int]], _struct_oidlen.unpack_from ) @@ -121,7 +121,7 @@ class TupleDumper(SequenceDumper): # Should be this, but it doesn't work # oid = _oids.RECORD_OID - def dump(self, obj: Tuple[Any, ...]) -> Buffer | None: + def dump(self, obj: tuple[Any, ...]) -> Buffer | None: return self._dump_sequence(obj, b"(", b")", b",") @@ -129,7 +129,7 @@ class TupleBinaryDumper(Dumper): format = pq.Format.BINARY # Subclasses must set this info - _field_types: Tuple[int, ...] + _field_types: tuple[int, ...] def __init__(self, cls: type, context: abc.AdaptContext | None = None): super().__init__(cls, context) @@ -144,7 +144,7 @@ class TupleBinaryDumper(Dumper): nfields = len(self._field_types) self._formats = (PyFormat.from_pq(self.format),) * nfields - def dump(self, obj: Tuple[Any, ...]) -> Buffer | None: + def dump(self, obj: tuple[Any, ...]) -> Buffer | None: out = bytearray(pack_len(len(obj))) adapted = self._tx.dump_sequence(obj, self._formats) for i in range(len(obj)): @@ -196,7 +196,7 @@ class BaseCompositeLoader(Loader): class RecordLoader(BaseCompositeLoader): - def load(self, data: abc.Buffer) -> Tuple[Any, ...]: + def load(self, data: abc.Buffer) -> tuple[Any, ...]: if data == b"()": return () @@ -217,9 +217,9 @@ class RecordBinaryLoader(Loader): # Usually there will be only one, but if there is more than one # row in the same query (in different columns, or even in different # records), oids might differ and we'd need separate transformers. - self._txs: dict[Tuple[int, ...], abc.Transformer] = {} + self._txs: dict[tuple[int, ...], abc.Transformer] = {} - def load(self, data: abc.Buffer) -> Tuple[Any, ...]: + def load(self, data: abc.Buffer) -> tuple[Any, ...]: nfields = unpack_len(data, 0)[0] offset = 4 oids = [] @@ -346,13 +346,13 @@ def _nt_from_info(info: CompositeInfo) -> type[NamedTuple]: @cache -def _make_nt(name: str, fields: Tuple[str, ...]) -> type[NamedTuple]: +def _make_nt(name: str, fields: tuple[str, ...]) -> type[NamedTuple]: return namedtuple(name, fields) # type: ignore[return-value] @cache def _make_loader( - name: str, types: Tuple[int, ...], factory: Callable[..., Any] + name: str, types: tuple[int, ...], factory: Callable[..., Any] ) -> type[BaseCompositeLoader]: return type( f"{name.title()}Loader", @@ -377,7 +377,7 @@ def _make_dumper(name: str, oid: int) -> type[TupleDumper]: @cache def _make_binary_dumper( - name: str, oid: int, field_types: Tuple[int, ...] + name: str, oid: int, field_types: tuple[int, ...] ) -> type[TupleBinaryDumper]: return type( f"{name.title()}BinaryDumper", diff --git a/psycopg/psycopg/types/datetime.py b/psycopg/psycopg/types/datetime.py index 55f8e9259..08bdf6d81 100644 --- a/psycopg/psycopg/types/datetime.py +++ b/psycopg/psycopg/types/datetime.py @@ -9,7 +9,7 @@ from __future__ import annotations import re import struct from datetime import date, datetime, time, timedelta, timezone -from typing import Any, Callable, cast, Tuple, TYPE_CHECKING +from typing import Any, Callable, cast, TYPE_CHECKING from .. import _oids from ..pq import Format @@ -24,12 +24,12 @@ if TYPE_CHECKING: _struct_timetz = struct.Struct("!qi") # microseconds, sec tz offset _pack_timetz = cast(Callable[[int, int], bytes], _struct_timetz.pack) -_unpack_timetz = cast(Callable[[Buffer], Tuple[int, int]], _struct_timetz.unpack) +_unpack_timetz = cast(Callable[[Buffer], tuple[int, int]], _struct_timetz.unpack) _struct_interval = struct.Struct("!qii") # microseconds, days, months _pack_interval = cast(Callable[[int, int, int], bytes], _struct_interval.pack) _unpack_interval = cast( - Callable[[Buffer], Tuple[int, int, int]], _struct_interval.unpack + Callable[[Buffer], tuple[int, int, int]], _struct_interval.unpack ) utc = timezone.utc diff --git a/psycopg/psycopg/types/enum.py b/psycopg/psycopg/types/enum.py index 1c4516e9b..3f1f44ac9 100644 --- a/psycopg/psycopg/types/enum.py +++ b/psycopg/psycopg/types/enum.py @@ -5,7 +5,7 @@ Adapters for the enum type. from __future__ import annotations from enum import Enum -from typing import Any, Generic, Mapping, Sequence, Tuple, cast, TYPE_CHECKING +from typing import Any, Generic, Mapping, Sequence, cast, TYPE_CHECKING from .. import sql from .. import postgres @@ -24,11 +24,11 @@ E = TypeVar("E", bound=Enum) EnumDumpMap: TypeAlias = dict[E, bytes] EnumLoadMap: TypeAlias = dict[bytes, E] -EnumMapping: TypeAlias = Mapping[E, str] | Sequence[Tuple[E, str]] | None +EnumMapping: TypeAlias = Mapping[E, str] | Sequence[tuple[E, str]] | None # Hashable versions -_HEnumDumpMap: TypeAlias = Tuple[Tuple[E, bytes], ...] -_HEnumLoadMap: TypeAlias = Tuple[Tuple[bytes, E], ...] +_HEnumDumpMap: TypeAlias = tuple[tuple[E, bytes], ...] +_HEnumLoadMap: TypeAlias = tuple[tuple[bytes, E], ...] TEXT = Format.TEXT BINARY = Format.BINARY @@ -170,7 +170,7 @@ def register_enum( @cache -def _make_enum(name: str, labels: Tuple[str, ...]) -> Enum: +def _make_enum(name: str, labels: tuple[str, ...]) -> Enum: return Enum(name.title(), labels, module=__name__) diff --git a/psycopg/psycopg/types/json.py b/psycopg/psycopg/types/json.py index 18a2fd58f..47e85fff6 100644 --- a/psycopg/psycopg/types/json.py +++ b/psycopg/psycopg/types/json.py @@ -7,7 +7,7 @@ Adapters for JSON types. from __future__ import annotations import json -from typing import Any, Callable, Tuple +from typing import Any, Callable from .. import abc from .. import _oids @@ -230,7 +230,7 @@ def _get_current_dumper( return _default_dumpers[cls, format] -_default_dumpers: dict[Tuple[type[_JsonWrapper], PyFormat], type[Dumper]] = { +_default_dumpers: dict[tuple[type[_JsonWrapper], PyFormat], type[Dumper]] = { (Json, PyFormat.BINARY): JsonBinaryDumper, (Json, PyFormat.TEXT): JsonDumper, (Jsonb, PyFormat.BINARY): JsonbBinaryDumper, diff --git a/psycopg/psycopg/types/numeric.py b/psycopg/psycopg/types/numeric.py index 35c3a051b..65bd10989 100644 --- a/psycopg/psycopg/types/numeric.py +++ b/psycopg/psycopg/types/numeric.py @@ -10,7 +10,7 @@ import sys import struct from abc import ABC, abstractmethod from math import log -from typing import Any, Callable, DefaultDict, Tuple, cast, TYPE_CHECKING +from typing import Any, Callable, DefaultDict, cast, TYPE_CHECKING from decimal import Decimal, DefaultContext, Context from .. import _oids @@ -315,7 +315,7 @@ for i in range(DefaultContext.prec): _contexts[i] = DefaultContext _unpack_numeric_head = cast( - Callable[[Buffer], Tuple[int, int, int, int]], + Callable[[Buffer], tuple[int, int, int, int]], struct.Struct("!HhHH").unpack_from, ) _pack_numeric_head = cast( @@ -370,7 +370,7 @@ class _MixedNumericDumper(Dumper, ABC): oid = _oids.NUMERIC_OID # If numpy is available, the dumped object might be a numpy integer too - int_classes: type | Tuple[type, ...] = () + int_classes: type | tuple[type, ...] = () def __init__(self, cls: type, context: AdaptContext | None = None): super().__init__(cls, context) diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index 051112fa4..0fa3926fe 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -7,7 +7,7 @@ Support for range types adaptation. from __future__ import annotations import re -from typing import Any, Generic, Tuple, cast, TYPE_CHECKING +from typing import Any, Generic, cast, TYPE_CHECKING from decimal import Decimal from datetime import date, datetime @@ -470,7 +470,7 @@ class RangeLoader(BaseRangeLoader[T]): return load_range_text(data, self._load)[0] -def load_range_text(data: Buffer, load: LoadFunc) -> Tuple[Range[Any], int]: +def load_range_text(data: Buffer, load: LoadFunc) -> tuple[Range[Any], int]: if data == b"empty": return Range(empty=True), 5 diff --git a/psycopg_c/psycopg_c/_psycopg.pyi b/psycopg_c/psycopg_c/_psycopg.pyi index 1961b9161..1c661ea99 100644 --- a/psycopg_c/psycopg_c/_psycopg.pyi +++ b/psycopg_c/psycopg_c/_psycopg.pyi @@ -9,7 +9,7 @@ information. Will submit a bug. from __future__ import annotations -from typing import Any, Sequence, Tuple +from typing import Any, Sequence from psycopg import pq, abc, BaseConnection from psycopg.rows import Row, RowMaker @@ -18,7 +18,7 @@ from psycopg.pq.abc import PGcancelConn, PGconn, PGresult from psycopg._compat import Deque class Transformer(abc.AdaptContext): - types: Tuple[int, ...] | None + types: tuple[int, ...] | None formats: list[pq.Format] | None def __init__(self, context: abc.AdaptContext | None = None): ... @classmethod @@ -47,7 +47,7 @@ class Transformer(abc.AdaptContext): def get_dumper(self, obj: Any, format: PyFormat) -> abc.Dumper: ... def load_rows(self, row0: int, row1: int, make_row: RowMaker[Row]) -> list[Row]: ... def load_row(self, row: int, make_row: RowMaker[Row]) -> Row | None: ... - def load_sequence(self, record: Sequence[abc.Buffer | None]) -> Tuple[Any, ...]: ... + def load_sequence(self, record: Sequence[abc.Buffer | None]) -> tuple[Any, ...]: ... def get_loader(self, oid: int, format: pq.Format) -> abc.Loader: ... # Generators @@ -73,8 +73,8 @@ def format_row_text( def format_row_binary( row: Sequence[Any], tx: abc.Transformer, out: bytearray | None = None ) -> bytearray: ... -def parse_row_text(data: abc.Buffer, tx: abc.Transformer) -> Tuple[Any, ...]: ... -def parse_row_binary(data: abc.Buffer, tx: abc.Transformer) -> Tuple[Any, ...]: ... +def parse_row_text(data: abc.Buffer, tx: abc.Transformer) -> tuple[Any, ...]: ... +def parse_row_binary(data: abc.Buffer, tx: abc.Transformer) -> tuple[Any, ...]: ... # Arrays optimization def array_load_text( diff --git a/psycopg_c/psycopg_c/_psycopg/copy.pyx b/psycopg_c/psycopg_c/_psycopg/copy.pyx index 04af0856c..dd34be1a9 100644 --- a/psycopg_c/psycopg_c/_psycopg/copy.pyx +++ b/psycopg_c/psycopg_c/_psycopg/copy.pyx @@ -208,7 +208,7 @@ cdef int _append_text_none(bytearray out, Py_ssize_t *pos, int with_tab) except return 0 -def parse_row_binary(data, tx: Transformer) -> Tuple[Any, ...]: +def parse_row_binary(data, tx: Transformer) -> tuple[Any, ...]: cdef unsigned char *ptr cdef Py_ssize_t bufsize _buffer_as_string_and_size(data, &ptr, &bufsize) @@ -243,7 +243,7 @@ def parse_row_binary(data, tx: Transformer) -> Tuple[Any, ...]: return tx.load_sequence(row) -def parse_row_text(data, tx: Transformer) -> Tuple[Any, ...]: +def parse_row_text(data, tx: Transformer) -> tuple[Any, ...]: cdef unsigned char *fstart cdef Py_ssize_t size _buffer_as_string_and_size(data, &fstart, &size) diff --git a/psycopg_c/psycopg_c/_psycopg/transform.pyx b/psycopg_c/psycopg_c/_psycopg/transform.pyx index 4e2ccdf0b..b2f252f2c 100644 --- a/psycopg_c/psycopg_c/_psycopg/transform.pyx +++ b/psycopg_c/psycopg_c/_psycopg/transform.pyx @@ -19,7 +19,7 @@ from cpython.bytes cimport PyBytes_AS_STRING from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM from cpython.object cimport PyObject, PyObject_CallFunctionObjArgs -from typing import Any, Iterable, Sequence, Tuple +from typing import Any, Iterable, Sequence from psycopg import errors as e from psycopg.pq import Format as PqFormat diff --git a/psycopg_c/psycopg_c/pq/pgconn.pyx b/psycopg_c/psycopg_c/pq/pgconn.pyx index b66804735..d01148528 100644 --- a/psycopg_c/psycopg_c/pq/pgconn.pyx +++ b/psycopg_c/psycopg_c/pq/pgconn.pyx @@ -549,7 +549,7 @@ cdef class PGconn: raise e.OperationalError(f"sending copy end failed: {error_message(self)}") return rv - def get_copy_data(self, int async_) -> Tuple[int, memoryview]: + def get_copy_data(self, int async_) -> tuple[int, memoryview]: cdef char *buffer_ptr = NULL cdef int nbytes nbytes = libpq.PQgetCopyData(self._pgconn_ptr, &buffer_ptr, async_) diff --git a/psycopg_pool/psycopg_pool/base.py b/psycopg_pool/psycopg_pool/base.py index 748d1362a..2937698c1 100644 --- a/psycopg_pool/psycopg_pool/base.py +++ b/psycopg_pool/psycopg_pool/base.py @@ -8,7 +8,7 @@ from __future__ import annotations from time import monotonic from random import random -from typing import Any, Tuple, TYPE_CHECKING +from typing import Any, TYPE_CHECKING from psycopg import errors as e @@ -118,7 +118,7 @@ class BasePool: """`!True` if the pool is closed.""" return self._closed - def _check_size(self, min_size: int, max_size: int | None) -> Tuple[int, int]: + def _check_size(self, min_size: int, max_size: int | None) -> tuple[int, int]: if max_size is None: max_size = min_size diff --git a/tests/fix_faker.py b/tests/fix_faker.py index 9da58903f..e09a59f9b 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -6,7 +6,7 @@ import ipaddress from math import isnan from uuid import UUID from random import choice, random, randrange -from typing import Any, Set, Tuple +from typing import Any, Set from decimal import Decimal from contextlib import contextmanager, asynccontextmanager @@ -199,7 +199,7 @@ class Faker: ) def choose_schema(self, ncols=20): - schema: list[Tuple[type, ...] | type] = [] + schema: list[tuple[type, ...] | type] = [] while len(schema) < ncols: s = self.make_schema(choice(self.types)) if s is not None: @@ -245,7 +245,7 @@ class Faker: return rv - def make_schema(self, cls: type) -> Tuple[type, ...] | type | None: + def make_schema(self, cls: type) -> tuple[type, ...] | type | None: """Create a schema spec from a Python type. A schema specifies what Postgres type to generate when a Python type diff --git a/tests/fix_gc.py b/tests/fix_gc.py index ead6c6b21..6538e232d 100644 --- a/tests/fix_gc.py +++ b/tests/fix_gc.py @@ -1,6 +1,7 @@ +from __future__ import annotations + import gc import sys -from typing import Tuple import pytest @@ -18,7 +19,7 @@ def pytest_configure(config): ) -NO_COUNT_TYPES: Tuple[type, ...] = () +NO_COUNT_TYPES: tuple[type, ...] = () if sys.version_info[:2] == (3, 10): # On my laptop there are occasional creations of a single one of these objects diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index b22ea435e..4a9675749 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -6,7 +6,7 @@ from __future__ import annotations import logging import weakref from time import time -from typing import Any, Tuple +from typing import Any import pytest @@ -435,7 +435,7 @@ def test_grow(dsn, monkeypatch, min_size, want_times): with pool.ConnectionPool(dsn, min_size=min_size, max_size=4, num_workers=3) as p: p.wait(1.0) - results: list[Tuple[int, float]] = [] + results: list[tuple[int, float]] = [] ts = [spawn(worker, args=(i,)) for i in range(len(want_times))] gather(*ts) @@ -449,7 +449,7 @@ def test_grow(dsn, monkeypatch, min_size, want_times): def test_shrink(dsn, monkeypatch): from psycopg_pool.pool import ShrinkPool - results: list[Tuple[int, int]] = [] + results: list[tuple[int, int]] = [] def run_hacked(self, pool): n0 = pool._nconns diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index b18c3e426..8f522a6f4 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging import weakref from time import time -from typing import Any, Tuple +from typing import Any import pytest @@ -438,7 +438,7 @@ async def test_grow(dsn, monkeypatch, min_size, want_times): dsn, min_size=min_size, max_size=4, num_workers=3 ) as p: await p.wait(1.0) - results: list[Tuple[int, float]] = [] + results: list[tuple[int, float]] = [] ts = [spawn(worker, args=(i,)) for i in range(len(want_times))] await gather(*ts) @@ -452,7 +452,7 @@ async def test_grow(dsn, monkeypatch, min_size, want_times): async def test_shrink(dsn, monkeypatch): from psycopg_pool.pool_async import ShrinkPool - results: list[Tuple[int, int]] = [] + results: list[tuple[int, int]] = [] async def run_hacked(self, pool): n0 = pool._nconns diff --git a/tests/pool/test_pool_common.py b/tests/pool/test_pool_common.py index fb994e024..60ef84b89 100644 --- a/tests/pool/test_pool_common.py +++ b/tests/pool/test_pool_common.py @@ -5,7 +5,7 @@ from __future__ import annotations import logging from time import time -from typing import Any, Tuple +from typing import Any import pytest @@ -171,7 +171,7 @@ def test_queue(pool_cls, dsn): t1 = time() results.append((n, t1 - t0, pid)) - results: list[Tuple[int, float, int]] = [] + results: list[tuple[int, float, int]] = [] with pool_cls(dsn, min_size=min_size(pool_cls, 2), max_size=2) as p: p.wait() ts = [spawn(worker, args=(i,)) for i in range(6)] @@ -237,8 +237,8 @@ def test_queue_timeout(pool_cls, dsn): t1 = time() results.append((n, t1 - t0, pid)) - results: list[Tuple[int, float, int]] = [] - errors: list[Tuple[int, float, Exception]] = [] + results: list[tuple[int, float, int]] = [] + errors: list[tuple[int, float, Exception]] = [] with pool_cls(dsn, min_size=min_size(pool_cls, 2), max_size=2, timeout=0.1) as p: ts = [spawn(worker, args=(i,)) for i in range(4)] @@ -296,8 +296,8 @@ def test_queue_timeout_override(pool_cls, dsn): t1 = time() results.append((n, t1 - t0, pid)) - results: list[Tuple[int, float, int]] = [] - errors: list[Tuple[int, float, Exception]] = [] + results: list[tuple[int, float, int]] = [] + errors: list[tuple[int, float, Exception]] = [] with pool_cls(dsn, min_size=min_size(pool_cls, 2), max_size=2, timeout=0.1) as p: ts = [spawn(worker, args=(i,)) for i in range(4)] diff --git a/tests/pool/test_pool_common_async.py b/tests/pool/test_pool_common_async.py index 141e3e6d7..01cc957a9 100644 --- a/tests/pool/test_pool_common_async.py +++ b/tests/pool/test_pool_common_async.py @@ -2,7 +2,7 @@ from __future__ import annotations import logging from time import time -from typing import Any, Tuple +from typing import Any import pytest @@ -181,7 +181,7 @@ async def test_queue(pool_cls, dsn): t1 = time() results.append((n, t1 - t0, pid)) - results: list[Tuple[int, float, int]] = [] + results: list[tuple[int, float, int]] = [] async with pool_cls(dsn, min_size=min_size(pool_cls, 2), max_size=2) as p: await p.wait() ts = [spawn(worker, args=(i,)) for i in range(6)] @@ -247,8 +247,8 @@ async def test_queue_timeout(pool_cls, dsn): t1 = time() results.append((n, t1 - t0, pid)) - results: list[Tuple[int, float, int]] = [] - errors: list[Tuple[int, float, Exception]] = [] + results: list[tuple[int, float, int]] = [] + errors: list[tuple[int, float, Exception]] = [] async with pool_cls( dsn, min_size=min_size(pool_cls, 2), max_size=2, timeout=0.1 @@ -306,8 +306,8 @@ async def test_queue_timeout_override(pool_cls, dsn): t1 = time() results.append((n, t1 - t0, pid)) - results: list[Tuple[int, float, int]] = [] - errors: list[Tuple[int, float, Exception]] = [] + results: list[tuple[int, float, int]] = [] + errors: list[tuple[int, float, Exception]] = [] async with pool_cls( dsn, min_size=min_size(pool_cls, 2), max_size=2, timeout=0.1 diff --git a/tests/scripts/pipeline-demo.py b/tests/scripts/pipeline-demo.py index ff05f0b01..1d44e4070 100644 --- a/tests/scripts/pipeline-demo.py +++ b/tests/scripts/pipeline-demo.py @@ -15,7 +15,7 @@ import asyncio import logging from contextlib import contextmanager from functools import partial -from typing import Any, Iterator, Sequence, Tuple +from typing import Any, Iterator, Sequence from psycopg import AsyncConnection, Connection from psycopg import pq, waiting @@ -111,7 +111,7 @@ class LoggingPGconn: @contextmanager def prepare_pipeline_demo_pq( pgconn: LoggingPGconn, rows_to_send: int, logger: logging.Logger -) -> Iterator[Tuple[Deque[PipelineCommand], Deque[str]]]: +) -> Iterator[tuple[Deque[PipelineCommand], Deque[str]]]: """Set up pipeline demo with initial queries and yield commands and results queue for pipeline_communicate(). """ diff --git a/tests/typing_example.py b/tests/typing_example.py index 4bea2c14d..12b51553f 100644 --- a/tests/typing_example.py +++ b/tests/typing_example.py @@ -3,7 +3,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Any, Callable, Sequence, Tuple +from typing import Any, Callable, Sequence from psycopg import Connection, Cursor, ServerCursor, connect, rows from psycopg import AsyncConnection, AsyncCursor, AsyncServerCursor @@ -113,8 +113,8 @@ def check_row_factory_connection() -> None: with conn2.cursor() as cur2: cur2.execute("select 2") - cur3: Cursor[Tuple[Any, ...]] - r3: Tuple[Any, ...] | None + cur3: Cursor[tuple[Any, ...]] + r3: tuple[Any, ...] | None conn3 = connect() cur3 = conn3.execute("select 3") with conn3.cursor() as cur3: @@ -147,8 +147,8 @@ async def async_check_row_factory_connection() -> None: async with conn2.cursor() as cur2: await cur2.execute("select 2") - cur3: AsyncCursor[Tuple[Any, ...]] - r3: Tuple[Any, ...] | None + cur3: AsyncCursor[tuple[Any, ...]] + r3: tuple[Any, ...] | None conn3 = await AsyncConnection.connect() cur3 = await conn3.execute("select 3") async with conn3.cursor() as cur3: @@ -159,7 +159,7 @@ async def async_check_row_factory_connection() -> None: def check_row_factories() -> None: conn1 = connect(row_factory=rows.tuple_row) - v1: Tuple[Any, ...] = conn1.execute("").fetchall()[0] + v1: tuple[Any, ...] = conn1.execute("").fetchall()[0] conn2 = connect(row_factory=rows.dict_row) v2: dict[str, Any] = conn2.execute("").fetchall()[0] diff --git a/tests/utils.py b/tests/utils.py index c2a97307a..443fba1c7 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -3,7 +3,7 @@ from __future__ import annotations import re import sys import operator -from typing import Callable, Tuple +from typing import Callable from contextlib import contextmanager @@ -73,7 +73,7 @@ class VersionCheck: *, skip: bool = False, op: str | None = None, - version_tuple: Tuple[int, ...] = (), + version_tuple: tuple[int, ...] = (), whose: str = "(wanted)", postgres_rule: bool = False, ): @@ -136,7 +136,7 @@ class VersionCheck: _OP_NAMES = {">=": "ge", "<=": "le", ">": "gt", "<": "lt", "==": "eq", "!=": "ne"} - def _match_version(self, got_tuple: Tuple[int, ...]) -> bool: + def _match_version(self, got_tuple: tuple[int, ...]) -> bool: if not self.version_tuple: return True @@ -145,11 +145,11 @@ class VersionCheck: assert len(version_tuple) <= 2 version_tuple = version_tuple[:1] + (0,) + version_tuple[1:] - op: Callable[[Tuple[int, ...], Tuple[int, ...]], bool] + op: Callable[[tuple[int, ...], tuple[int, ...]], bool] op = getattr(operator, self._OP_NAMES[self.op]) return op(got_tuple, version_tuple) - def _parse_int_version(self, version: int | None) -> Tuple[int, ...]: + def _parse_int_version(self, version: int | None) -> tuple[int, ...]: if version is None: return () version, ver_fix = divmod(version, 100)