From: Daniele Varrazzo Date: Mon, 27 Jun 2022 21:05:54 +0000 (+0100) Subject: feat: use LiteralString for Query and SQL aliases X-Git-Tag: 3.1~53^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1dc1bfd97f76ce5d39aa48824989dfb5a471be4e;p=thirdparty%2Fpsycopg.git feat: use LiteralString for Query and SQL aliases Current mypy version probably doesn't do anything with it at the moment. --- diff --git a/psycopg/psycopg/_compat.py b/psycopg/psycopg/_compat.py index 25804c8b5..16b1685c8 100644 --- a/psycopg/psycopg/_compat.py +++ b/psycopg/psycopg/_compat.py @@ -39,9 +39,15 @@ if sys.version_info >= (3, 10): else: from typing_extensions import TypeAlias, TypeGuard +if sys.version_info >= (3, 11): + from typing import LiteralString +else: + from typing_extensions import LiteralString + __all__ = [ "Counter", "Deque", + "LiteralString", "Protocol", "TypeAlias", "TypeGuard", diff --git a/psycopg/psycopg/abc.py b/psycopg/psycopg/abc.py index f4af55ac0..e6ff7607e 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, TypeAlias +from ._compat import Protocol, TypeAlias, LiteralString if TYPE_CHECKING: from .sql import Composable @@ -23,7 +23,7 @@ if TYPE_CHECKING: # An object implementing the buffer protocol Buffer: TypeAlias = Union[bytes, bytearray, memoryview] -Query: TypeAlias = Union[str, bytes, "Composable"] +Query: TypeAlias = Union[LiteralString, bytes, "Composable"] Params: TypeAlias = Union[Sequence[Any], Mapping[str, Any]] ConnectionType = TypeVar("ConnectionType", bound="BaseConnection[Any]") PipelineCommand: TypeAlias = Callable[[], None] diff --git a/psycopg/psycopg/sql.py b/psycopg/psycopg/sql.py index fcb67b874..df44fdf51 100644 --- a/psycopg/psycopg/sql.py +++ b/psycopg/psycopg/sql.py @@ -12,6 +12,7 @@ from typing import Any, Iterator, Iterable, List, Optional, Sequence, Union from .pq import Escaping from .abc import AdaptContext from .adapt import Transformer, PyFormat +from ._compat import LiteralString from ._encodings import conn_encoding @@ -193,7 +194,7 @@ class SQL(Composable): _obj: str _formatter = string.Formatter() - def __init__(self, obj: str): + def __init__(self, obj: LiteralString): super().__init__(obj) if not isinstance(obj, str): raise TypeError(f"SQL values must be strings, got {obj!r} instead") diff --git a/psycopg/setup.cfg b/psycopg/setup.cfg index fbf87e4fe..c0907e8c3 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 >= 4.1; python_version < "3.11" tzdata; sys_platform == "win32" [options.package_data] diff --git a/tests/constraints.txt b/tests/constraints.txt index ce4667d9d..7acc4c10c 100644 --- a/tests/constraints.txt +++ b/tests/constraints.txt @@ -5,7 +5,7 @@ # From install_requires backports.zoneinfo == 0.2.0 -typing-extensions == 3.10.0.0 +typing-extensions == 4.1.0 # From the 'test' extra mypy == 0.940