Skip to content

fix: reading the labels attribute on Job instances #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions google/cloud/bigquery/job/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def path(self):
@property
def labels(self):
"""Dict[str, str]: Labels for the job."""
return self._properties.setdefault("labels", {})
return self._properties.setdefault("configuration", {}).setdefault("labels", {})

@property
def etag(self):
Expand Down Expand Up @@ -671,9 +671,8 @@ def __setattr__(self, name, value):
def labels(self):
"""Dict[str, str]: Labels for the job.

This method always returns a dict. To change a job's labels,
modify the dict, then call ``Client.update_job``. To delete a
label, set its value to :data:`None` before updating.
This method always returns a dict. Once a job has been created on the
server, its labels cannot be modified anymore.

Raises:
ValueError: If ``value`` type is invalid.
Expand Down
17 changes: 17 additions & 0 deletions tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,23 @@ def test_job_cancel(self):
# raise an error, and that the job completed (in the `retry()`
# above).

def test_job_labels(self):
DATASET_ID = _make_dataset_id("job_cancel")
JOB_ID_PREFIX = "fetch_" + DATASET_ID
QUERY = "SELECT 1 as one"

self.temp_dataset(DATASET_ID)

job_config = bigquery.QueryJobConfig(
labels={"custom_label": "label_value", "another_label": "foo123"}
)
job = Config.CLIENT.query(
QUERY, job_id_prefix=JOB_ID_PREFIX, job_config=job_config
)

expected_labels = {"custom_label": "label_value", "another_label": "foo123"}
self.assertEqual(job.labels, expected_labels)

def test_get_failed_job(self):
# issue 4246
from google.api_core.exceptions import BadRequest
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/job/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_labels_hit(self):
labels = {"foo": "bar"}
client = _make_client(project=self.PROJECT)
job = self._make_one(self.JOB_ID, client)
job._properties["labels"] = labels
job._properties.setdefault("configuration", {})["labels"] = labels
self.assertEqual(job.labels, labels)

def test_etag(self):
Expand Down