| 1 |
""" |
| 2 |
Basic types we will more widely need in DaCHS; we also import everything |
| 3 |
we need from typing, so for our type annotations we only need to say |
| 4 |
from utils.dachstypes import ... |
| 5 |
|
| 6 |
This also lets us smuggle in fallbacks for python 3.7 (and we won't go |
| 7 |
further back than that). |
| 8 |
""" |
| 9 |
|
| 10 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 11 |
#c |
| 12 |
#c This program is free software, covered by the GNU GPL. See the |
| 13 |
#c COPYING file in the source distribution. |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
|
| 18 |
from typing import ( #noflake: gratuitous imports for re-exporting |
| 19 |
AbstractSet, Any, |
| 20 |
BinaryIO, |
| 21 |
Callable, ClassVar, ContextManager, |
| 22 |
Dict, |
| 23 |
Final, |
| 24 |
FrozenSet, |
| 25 |
Generator, |
| 26 |
Hashable, |
| 27 |
IO, Iterable, Iterator, |
| 28 |
Generic, |
| 29 |
Hashable, |
| 30 |
List, |
| 31 |
Mapping, |
| 32 |
NewType, NoReturn, |
| 33 |
Optional, |
| 34 |
Protocol, |
| 35 |
Sequence, Set, |
| 36 |
TextIO, Tuple, TYPE_CHECKING, Type, TypeVar, |
| 37 |
Union, |
| 38 |
cast) |
| 39 |
|
| 40 |
# things introduced after python 3.7; if their import fails, we replace |
| 41 |
# them with None, so type checking will fail with older pythons, but |
| 42 |
# that seems fine to me. |
| 43 |
try: |
| 44 |
from typing import ( #noflake: gratuitous imports for re-exporting |
| 45 |
Self |
| 46 |
) |
| 47 |
except ImportError: |
| 48 |
Self = None # type: ignore |
| 49 |
|
| 50 |
from types import ModuleType #noflake: gratuitous imports for re-exporting |
| 51 |
from re import Match #noflake: gratuitous imports for re-exporting |
| 52 |
|
| 53 |
import numpy |
| 54 |
|
| 55 |
try: |
| 56 |
from numpy.typing import NDArray |
| 57 |
except ImportError: |
| 58 |
NDArray = NewType('NDArray', object) # type: ignore |
| 59 |
|
| 60 |
Numeric = Union[int, float, numpy.floating, numpy.integer] |
| 61 |
|
| 62 |
# a base.events event handler |
| 63 |
BaseEventHandler = Callable |
| 64 |
|
| 65 |
import pathlib |
| 66 |
Filename = Union[str, pathlib.Path] |
| 67 |
# positions when doing XML parsing |
| 68 |
StrOrBytes = Union[str, bytes] |
| 69 |
StrToStrMap = Dict[str, str] |
| 70 |
|
| 71 |
# use this instead of T = TypeVar()... whenever possible. |
| 72 |
SomeClass = TypeVar("SomeClass") |
| 73 |
|
| 74 |
# see utils.plainxml |
| 75 |
PlainXMLEvent = Tuple[ |
| 76 |
str, |
| 77 |
Optional[str], |
| 78 |
Union[Dict[str, str], None, str]] |
| 79 |
|
| 80 |
# Something with a write method accepting bytes |
| 81 |
class BytesWritable(Protocol): |
| 82 |
def write(self, data: bytes): |
| 83 |
"outputs data somewhere." |
| 84 |
|
| 85 |
class BytesReadable(Protocol): |
| 86 |
def read(self, nBytes: Optional[int] = None): |
| 87 |
"reads data from somewhere." |
| 88 |
|
| 89 |
|
| 90 |
if TYPE_CHECKING: |
| 91 |
from gavo.base import Structure |
| 92 |
from gavo.base.sqlsupport import DaCHSConnection |
| 93 |
from gavo.base.parsecontext import ParseContext |
| 94 |
from gavo.rsc.table import BaseTable |
| 95 |
else: |
| 96 |
Structure = None # type: ignore |
| 97 |
DaCHSConnection = None # type: ignore |
| 98 |
ParseContext = None # type: ignore |
| 99 |
BaseTable = None # type: ignore |
| 100 |
|
| 101 |
# StanElement should be anything we can process within an utils.xmlstan |
| 102 |
# tree; use this whenever we are producing HTML or XML. For now |
| 103 |
# I'm being overly generic. Let's see if we want to lock this down a bit |
| 104 |
StanElement = Any |
| 105 |
|
| 106 |
StructParseContext = ParseContext |
| 107 |
|
| 108 |
# the type of value arguments in struct parser events. |
| 109 |
StructParserValue = Optional[Union[Dict[str, str], str]] |
| 110 |
|
| 111 |
# One of these days we ought to be a bit more concrete about which |
| 112 |
# source tokens we accept; but it's really up to the grammars... |
| 113 |
SourceToken = Any |
| 114 |
|
| 115 |
# We could get a lot more fancy here because the structure of |
| 116 |
# vocabularies is fairly strictly defined by desise; but I'd say |
| 117 |
# it's unlikely that type checking is a bit help when dealing with |
| 118 |
# them. |
| 119 |
Vocabulary = Dict[str, Any] |