11from dataclasses import dataclass
2+ from dataclasses import field as dataclass_field
23from datetime import datetime , timedelta
34from enum import Enum
4- from typing import TypedDict , get_args , get_origin
5+ from typing import Literal , TypeAlias , TypedDict , get_args , get_origin
56from uuid import UUID
67
78import numpy as np
@@ -24,17 +25,56 @@ class DatasetKind(Enum):
2425_dataset_kind_int_to_enum = {kind .value : kind for kind in DatasetKind }
2526
2627
28+ class FieldRole (Enum ):
29+ PRIMARY_TITLE = dataset_type_pb2 .FIELD_ROLE_PRIMARY_TITLE
30+ """The primary human-readable title or name used when displaying a datapoint."""
31+
32+
33+ _field_roles_from_string = {role .name .lower (): role for role in FieldRole }
34+ _field_role_int_to_enum = {role .value : role for role in FieldRole }
35+ FieldRolesLike : TypeAlias = list [FieldRole ] | list [Literal ["primary_title" ]]
36+
37+
2738@dataclass (frozen = True )
2839class FieldAnnotation :
2940 description : str
41+ """A human-readable description of the field."""
3042 example_value : str
43+ """An example value illustrating the field's expected contents."""
44+ source_json_pointer : str | None = None
45+ """An RFC 6901 JSON Pointer locating the field value in the source document."""
46+ queryable : bool = False
47+ """Whether the field is available for use in server-side filter expressions."""
48+ json_schema_ref : str | None = None
49+ """A JSON Schema reference for the field when it corresponds to a well-known schema."""
50+ roles : list [FieldRole ] = dataclass_field (default_factory = list )
51+ """Semantic display roles fulfilled by the field."""
3152
3253 @classmethod
3354 def from_message (cls , annotation : dataset_type_pb2 .FieldAnnotation ) -> "FieldAnnotation" :
34- return cls (description = annotation .description , example_value = annotation .example_value )
55+ return cls (
56+ description = annotation .description ,
57+ example_value = annotation .example_value ,
58+ source_json_pointer = (
59+ annotation .source_json_pointer if annotation .HasField ("source_json_pointer" ) else None
60+ ),
61+ queryable = annotation .queryable ,
62+ json_schema_ref = annotation .json_schema_ref if annotation .HasField ("json_schema_ref" ) else None ,
63+ roles = [_field_role_int_to_enum [role ] for role in annotation .roles ],
64+ )
3565
3666 def to_message (self ) -> dataset_type_pb2 .FieldAnnotation :
37- return dataset_type_pb2 .FieldAnnotation (description = self .description , example_value = self .example_value )
67+ annotation = dataset_type_pb2 .FieldAnnotation (
68+ description = self .description ,
69+ example_value = self .example_value ,
70+ queryable = self .queryable ,
71+ roles = [role .value for role in self .roles ],
72+ )
73+ if self .source_json_pointer is not None :
74+ annotation .source_json_pointer = self .source_json_pointer
75+ if self .json_schema_ref is not None :
76+ annotation .json_schema_ref = self .json_schema_ref
77+ return annotation
3878
3979
4080class FieldDict (TypedDict ):
@@ -63,6 +103,10 @@ class FieldDict(TypedDict):
63103 ]
64104 description : NotRequired [str ]
65105 example_value : NotRequired [str ]
106+ source_json_pointer : NotRequired [str | None ]
107+ queryable : NotRequired [bool ]
108+ json_schema_ref : NotRequired [str | None ]
109+ roles : NotRequired [FieldRolesLike ]
66110
67111
68112_TYPE_INFO : dict [type , tuple [FieldDescriptorProto .Type .ValueType , str | None ]] = {
@@ -83,14 +127,12 @@ class FieldDict(TypedDict):
83127class Field :
84128 descriptor : FieldDescriptorProto
85129 annotation : FieldAnnotation
86- queryable : bool
87130
88131 @classmethod
89132 def from_message (cls , field : dataset_type_pb2 .Field ) -> "Field" :
90133 return cls (
91134 descriptor = field .descriptor ,
92135 annotation = FieldAnnotation .from_message (field .annotation ),
93- queryable = field .annotation .queryable ,
94136 )
95137
96138 @classmethod
@@ -116,16 +158,19 @@ def from_dict(cls, field: FieldDict) -> "Field":
116158 annotation = FieldAnnotation (
117159 description = field .get ("description" , "" ),
118160 example_value = field .get ("example_value" , "" ),
161+ source_json_pointer = field .get ("source_json_pointer" ),
162+ queryable = field .get ("queryable" , False ),
163+ json_schema_ref = field .get ("json_schema_ref" ),
164+ roles = [
165+ _field_roles_from_string [role ] if isinstance (role , str ) else role for role in field .get ("roles" , [])
166+ ],
119167 ),
120- queryable = False ,
121168 )
122169
123170 def to_message (self ) -> dataset_type_pb2 .Field :
124- annotation = self .annotation .to_message ()
125- annotation .queryable = self .queryable
126171 return dataset_type_pb2 .Field (
127172 descriptor = self .descriptor ,
128- annotation = annotation ,
173+ annotation = self . annotation . to_message () ,
129174 )
130175
131176
0 commit comments