Skip to content

Commit 80944f0

Browse files
authored
fix: reading the labels attribute on Job instances (#471)
1 parent d7fc252 commit 80944f0

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

google/cloud/bigquery/job/base.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def path(self):
233233
@property
234234
def labels(self):
235235
"""Dict[str, str]: Labels for the job."""
236-
return self._properties.setdefault("labels", {})
236+
return self._properties.setdefault("configuration", {}).setdefault("labels", {})
237237

238238
@property
239239
def etag(self):
@@ -671,9 +671,8 @@ def __setattr__(self, name, value):
671671
def labels(self):
672672
"""Dict[str, str]: Labels for the job.
673673
674-
This method always returns a dict. To change a job's labels,
675-
modify the dict, then call ``Client.update_job``. To delete a
676-
label, set its value to :data:`None` before updating.
674+
This method always returns a dict. Once a job has been created on the
675+
server, its labels cannot be modified anymore.
677676
678677
Raises:
679678
ValueError: If ``value`` type is invalid.

tests/system.py

+17
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,23 @@ def test_job_cancel(self):
16671667
# raise an error, and that the job completed (in the `retry()`
16681668
# above).
16691669

1670+
def test_job_labels(self):
1671+
DATASET_ID = _make_dataset_id("job_cancel")
1672+
JOB_ID_PREFIX = "fetch_" + DATASET_ID
1673+
QUERY = "SELECT 1 as one"
1674+
1675+
self.temp_dataset(DATASET_ID)
1676+
1677+
job_config = bigquery.QueryJobConfig(
1678+
labels={"custom_label": "label_value", "another_label": "foo123"}
1679+
)
1680+
job = Config.CLIENT.query(
1681+
QUERY, job_id_prefix=JOB_ID_PREFIX, job_config=job_config
1682+
)
1683+
1684+
expected_labels = {"custom_label": "label_value", "another_label": "foo123"}
1685+
self.assertEqual(job.labels, expected_labels)
1686+
16701687
def test_get_failed_job(self):
16711688
# issue 4246
16721689
from google.api_core.exceptions import BadRequest

tests/unit/job/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def test_labels_hit(self):
251251
labels = {"foo": "bar"}
252252
client = _make_client(project=self.PROJECT)
253253
job = self._make_one(self.JOB_ID, client)
254-
job._properties["labels"] = labels
254+
job._properties.setdefault("configuration", {})["labels"] = labels
255255
self.assertEqual(job.labels, labels)
256256

257257
def test_etag(self):

0 commit comments

Comments
 (0)