]> git.openstreetmap.org Git - nominatim.git/commitdiff
move complex typing annotations to extra file
authorSarah Hoffmann <lonvia@denofr.de>
Sat, 2 Jul 2022 09:59:19 +0000 (11:59 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Mon, 18 Jul 2022 07:47:57 +0000 (09:47 +0200)
nominatim/config.py
nominatim/db/connection.py
nominatim/typing.py [new file with mode: 0644]

index ed7d35e2448e55c9530406a2439b5acdcefedf3b..adc022e2e84b2d319b9b9d4d6c43099765d1be5b 100644 (file)
@@ -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.
index 2cc0fef8596db5ae4d1f259476f3da477ec20f7a..729e8a700761843ea33e36f7630482cc063faa72 100644 (file)
@@ -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 (file)
index 0000000..abecc53
--- /dev/null
@@ -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')