From: Daniele Varrazzo Date: Wed, 3 Jan 2024 00:33:35 +0000 (+0100) Subject: refactor: use TypeVar from typing_extension X-Git-Tag: pool-3.2.1~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3e56e0a82f4409fff0405deb8b6677d067176d46;p=thirdparty%2Fpsycopg.git refactor: use TypeVar from typing_extension This includes the default parameter, which should fix the problem of the awkward definition of Row defaulting to TupleRow. --- diff --git a/psycopg/psycopg/_acompat.py b/psycopg/psycopg/_acompat.py index 4915cab91..cf106c5ba 100644 --- a/psycopg/psycopg/_acompat.py +++ b/psycopg/psycopg/_acompat.py @@ -13,10 +13,12 @@ from __future__ import annotations import queue import asyncio import threading -from typing import Any, Callable, Coroutine, TypeVar, TYPE_CHECKING +from typing import Any, Callable, Coroutine, TYPE_CHECKING from typing_extensions import TypeAlias +from ._compat import TypeVar + Worker: TypeAlias = threading.Thread AWorker: TypeAlias = "asyncio.Task[None]" T = TypeVar("T") diff --git a/psycopg/psycopg/_adapters_map.py b/psycopg/psycopg/_adapters_map.py index fae5cb545..904ec71d1 100644 --- a/psycopg/psycopg/_adapters_map.py +++ b/psycopg/psycopg/_adapters_map.py @@ -4,13 +4,14 @@ Mapping from types/oids to Dumpers/Loaders # Copyright (C) 2020 The Psycopg Team -from typing import Any, Dict, List, Optional, Type, TypeVar, Union +from typing import Any, Dict, List, Optional, Type, Union from typing import cast, TYPE_CHECKING from . import pq from . import errors as e from .abc import Dumper, Loader from ._enums import PyFormat as PyFormat +from ._compat import TypeVar from ._cmodule import _psycopg from ._typeinfo import TypesRegistry diff --git a/psycopg/psycopg/_compat.py b/psycopg/psycopg/_compat.py index c9e97e760..6ac16505d 100644 --- a/psycopg/psycopg/_compat.py +++ b/psycopg/psycopg/_compat.py @@ -27,11 +27,17 @@ if sys.version_info >= (3, 11): else: from typing_extensions import LiteralString +if sys.version_info >= (3, 13): + from typing import TypeVar +else: + from typing_extensions import TypeVar + __all__ = [ "Counter", "Deque", "LiteralString", "TypeGuard", + "TypeVar", "ZoneInfo", "cache", ] diff --git a/psycopg/psycopg/_connection_base.py b/psycopg/psycopg/_connection_base.py index fb8db5d91..77b9b797e 100644 --- a/psycopg/psycopg/_connection_base.py +++ b/psycopg/psycopg/_connection_base.py @@ -6,7 +6,7 @@ psycopg connection objects import logging from typing import Callable, Generic -from typing import List, NamedTuple, Optional, Type, TypeVar, Tuple, Union +from typing import List, NamedTuple, Optional, Type, Tuple, Union from typing import TYPE_CHECKING from weakref import ref, ReferenceType from warnings import warn @@ -23,7 +23,7 @@ from ._tpc import Xid from .rows import Row from .adapt import AdaptersMap from ._enums import IsolationLevel -from ._compat import LiteralString +from ._compat import LiteralString, TypeVar from .pq.misc import connection_summary from .conninfo import ConnectionInfo from ._pipeline import BasePipeline diff --git a/psycopg/psycopg/_typeinfo.py b/psycopg/psycopg/_typeinfo.py index 141ed0427..bfa740ff9 100644 --- a/psycopg/psycopg/_typeinfo.py +++ b/psycopg/psycopg/_typeinfo.py @@ -8,13 +8,14 @@ information to the adapters if needed. # Copyright (C) 2020 The Psycopg Team from typing import Any, Dict, Iterator, Optional, overload -from typing import Sequence, Tuple, Type, TypeVar, Union, TYPE_CHECKING +from typing import Sequence, Tuple, Type, Union, TYPE_CHECKING from typing_extensions import TypeAlias from . import sql from . import errors as e from .abc import AdaptContext, Query from .rows import dict_row +from ._compat import TypeVar from ._encodings import conn_encoding if TYPE_CHECKING: diff --git a/psycopg/psycopg/abc.py b/psycopg/psycopg/abc.py index 8edcdc107..0952e8d0b 100644 --- a/psycopg/psycopg/abc.py +++ b/psycopg/psycopg/abc.py @@ -5,13 +5,13 @@ Protocol objects representing different implementations of the same classes. # Copyright (C) 2020 The Psycopg Team from typing import Any, Callable, Generator, Mapping -from typing import List, Optional, Protocol, Sequence, Tuple, TypeVar, Union +from typing import List, Optional, Protocol, Sequence, Tuple, Union from typing import TYPE_CHECKING from typing_extensions import TypeAlias from . import pq from ._enums import PyFormat as PyFormat -from ._compat import LiteralString +from ._compat import LiteralString, TypeVar if TYPE_CHECKING: from . import sql diff --git a/psycopg/psycopg/pq/_debug.py b/psycopg/psycopg/pq/_debug.py index f86f3bdcb..50fc819e4 100644 --- a/psycopg/psycopg/pq/_debug.py +++ b/psycopg/psycopg/pq/_debug.py @@ -30,8 +30,9 @@ Suggested usage:: import inspect import logging -from typing import Any, Callable, Type, TypeVar, TYPE_CHECKING +from typing import Any, Callable, Type, TYPE_CHECKING from functools import wraps +from .._compat import TypeVar from . import PGconn from .misc import connection_summary diff --git a/psycopg/psycopg/rows.py b/psycopg/psycopg/rows.py index f9b78e5e2..d0d834864 100644 --- a/psycopg/psycopg/rows.py +++ b/psycopg/psycopg/rows.py @@ -6,12 +6,13 @@ psycopg row factories import functools from typing import Any, Callable, Dict, List, Optional, NamedTuple, NoReturn -from typing import TYPE_CHECKING, Protocol, Sequence, Tuple, Type, TypeVar +from typing import TYPE_CHECKING, Protocol, Sequence, Tuple, Type from collections import namedtuple from typing_extensions import TypeAlias from . import pq from . import errors as e +from ._compat import TypeVar from ._encodings import _as_python_identifier if TYPE_CHECKING: diff --git a/psycopg/psycopg/types/enum.py b/psycopg/psycopg/types/enum.py index 3035214ba..e15c11299 100644 --- a/psycopg/psycopg/types/enum.py +++ b/psycopg/psycopg/types/enum.py @@ -3,7 +3,7 @@ Adapters for the enum type. """ from enum import Enum from typing import Any, Dict, Generic, Optional, Mapping, Sequence -from typing import Tuple, Type, TypeVar, Union, cast, TYPE_CHECKING +from typing import Tuple, Type, Union, cast, TYPE_CHECKING from typing_extensions import TypeAlias from .. import sql @@ -12,7 +12,7 @@ from .. import errors as e from ..pq import Format from ..abc import AdaptContext, Query from ..adapt import Buffer, Dumper, Loader -from .._compat import cache +from .._compat import cache, TypeVar from .._encodings import conn_encoding from .._typeinfo import TypeInfo diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index 6290ca080..9d91f8910 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -5,7 +5,7 @@ Support for range types adaptation. # Copyright (C) 2020 The Psycopg Team import re -from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Type, Tuple +from typing import Any, Callable, Dict, Generic, List, Optional, Type, Tuple from typing import cast, TYPE_CHECKING from decimal import Decimal from datetime import date, datetime @@ -18,7 +18,7 @@ from ..pq import Format from ..abc import AdaptContext, Buffer, Dumper, DumperKey, Query from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat from .._oids import INVALID_OID, TEXT_OID -from .._compat import cache +from .._compat import cache, TypeVar from .._struct import pack_len, unpack_len from .._typeinfo import TypeInfo, TypesRegistry diff --git a/psycopg/setup.cfg b/psycopg/setup.cfg index 900418f57..f335c5896 100644 --- a/psycopg/setup.cfg +++ b/psycopg/setup.cfg @@ -53,7 +53,7 @@ packages = find: zip_safe = False install_requires = backports.zoneinfo >= 0.2.0; python_version < "3.9" - typing-extensions >= 4.2 + typing-extensions >= 4.4 tzdata; sys_platform == "win32" [options.extras_require] @@ -65,7 +65,7 @@ pool = psycopg-pool test = anyio >= 4.0 - mypy >= 1.4.1 + mypy >= 1.6 pproxy >= 2.7 pytest >= 6.2.5 pytest-cov >= 3.0 @@ -76,7 +76,7 @@ dev = black >= 23.1.0 dnspython >= 2.1 flake8 >= 4.0 - mypy >= 1.4.1 + mypy >= 1.6 types-setuptools >= 57.4 wheel >= 0.37 docs = diff --git a/psycopg_pool/psycopg_pool/_acompat.py b/psycopg_pool/psycopg_pool/_acompat.py index 09de06cc0..d6417590b 100644 --- a/psycopg_pool/psycopg_pool/_acompat.py +++ b/psycopg_pool/psycopg_pool/_acompat.py @@ -14,10 +14,12 @@ import queue import asyncio import logging import threading -from typing import Any, Callable, Coroutine, TypeVar, TYPE_CHECKING +from typing import Any, Callable, Coroutine, TYPE_CHECKING from typing_extensions import TypeAlias +from ._compat import TypeVar + logger = logging.getLogger("psycopg.pool") T = TypeVar("T") diff --git a/psycopg_pool/psycopg_pool/_compat.py b/psycopg_pool/psycopg_pool/_compat.py index e88cf2b3d..5917ff31b 100644 --- a/psycopg_pool/psycopg_pool/_compat.py +++ b/psycopg_pool/psycopg_pool/_compat.py @@ -19,10 +19,16 @@ if sys.version_info >= (3, 11): else: from typing_extensions import Self +if sys.version_info >= (3, 13): + from typing import TypeVar +else: + from typing_extensions import TypeVar + __all__ = [ "Counter", "Deque", "Self", + "TypeVar", ] # Workaround for psycopg < 3.0.8. diff --git a/psycopg_pool/psycopg_pool/abc.py b/psycopg_pool/psycopg_pool/abc.py index 5cc72f7fe..9d76b17fd 100644 --- a/psycopg_pool/psycopg_pool/abc.py +++ b/psycopg_pool/psycopg_pool/abc.py @@ -6,14 +6,16 @@ Types used in the psycopg_pool package from __future__ import annotations -from typing import Any, Awaitable, Callable, TypeVar, Union, TYPE_CHECKING +from typing import Any, Awaitable, Callable, Union, TYPE_CHECKING from typing_extensions import TypeAlias +from ._compat import TypeVar + if TYPE_CHECKING: from .pool import ConnectionPool from .pool_async import AsyncConnectionPool - from psycopg import Connection, AsyncConnection + from psycopg import Connection, AsyncConnection # noqa: F401 # Connection types to make the pool generic CT = TypeVar("CT", bound="Connection[Any]") diff --git a/psycopg_pool/setup.cfg b/psycopg_pool/setup.cfg index 8c3de4ba1..0d4f476cb 100644 --- a/psycopg_pool/setup.cfg +++ b/psycopg_pool/setup.cfg @@ -46,7 +46,7 @@ python_requires = >= 3.8 packages = find: zip_safe = False install_requires = - typing-extensions >= 4.0 + typing-extensions >= 4.4 [options.package_data] psycopg_pool = py.typed diff --git a/tests/constraints.txt b/tests/constraints.txt index 459f006e1..a676a993f 100644 --- a/tests/constraints.txt +++ b/tests/constraints.txt @@ -5,12 +5,12 @@ # From install_requires backports.zoneinfo == 0.2.0 -typing-extensions == 4.2.0 +typing-extensions == 4.4.0 importlib-metadata == 1.4 # From the 'test' extra anyio == 4.0 -mypy == 1.4.1 +mypy == 1.6.0 pproxy == 2.7.0 pytest == 6.2.5 pytest-cov == 3.0.0