-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtypes.py
More file actions
114 lines (85 loc) · 2.78 KB
/
types.py
File metadata and controls
114 lines (85 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from dataclasses import dataclass
from datetime import date, datetime
from enum import Enum
from typing import TYPE_CHECKING, Generic, List, Literal, Tuple, Type, TypeVar, Union
from typing_extensions import TypeAlias
from latch.registry.record import Record
from latch.registry.upstream_types.types import DBType
from latch.registry.upstream_types.values import EmptyCell
from latch.ldata.path import LPath
from latch.types.directory import LatchDir
from latch.types.file import LatchFile
if not TYPE_CHECKING:
try:
from enum import StrEnum
except ImportError:
class StrEnum(str, Enum): ...
else:
class StrEnum(str, Enum): ...
@dataclass(frozen=True)
class InvalidValue:
"""Registry :class:`Record` value that failed validation."""
raw_value: str
"""User-provided string representation of the invalid value.
May be `""` (the empty string) if the value is missing but the column is required.
"""
RegistryPrimitivePythonValue: TypeAlias = Union[
str,
datetime,
date,
int,
float,
LatchFile,
LatchDir,
LPath,
None,
bool,
]
RegistryPythonValue: TypeAlias = Union[
RegistryPrimitivePythonValue,
Record,
List["RegistryPythonValue"],
]
RecordValue: TypeAlias = Union[RegistryPythonValue, EmptyCell, InvalidValue]
@dataclass(frozen=True)
class Column:
"""Registry :class:`Table` column definition.
:meth:`Table.get_columns` is the typical way to get a :class:`Column`.
"""
key: str
"""Unique identifier within the table. Not globally unique."""
type: Union[Type[RegistryPythonValue], Type[Union[RegistryPythonValue, EmptyCell]]]
"""Python equivalent of the stored column type."""
# fixme(maximsmol): deal with defaults
# default: Union[RegistryPythonValue, EmptyCell]
upstream_type: DBType
"""Raw column type.
Used to convert between Python values and Registry values.
"""
RegistryEnumDefinitionArg = TypeVar("RegistryEnumDefinitionArg", bound=Tuple[str, ...])
class RegistryEnumDefinition(Generic[RegistryEnumDefinitionArg]):
def __new__(cls, *values: str):
return RegistryEnumDefinition[Tuple[tuple(Literal[v] for v in values)]]
LinkedRecordTypeArg = TypeVar("LinkedRecordTypeArg", bound=str)
class LinkedRecordType(Generic[LinkedRecordTypeArg]):
def __new__(cls, id: str):
return LinkedRecordType[Literal[id]]
RegistryPrimitivePythonType: TypeAlias = Union[
Type[str],
Type[int],
Type[float],
Type[date],
Type[datetime],
Type[bool],
Type[LatchFile],
Type[LatchDir],
Type[StrEnum],
Type[RegistryEnumDefinition],
Type[LinkedRecordType],
]
RegistryPythonType: TypeAlias = Union[
RegistryPrimitivePythonType,
Type[List[LatchFile]],
Type[List[LatchDir]],
Type[List[LinkedRecordType]],
]