Type-hint conventions¶
AxsDB targets Python 3.9+. The rules below keep annotations consistent across
the codebase and avoid deprecated typing shims.
from __future__ import annotationsEvery module that carries type annotations must open with:
from __future__ import annotationsThis makes all annotations strings at runtime, which is what allows the remaining rules to work on Python 3.9 without runtime errors.
- Built-in generics
Use lowercase built-in types as generics directly, e.g.:
dict[str, int] list[float] tuple[np.ndarray, np.ndarray] set[str]
Avoid importing
typing‘sDict,List,Tuple, orSet.- Union and
None Use the pipe operator for all unions and for optional types:
float | tuple[float, float] # not Union[float, Tuple[float, float]] ErrorHandlingConfiguration | None # not Optional[ErrorHandlingConfiguration]
- Abstract container types
Import abstract base classes from
collections.abc, nottyping:from collections.abc import Callable, Hashable, Mapping, Sequence- Class-object annotations
Use lowercase
type[X]instead oftyping.Type[X]:AbsorptionDatabaseT = type[AbsorptionDatabase] # inside TYPE_CHECKING block- Permitted
typingimports The following names have no built-in or
collections.abcequivalent and are the only items that should appear in afrom typing import …line:AnyAnnotatedLiteralTypeAliasTypeVarTYPE_CHECKING
- Tests
Test functions and methods do not carry type annotations. Helpers might, but this is not required.