1414# limitations under the License.
1515"""Helper to deal with field types in Spanner interactions."""
1616
17+ from __future__ import annotations
18+
1719import abc
1820import datetime
21+ from typing import Any , Type
1922
2023from google .cloud .spanner_v1 .proto import type_pb2
2124
2225
2326class Field (object ):
2427 """Represents a column in a table as a field in a model."""
2528
26- def __init__ (self , field_type , nullable = False , primary_key = False ):
29+ def __init__ (self ,
30+ field_type : Type [FieldType ],
31+ nullable : bool = False ,
32+ primary_key : bool = False ):
2733 self ._type = field_type
2834 self ._nullable = nullable
2935 self ._primary_key = primary_key
3036 self .name = None
3137
32- def ddl (self ):
38+ def ddl (self ) -> str :
3339 if self ._nullable :
3440 return self ._type .ddl ()
3541 return '{field_type} NOT NULL' .format (field_type = self ._type .ddl ())
3642
37- def field_type (self ):
43+ def field_type (self ) -> Type [ FieldType ] :
3844 return self ._type
3945
40- def grpc_type (self ):
46+ def grpc_type (self ) -> str :
4147 return self ._type .grpc_type ()
4248
43- def nullable (self ):
49+ def nullable (self ) -> bool :
4450 return self ._nullable
4551
46- def primary_key (self ):
52+ def primary_key (self ) -> bool :
4753 return self ._primary_key
4854
49- def validate (self , value ):
55+ def validate (self , value ) -> None :
5056 if value is None :
5157 assert self ._nullable , 'None set for non-nullable field'
5258 else :
@@ -58,81 +64,81 @@ class FieldType(abc.ABC):
5864
5965 @staticmethod
6066 @abc .abstractmethod
61- def ddl ():
67+ def ddl () -> str :
6268 raise NotImplementedError
6369
6470 @staticmethod
6571 @abc .abstractmethod
66- def grpc_type ():
72+ def grpc_type () -> type_pb2 . Type :
6773 raise NotImplementedError
6874
6975 @staticmethod
7076 @abc .abstractmethod
71- def validate_type (value ) :
77+ def validate_type (value : Any ) -> None :
7278 raise NotImplementedError
7379
7480
7581class Boolean (FieldType ):
7682 """Represents a boolean type."""
7783
7884 @staticmethod
79- def ddl ():
85+ def ddl () -> str :
8086 return 'BOOL'
8187
8288 @staticmethod
83- def grpc_type ():
89+ def grpc_type () -> type_pb2 . Type :
8490 return type_pb2 .Type (code = type_pb2 .BOOL )
8591
8692 @staticmethod
87- def validate_type (value ) :
93+ def validate_type (value : Any ) -> None :
8894 assert isinstance (value , bool ), '{} is not of type bool' .format (value )
8995
9096
9197class Integer (FieldType ):
9298 """Represents an integer type."""
9399
94100 @staticmethod
95- def ddl ():
101+ def ddl () -> str :
96102 return 'INT64'
97103
98104 @staticmethod
99- def grpc_type ():
105+ def grpc_type () -> type_pb2 . Type :
100106 return type_pb2 .Type (code = type_pb2 .INT64 )
101107
102108 @staticmethod
103- def validate_type (value ) :
109+ def validate_type (value : Any ) -> None :
104110 assert isinstance (value , int ), '{} is not of type int' .format (value )
105111
106112
107113class String (FieldType ):
108114 """Represents a string type."""
109115
110116 @staticmethod
111- def ddl ():
117+ def ddl () -> str :
112118 return 'STRING(MAX)'
113119
114120 @staticmethod
115- def grpc_type ():
121+ def grpc_type () -> type_pb2 . Type :
116122 return type_pb2 .Type (code = type_pb2 .STRING )
117123
118124 @staticmethod
119- def validate_type (value ):
125+ def validate_type (value ) -> None :
120126 assert isinstance (value , str ), '{} is not of type str' .format (value )
121127
122128
123129class StringArray (FieldType ):
124130 """Represents an array of strings type."""
125131
126132 @staticmethod
127- def ddl ():
133+ def ddl () -> str :
128134 return 'ARRAY<STRING(MAX)>'
129135
130136 @staticmethod
131- def grpc_type ():
137+ def grpc_type () -> type_pb2 . Type :
132138 return type_pb2 .Type (code = type_pb2 .ARRAY )
133139
134140 @staticmethod
135- def validate_type (value ) :
141+ def validate_type (value : Any ) -> None :
136142 assert isinstance (value , list ), '{} is not of type list' .format (value )
137143 for item in value :
138144 assert isinstance (item , str ), '{} is not of type str' .format (item )
@@ -142,15 +148,15 @@ class Timestamp(FieldType):
142148 """Represents a timestamp type."""
143149
144150 @staticmethod
145- def ddl ():
151+ def ddl () -> str :
146152 return 'TIMESTAMP'
147153
148154 @staticmethod
149- def grpc_type ():
155+ def grpc_type () -> type_pb2 . Type :
150156 return type_pb2 .Type (code = type_pb2 .TIMESTAMP )
151157
152158 @staticmethod
153- def validate_type (value ) :
159+ def validate_type (value : Any ) -> None :
154160 assert isinstance (value , datetime .datetime )
155161
156162
0 commit comments