"""
Specialised connection and cursor functions.
"""
-from typing import List, Optional, Any, Callable, ContextManager, Dict, Mapping, cast, overload, Tuple
+from typing import List, Optional, Any, Callable, ContextManager, Dict, cast, overload, Tuple
import contextlib
import logging
import os
import psycopg2.extras
from psycopg2 import sql as pysql
-from nominatim.typing import Query, T_cursor
+from nominatim.typing import SysEnv, Query, T_cursor
from nominatim.errors import UsageError
LOG = logging.getLogger()
def get_pg_env(dsn: str,
- base_env: Optional[Mapping[str, str]] = None) -> Dict[str, str]:
+ base_env: Optional[SysEnv] = None) -> Dict[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
"""
Helper functions for executing external programs.
"""
-from typing import Any, Union, Optional, Mapping
+from typing import Any, Union, Optional, Mapping, IO
from pathlib import Path
import logging
import subprocess
headers = {"User-Agent": f"Nominatim/{version_str()}"}
try:
- with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
+ request = urlrequest.Request(url, headers=headers)
+ with urlrequest.urlopen(request) as response: # type: IO[bytes]
return response.read().decode('utf-8')
except Exception:
LOG.fatal('Failed to load URL: %s', url)
Complex type definitions are moved here, to keep the source files readable.
"""
-from typing import Union, TypeVar, TYPE_CHECKING
+from typing import Union, Mapping, TypeVar, TYPE_CHECKING
# Generics varaible names do not confirm to naming styles, ignore globally here.
# pylint: disable=invalid-name
StrPath = Union[str, 'os.PathLike[str]']
+SysEnv = Mapping[str, str]
+
# psycopg2-related types
Query = Union[str, bytes, 'psycopg2.sql.Composable']