1717import base64
1818import binascii
1919import datetime
20- from typing import Any , Type
20+ from typing import Any , Optional , Type
2121
2222from google .cloud import spanner
2323from google .cloud import spanner_v1
@@ -30,49 +30,70 @@ class FieldType(abc.ABC):
3030 @staticmethod
3131 @abc .abstractmethod
3232 def ddl () -> str :
33+ """Returns the DDL for this type."""
3334 raise NotImplementedError
3435
3536 @staticmethod
3637 @abc .abstractmethod
3738 def grpc_type () -> spanner_v1 .Type :
39+ """Returns the type as used in Cloud Spanner's gRPC API."""
3840 raise NotImplementedError
3941
4042 @staticmethod
4143 @abc .abstractmethod
4244 def validate_type (value : Any ) -> None :
45+ """Raises error.ValidationError if value doesn't match the type."""
4346 raise NotImplementedError
4447
4548
4649class Field :
47- """Represents a column in a table as a field in a model."""
50+ """Represents a column in a table as a field in a model.
51+
52+ Attributes:
53+ name: Name of the column, or None if this hasn't been bound to a column yet.
54+ """
55+ name : Optional [str ]
4856
4957 def __init__ (self ,
5058 field_type : Type [FieldType ],
5159 nullable : bool = False ,
5260 primary_key : bool = False ):
61+ """Initializer.
62+
63+ Args:
64+ field_type: Type of the field.
65+ nullable: Whether the field can be NULL.
66+ primary_key: Whether the field is part of the table's primary key.
67+ """
5368 self .name = None
5469 self ._type = field_type
5570 self ._nullable = nullable
5671 self ._primary_key = primary_key
5772
5873 def ddl (self ) -> str :
74+ """Returns DDL for the column."""
5975 if self ._nullable :
6076 return self ._type .ddl ()
6177 return '{field_type} NOT NULL' .format (field_type = self ._type .ddl ())
6278
6379 def field_type (self ) -> Type [FieldType ]:
80+ """Returns the type of the field."""
6481 return self ._type
6582
6683 def grpc_type (self ) -> str :
84+ """Returns the type as used in Cloud Spanner's gRPC API."""
6785 return self ._type .grpc_type ()
6886
6987 def nullable (self ) -> bool :
88+ """Returns whether the field can be NULL."""
7089 return self ._nullable
7190
7291 def primary_key (self ) -> bool :
92+ """Returns whether the field is part of the table's primary key."""
7393 return self ._primary_key
7494
75- def validate (self , value ) -> None :
95+ def validate (self , value : Any ) -> None :
96+ """Raises error.ValidationError if value isn't compatible with the field."""
7697 if value is None :
7798 if not self ._nullable :
7899 raise error .ValidationError ('None set for non-nullable field' )
@@ -85,14 +106,17 @@ class Boolean(FieldType):
85106
86107 @staticmethod
87108 def ddl () -> str :
109+ """See base class."""
88110 return 'BOOL'
89111
90112 @staticmethod
91113 def grpc_type () -> spanner_v1 .Type :
114+ """See base class."""
92115 return spanner .param_types .BOOL
93116
94117 @staticmethod
95118 def validate_type (value : Any ) -> None :
119+ """See base class."""
96120 if not isinstance (value , bool ):
97121 raise error .ValidationError ('{} is not of type bool' .format (value ))
98122
@@ -102,14 +126,17 @@ class Integer(FieldType):
102126
103127 @staticmethod
104128 def ddl () -> str :
129+ """See base class."""
105130 return 'INT64'
106131
107132 @staticmethod
108133 def grpc_type () -> spanner_v1 .Type :
134+ """See base class."""
109135 return spanner .param_types .INT64
110136
111137 @staticmethod
112138 def validate_type (value : Any ) -> None :
139+ """See base class."""
113140 if not isinstance (value , int ):
114141 raise error .ValidationError ('{} is not of type int' .format (value ))
115142
@@ -119,14 +146,17 @@ class Float(FieldType):
119146
120147 @staticmethod
121148 def ddl () -> str :
149+ """See base class."""
122150 return 'FLOAT64'
123151
124152 @staticmethod
125153 def grpc_type () -> spanner_v1 .Type :
154+ """See base class."""
126155 return spanner .param_types .FLOAT64
127156
128157 @staticmethod
129158 def validate_type (value : Any ) -> None :
159+ """See base class."""
130160 if not isinstance (value , (int , float )):
131161 raise error .ValidationError ('{} is not of type float' .format (value ))
132162
@@ -136,14 +166,17 @@ class String(FieldType):
136166
137167 @staticmethod
138168 def ddl () -> str :
169+ """See base class."""
139170 return 'STRING(MAX)'
140171
141172 @staticmethod
142173 def grpc_type () -> spanner_v1 .Type :
174+ """See base class."""
143175 return spanner .param_types .STRING
144176
145177 @staticmethod
146- def validate_type (value ) -> None :
178+ def validate_type (value : Any ) -> None :
179+ """See base class."""
147180 if not isinstance (value , str ):
148181 raise error .ValidationError ('{} is not of type str' .format (value ))
149182
@@ -153,14 +186,17 @@ class StringArray(FieldType):
153186
154187 @staticmethod
155188 def ddl () -> str :
189+ """See base class."""
156190 return 'ARRAY<STRING(MAX)>'
157191
158192 @staticmethod
159193 def grpc_type () -> spanner_v1 .Type :
194+ """See base class."""
160195 return spanner .param_types .Array (spanner .param_types .STRING )
161196
162197 @staticmethod
163198 def validate_type (value : Any ) -> None :
199+ """See base class."""
164200 if not isinstance (value , list ):
165201 raise error .ValidationError ('{} is not of type list' .format (value ))
166202 for item in value :
@@ -173,14 +209,17 @@ class Timestamp(FieldType):
173209
174210 @staticmethod
175211 def ddl () -> str :
212+ """See base class."""
176213 return 'TIMESTAMP'
177214
178215 @staticmethod
179216 def grpc_type () -> spanner_v1 .Type :
217+ """See base class."""
180218 return spanner .param_types .TIMESTAMP
181219
182220 @staticmethod
183221 def validate_type (value : Any ) -> None :
222+ """See base class."""
184223 if not isinstance (value , datetime .datetime ):
185224 raise error .ValidationError ('{} is not of type datetime' .format (value ))
186225
@@ -190,14 +229,17 @@ class BytesBase64(FieldType):
190229
191230 @staticmethod
192231 def ddl () -> str :
232+ """See base class."""
193233 return 'BYTES(MAX)'
194234
195235 @staticmethod
196236 def grpc_type () -> spanner_v1 .Type :
237+ """See base class."""
197238 return spanner .param_types .BYTES
198239
199240 @staticmethod
200- def validate_type (value ) -> None :
241+ def validate_type (value : Any ) -> None :
242+ """See base class."""
201243 if not isinstance (value , bytes ):
202244 raise error .ValidationError ('{} is not of type bytes' .format (value ))
203245 # Rudimentary test to check for base64 encoding.
0 commit comments