From c4928c646df3b4ff1409358c2e34b9883d600d39 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 3 Jul 2022 17:38:11 +0200 Subject: [PATCH] define type for enivronment dictionaries --- nominatim/db/connection.py | 6 +++--- nominatim/tools/exec_utils.py | 5 +++-- nominatim/typing.py | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/nominatim/db/connection.py b/nominatim/db/connection.py index 8d9c742a..10327725 100644 --- a/nominatim/db/connection.py +++ b/nominatim/db/connection.py @@ -7,7 +7,7 @@ """ 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 @@ -17,7 +17,7 @@ import psycopg2.extensions 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() @@ -225,7 +225,7 @@ _PG_CONNECTION_STRINGS = { 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 diff --git a/nominatim/tools/exec_utils.py b/nominatim/tools/exec_utils.py index 6b080792..4e6afdc2 100644 --- a/nominatim/tools/exec_utils.py +++ b/nominatim/tools/exec_utils.py @@ -7,7 +7,7 @@ """ 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 @@ -160,7 +160,8 @@ def get_url(url: str) -> str: 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) diff --git a/nominatim/typing.py b/nominatim/typing.py index abecc53d..6d754989 100644 --- a/nominatim/typing.py +++ b/nominatim/typing.py @@ -9,7 +9,7 @@ Type definitions for typing annotations. 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 @@ -21,6 +21,8 @@ if TYPE_CHECKING: StrPath = Union[str, 'os.PathLike[str]'] +SysEnv = Mapping[str, str] + # psycopg2-related types Query = Union[str, bytes, 'psycopg2.sql.Composable'] -- 2.39.5