forked from hans/Nominatim
move complex typing annotations to extra file
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
27
nominatim/typing.py
Normal file
27
nominatim/typing.py
Normal file
@@ -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')
|
||||
Reference in New Issue
Block a user