Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 7f49237

Browse files
committed
Added lazy initialization to table object
1 parent 39ae646 commit 7f49237

2 files changed

Lines changed: 75 additions & 29 deletions

File tree

google/cloud/bigtable/table.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,9 @@ def __init__(self, table_id, instance, mutation_timeout=None, app_profile_id=Non
132132
self._app_profile_id = app_profile_id
133133
self.mutation_timeout = mutation_timeout
134134

135-
# TODO: Figure out which value to use for mutation_timeout after looking at how
136-
# mutation_timeout is used in this class.
137-
self._table_impl = (
138-
self._instance._client._veneer_data_client.get_table(
139-
self._instance.instance_id,
140-
self.table_id,
141-
app_profile_id=app_profile_id,
142-
)
143-
if self._instance
144-
else None
145-
)
135+
# Lazily initialize the table shim to avoid causing constructor errors for instance=None
136+
# that don't exist in the original implementation.
137+
self._table_impl = None
146138

147139
@property
148140
def name(self):
@@ -174,6 +166,18 @@ def name(self):
174166
project=project, instance=instance_id, table=self.table_id
175167
)
176168

169+
@property
170+
def _veneer_data_table(self):
171+
"""Getter for the data client table representation of this object."""
172+
if self._table_impl is None:
173+
self._table_impl = self._instance._client._veneer_data_client.get_table(
174+
self._instance.instance_id,
175+
self.table_id,
176+
app_profile_id=self._app_profile_id,
177+
)
178+
179+
return self._table_impl
180+
177181
def get_iam_policy(self):
178182
"""Gets the IAM access control policy for this table.
179183

tests/unit/v2_client/test_table.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ def _make_table(*args, **kwargs):
162162

163163

164164
def test_table_constructor_defaults():
165+
instance = mock.Mock(spec=[])
166+
167+
table = _make_table(TABLE_ID, instance)
168+
169+
assert table.table_id == TABLE_ID
170+
assert table._instance is instance
171+
assert table.mutation_timeout is None
172+
assert table._app_profile_id is None
173+
174+
175+
def test_table_constructor_explicit():
176+
instance = mock.Mock(spec=[])
177+
mutation_timeout = 123
178+
app_profile_id = "profile-123"
179+
180+
table = _make_table(
181+
TABLE_ID,
182+
instance,
183+
mutation_timeout=mutation_timeout,
184+
app_profile_id=app_profile_id,
185+
)
186+
187+
assert table.table_id == TABLE_ID
188+
assert table._instance is instance
189+
assert table.mutation_timeout == mutation_timeout
190+
assert table._app_profile_id == app_profile_id
191+
192+
193+
def test_table_name():
165194
table_data_client = mock.Mock(spec=["table_path"])
166195
_veneer_data_client = mock.Mock()
167196
client = mock.Mock(
@@ -178,17 +207,35 @@ def test_table_constructor_defaults():
178207

179208
table = _make_table(TABLE_ID, instance)
180209

181-
assert table.table_id == TABLE_ID
182-
assert table._instance is instance
183-
assert table.mutation_timeout is None
184-
assert table._app_profile_id is None
185-
assert table._table_impl == _veneer_data_client.get_table.return_value
210+
assert table.name == table_data_client.table_path.return_value
211+
212+
213+
def test_table_veneer_data_table_not_initialized_defaults():
214+
table_data_client = mock.Mock(spec=["table_path"])
215+
_veneer_data_client = mock.Mock()
216+
client = mock.Mock(
217+
project=PROJECT_ID,
218+
table_data_client=table_data_client,
219+
_veneer_data_client=_veneer_data_client,
220+
spec=["project", "table_data_client", "_veneer_data_client"],
221+
)
222+
instance = mock.Mock(
223+
_client=client,
224+
instance_id=INSTANCE_ID,
225+
spec=["_client", "instance_id"],
226+
)
227+
228+
table = _make_table(TABLE_ID, instance)
229+
table._veneer_data_table
230+
186231
_veneer_data_client.get_table.assert_called_once_with(
187-
INSTANCE_ID, TABLE_ID, app_profile_id=None
232+
INSTANCE_ID,
233+
TABLE_ID,
234+
app_profile_id=None,
188235
)
189236

190237

191-
def test_table_constructor_explicit():
238+
def test_table_veneer_data_table_not_initialized_explicit():
192239
table_data_client = mock.Mock(spec=["table_path"])
193240
_veneer_data_client = mock.Mock()
194241
client = mock.Mock(
@@ -202,29 +249,23 @@ def test_table_constructor_explicit():
202249
instance_id=INSTANCE_ID,
203250
spec=["_client", "instance_id"],
204251
)
205-
mutation_timeout = 123
206-
app_profile_id = "profile-123"
207252

253+
app_profile_id = "profile-123"
208254
table = _make_table(
209255
TABLE_ID,
210256
instance,
211-
mutation_timeout=mutation_timeout,
212257
app_profile_id=app_profile_id,
213258
)
259+
table._veneer_data_table
214260

215-
assert table.table_id == TABLE_ID
216-
assert table._instance is instance
217-
assert table.mutation_timeout == mutation_timeout
218-
assert table._app_profile_id == app_profile_id
219-
assert table._table_impl == _veneer_data_client.get_table.return_value
220261
_veneer_data_client.get_table.assert_called_once_with(
221262
INSTANCE_ID,
222263
TABLE_ID,
223264
app_profile_id=app_profile_id,
224265
)
225266

226267

227-
def test_table_name():
268+
def test_table_veneer_data_table_initialized():
228269
table_data_client = mock.Mock(spec=["table_path"])
229270
_veneer_data_client = mock.Mock()
230271
client = mock.Mock(
@@ -240,8 +281,9 @@ def test_table_name():
240281
)
241282

242283
table = _make_table(TABLE_ID, instance)
243-
244-
assert table.name == table_data_client.table_path.return_value
284+
already = table._table_impl = object()
285+
assert table._veneer_data_table is already
286+
_veneer_data_client.get_table.assert_not_called()
245287

246288

247289
def _table_row_methods_helper():

0 commit comments

Comments
 (0)