move complex typing annotations to extra file

This commit is contained in:
Sarah Hoffmann
2022-07-02 11:59:19 +02:00
parent 992e6f72cf
commit f22fa992f7
3 changed files with 37 additions and 12 deletions

View File

@@ -7,7 +7,7 @@
""" """
Nominatim configuration accessor. Nominatim configuration accessor.
""" """
from typing import Dict, Any, List, Mapping, Optional, Union from typing import Dict, Any, List, Mapping, Optional
import logging import logging
import os import os
from pathlib import Path from pathlib import Path
@@ -16,10 +16,9 @@ import yaml
from dotenv import dotenv_values from dotenv import dotenv_values
from nominatim.typing import StrPath
from nominatim.errors import UsageError from nominatim.errors import UsageError
PathOrStr = Union[str, os.PathLike[str]]
LOG = logging.getLogger() LOG = logging.getLogger()
CONFIG_CACHE : Dict[str, Any] = {} CONFIG_CACHE : Dict[str, Any] = {}
@@ -72,7 +71,7 @@ class Configuration:
self.lib_dir = _LibDirs() 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. """ Set paths to library functions and data.
""" """
for key, value in kwargs.items(): for key, value in kwargs.items():
@@ -178,7 +177,7 @@ class Configuration:
return env return env
def load_sub_configuration(self, filename: PathOrStr, def load_sub_configuration(self, filename: StrPath,
config: Optional[str] = None) -> Any: config: Optional[str] = None) -> Any:
""" Load additional configuration from a file. `filename` is the name """ Load additional configuration from a file. `filename` is the name
of the configuration file. The file is first searched in the of the configuration file. The file is first searched in the
@@ -216,7 +215,7 @@ class Configuration:
return result return result
def find_config_file(self, filename: PathOrStr, def find_config_file(self, filename: StrPath,
config: Optional[str] = None) -> Path: config: Optional[str] = None) -> Path:
""" Resolve the location of a configuration file given a filename and """ Resolve the location of a configuration file given a filename and
an optional configuration option with the file name. an optional configuration option with the file name.

View File

@@ -7,7 +7,7 @@
""" """
Specialised connection and cursor functions. 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 contextlib
import logging import logging
import os import os
@@ -17,11 +17,9 @@ import psycopg2.extensions
import psycopg2.extras import psycopg2.extras
from psycopg2 import sql as pysql from psycopg2 import sql as pysql
from nominatim.typing import Query, T_cursor
from nominatim.errors import UsageError from nominatim.errors import UsageError
Query = Union[str, bytes, pysql.Composable]
T = TypeVar('T', bound=psycopg2.extensions.cursor)
LOG = logging.getLogger() LOG = logging.getLogger()
class _Cursor(psycopg2.extras.DictCursor): class _Cursor(psycopg2.extras.DictCursor):
@@ -38,7 +36,8 @@ class _Cursor(psycopg2.extras.DictCursor):
super().execute(query, args) 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 """ Wrapper for the psycopg2 convenience function to execute
SQL for a list of values. SQL for a list of values.
""" """
@@ -91,7 +90,7 @@ class _Connection(psycopg2.extensions.connection):
... ...
@overload @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 def cursor(self, cursor_factory = _Cursor, **kwargs): # type: ignore

27
nominatim/typing.py Normal file
View 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')