From f22fa992f7975757d84ae17e42ff98c15f7a572b Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sat, 2 Jul 2022 11:59:19 +0200 Subject: [PATCH] move complex typing annotations to extra file --- nominatim/config.py | 11 +++++------ nominatim/db/connection.py | 11 +++++------ nominatim/typing.py | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 nominatim/typing.py diff --git a/nominatim/config.py b/nominatim/config.py index ed7d35e2..adc022e2 100644 --- a/nominatim/config.py +++ b/nominatim/config.py @@ -7,7 +7,7 @@ """ Nominatim configuration accessor. """ -from typing import Dict, Any, List, Mapping, Optional, Union +from typing import Dict, Any, List, Mapping, Optional import logging import os from pathlib import Path @@ -16,10 +16,9 @@ import yaml from dotenv import dotenv_values +from nominatim.typing import StrPath from nominatim.errors import UsageError -PathOrStr = Union[str, os.PathLike[str]] - LOG = logging.getLogger() CONFIG_CACHE : Dict[str, Any] = {} @@ -72,7 +71,7 @@ class Configuration: self.lib_dir = _LibDirs() - def set_libdirs(self, **kwargs: PathOrStr) -> None: + def set_libdirs(self, **kwargs: StrPath) -> None: """ Set paths to library functions and data. """ for key, value in kwargs.items(): @@ -178,7 +177,7 @@ class Configuration: return env - def load_sub_configuration(self, filename: PathOrStr, + def load_sub_configuration(self, filename: StrPath, config: Optional[str] = None) -> Any: """ Load additional configuration from a file. `filename` is the name of the configuration file. The file is first searched in the @@ -216,7 +215,7 @@ class Configuration: return result - def find_config_file(self, filename: PathOrStr, + def find_config_file(self, filename: StrPath, config: Optional[str] = None) -> Path: """ Resolve the location of a configuration file given a filename and an optional configuration option with the file name. diff --git a/nominatim/db/connection.py b/nominatim/db/connection.py index 2cc0fef8..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 diff --git a/nominatim/typing.py b/nominatim/typing.py new file mode 100644 index 00000000..abecc53d --- /dev/null +++ b/nominatim/typing.py @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# This file is part of Nominatim. (https://nominatim.org) +# +# Copyright (C) 2022 by the Nominatim developer community. +# For a full list of authors see the git log. +""" +Type definitions for typing annotations. + +Complex type definitions are moved here, to keep the source files readable. +""" +from typing import Union, TypeVar, TYPE_CHECKING + +# Generics varaible names do not confirm to naming styles, ignore globally here. +# pylint: disable=invalid-name + +if TYPE_CHECKING: + import psycopg2.sql + import psycopg2.extensions + import os + +StrPath = Union[str, 'os.PathLike[str]'] + +# psycopg2-related types + +Query = Union[str, bytes, 'psycopg2.sql.Composable'] +T_cursor = TypeVar('T_cursor', bound='psycopg2.extensions.cursor') -- 2.45.1