X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/e6ee3c772c7489a2456ff1ab42684f4664888ba4..681aad7e0dc099658eea15b769fdefd44cd8c484:/nominatim/db/connection.py diff --git a/nominatim/db/connection.py b/nominatim/db/connection.py index cbdbbfef..729e8a70 100644 --- a/nominatim/db/connection.py +++ b/nominatim/db/connection.py @@ -7,7 +7,7 @@ """ Specialised connection and cursor functions. """ -from typing import Union, List, Optional, Any, Callable, ContextManager, Mapping, cast, TypeVar, overload, Tuple, Sequence +from typing import List, Optional, Any, Callable, ContextManager, Mapping, cast, overload, Tuple import contextlib import logging import os @@ -17,11 +17,9 @@ import psycopg2.extensions import psycopg2.extras from psycopg2 import sql as pysql +from nominatim.typing import Query, T_cursor from nominatim.errors import UsageError -Query = Union[str, bytes, pysql.Composable] -T = TypeVar('T', bound=psycopg2.extensions.cursor) - LOG = logging.getLogger() class _Cursor(psycopg2.extras.DictCursor): @@ -38,7 +36,8 @@ class _Cursor(psycopg2.extras.DictCursor): super().execute(query, args) - def execute_values(self, sql: Query, argslist: List[Any], template: Optional[str] = None) -> None: + def execute_values(self, sql: Query, argslist: List[Any], + template: Optional[str] = None) -> None: """ Wrapper for the psycopg2 convenience function to execute SQL for a list of values. """ @@ -91,7 +90,7 @@ class _Connection(psycopg2.extensions.connection): ... @overload - def cursor(self, cursor_factory: Callable[..., T]) -> T: + def cursor(self, cursor_factory: Callable[..., T_cursor]) -> T_cursor: ... def cursor(self, cursor_factory = _Cursor, **kwargs): # type: ignore @@ -226,7 +225,7 @@ _PG_CONNECTION_STRINGS = { def get_pg_env(dsn: str, - base_env: Optional[Mapping[str, Optional[str]]] = None) -> Mapping[str, Optional[str]]: + base_env: Optional[Mapping[str, str]] = None) -> Mapping[str, str]: """ Return a copy of `base_env` with the environment variables for PostgresSQL set up from the given database connection string. If `base_env` is None, then the OS environment is used as a base