4
4
import abc
5
5
import inspect
6
6
import json
7
+ from typing import Any , Dict , List , Mapping , Optional , Tuple , Union
7
8
8
- from . import utils
9
- from . import sdkType
10
-
11
- from typing import Any , Dict , List , Optional , Mapping , Union , Tuple
9
+ from . import sdkType , utils
12
10
13
11
14
12
class Datum :
@@ -20,17 +18,17 @@ def __init__(self, value: Any, type: Optional[str]):
20
18
def python_value (self ) -> Any :
21
19
if self .value is None or self .type is None :
22
20
return None
23
- elif self .type in (' bytes' , ' string' , ' int' , ' double' ):
21
+ elif self .type in (" bytes" , " string" , " int" , " double" ):
24
22
return self .value
25
- elif self .type == ' json' :
23
+ elif self .type == " json" :
26
24
return json .loads (self .value )
27
- elif self .type == ' collection_string' :
25
+ elif self .type == " collection_string" :
28
26
return [v for v in self .value .string ]
29
- elif self .type == ' collection_bytes' :
27
+ elif self .type == " collection_bytes" :
30
28
return [v for v in self .value .bytes ]
31
- elif self .type == ' collection_double' :
29
+ elif self .type == " collection_double" :
32
30
return [v for v in self .value .double ]
33
- elif self .type == ' collection_sint64' :
31
+ elif self .type == " collection_sint64" :
34
32
return [v for v in self .value .sint64 ]
35
33
else :
36
34
return self .value
@@ -51,27 +49,28 @@ def __hash__(self):
51
49
def __repr__ (self ):
52
50
val_repr = repr (self .value )
53
51
if len (val_repr ) > 10 :
54
- val_repr = val_repr [:10 ] + ' ...'
55
- return ' <Datum {} {}>' .format (self .type , val_repr )
52
+ val_repr = val_repr [:10 ] + " ..."
53
+ return " <Datum {} {}>" .format (self .type , val_repr )
56
54
57
55
58
56
class _ConverterMeta (abc .ABCMeta ):
59
57
60
58
_bindings : Dict [str , type ] = {}
61
59
62
- def __new__ (mcls , name , bases , dct , * ,
63
- binding : Optional [str ],
64
- trigger : Optional [ str ] = None ):
60
+ def __new__ (
61
+ mcls , name , bases , dct , * , binding : Optional [str ], trigger : Optional [ str ] = None
62
+ ):
65
63
cls = super ().__new__ (mcls , name , bases , dct )
66
64
cls ._trigger = trigger # type: ignore
67
65
if binding is None :
68
66
return cls
69
67
70
68
if binding in mcls ._bindings :
71
69
raise RuntimeError (
72
- f'cannot register a converter for { binding !r} binding: '
73
- f'another converter for this binding has already been '
74
- f'registered' )
70
+ f"cannot register a converter for { binding !r} binding: "
71
+ f"another converter for this binding has already been "
72
+ f"registered"
73
+ )
75
74
76
75
mcls ._bindings [binding ] = cls
77
76
if trigger is not None :
@@ -101,51 +100,59 @@ class _BaseConverter(metaclass=_ConverterMeta, binding=None):
101
100
102
101
@classmethod
103
102
def _decode_typed_data (
104
- cls , data : Datum , * ,
105
- python_type : Union [type , Tuple [type , ...]],
106
- context : str = 'data' ) -> Any :
103
+ cls ,
104
+ data : Datum ,
105
+ * ,
106
+ python_type : Union [type , Tuple [type , ...]],
107
+ context : str = "data" ,
108
+ ) -> Any :
107
109
if data is None :
108
110
return None
109
111
110
112
data_type = data .type
111
- if data_type == ' model_binding_data' :
113
+ if data_type == " model_binding_data" :
112
114
result = data .value
113
115
elif data_type is None :
114
116
return None
115
117
else :
116
- raise ValueError (
117
- f'unsupported type of { context } : { data_type } ' )
118
+ raise ValueError (f"unsupported type of { context } : { data_type } " )
118
119
119
120
if not isinstance (result , python_type ):
120
121
if isinstance (python_type , (tuple , list , dict )):
121
122
raise ValueError (
122
- f'unexpected value type in { context } : '
123
- f'{ type (result ).__name__ } , expected one of: '
124
- f'{ ", " .join (t .__name__ for t in python_type )} ' )
123
+ f"unexpected value type in { context } : "
124
+ f"{ type (result ).__name__ } , expected one of: "
125
+ f'{ ", " .join (t .__name__ for t in python_type )} '
126
+ )
125
127
else :
126
128
try :
127
129
# Try coercing into the requested type
128
130
result = python_type (result )
129
131
except (TypeError , ValueError ) as e :
130
132
raise ValueError (
131
- f'cannot convert value of { context } into '
132
- f'{ python_type .__name__ } : { e } ' ) from None
133
+ f"cannot convert value of { context } into "
134
+ f"{ python_type .__name__ } : { e } "
135
+ ) from None
133
136
134
137
return result
135
138
136
139
@classmethod
137
140
def _decode_trigger_metadata_field (
138
- cls , trigger_metadata : Mapping [str , Datum ],
139
- field : str , * ,
140
- python_type : Union [type , Tuple [type , ...]]) \
141
- -> Any :
141
+ cls ,
142
+ trigger_metadata : Mapping [str , Datum ],
143
+ field : str ,
144
+ * ,
145
+ python_type : Union [type , Tuple [type , ...]],
146
+ ) -> Any :
142
147
data = trigger_metadata .get (field )
143
148
if data is None :
144
149
return None
145
150
else :
146
151
return cls ._decode_typed_data (
147
- data , python_type = python_type ,
148
- context = f'field { field !r} in trigger metadata' )
152
+ data ,
153
+ python_type = python_type ,
154
+ context = f"field { field !r} in trigger metadata" ,
155
+ )
149
156
150
157
151
158
class InConverter (_BaseConverter , binding = None ):
@@ -175,8 +182,7 @@ def check_output_type_annotation(cls, pytype: type) -> bool:
175
182
176
183
@classmethod
177
184
@abc .abstractmethod
178
- def encode (cls , obj : Any , * ,
179
- expected_type : Optional [type ]) -> Optional [Datum ]:
185
+ def encode (cls , obj : Any , * , expected_type : Optional [type ]) -> Optional [Datum ]:
180
186
raise NotImplementedError
181
187
182
188
0 commit comments