1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Type definitions for typing annotations.
10 Complex type definitions are moved here, to keep the source files readable.
12 from typing import Any, Union, Mapping, TypeVar, Sequence, TYPE_CHECKING
14 # Generics variable names do not confirm to naming styles, ignore globally here.
15 # pylint: disable=invalid-name,abstract-method,multiple-statements
16 # pylint: disable=missing-class-docstring,useless-import-alias
20 import psycopg2.extensions
21 import psycopg2.extras
24 StrPath = Union[str, 'os.PathLike[str]']
26 SysEnv = Mapping[str, str]
28 # psycopg2-related types
30 Query = Union[str, bytes, 'psycopg2.sql.Composable']
32 T_ResultKey = TypeVar('T_ResultKey', int, str)
34 class DictCursorResult(Mapping[str, Any]):
35 def __getitem__(self, x: Union[int, str]) -> Any: ...
37 DictCursorResults = Sequence[DictCursorResult]
39 T_cursor = TypeVar('T_cursor', bound='psycopg2.extensions.cursor')
41 # The following typing features require typing_extensions to work
42 # on all supported Python versions.
43 # Only require this for type checking but not for normal operations.
46 from typing_extensions import (Protocol as Protocol,
48 TypedDict as TypedDict)
55 # SQLAlchemy introduced generic types in version 2.0 making typing
56 # incompatible with older versions. Add wrappers here so we don't have
57 # to litter the code with bare-string types.
60 import sqlalchemy as sa
61 from typing_extensions import (TypeAlias as TypeAlias)
65 SaSelect: TypeAlias = 'sa.Select[Any]'
66 SaRow: TypeAlias = 'sa.Row[Any]'
67 SaColumn: TypeAlias = 'sa.ColumnElement[Any]'
68 SaLabel: TypeAlias = 'sa.Label[Any]'
69 SaFromClause: TypeAlias = 'sa.FromClause'
70 SaSelectable: TypeAlias = 'sa.Selectable'