GeezerVison is showing github.com — Storage: bucket.labels surprisingly returns a deepcopy · Issue #3710 · googleapis/google-cloud-python
Storage: bucket.labels surprisingly returns a deepcopy #3710
Description
I expected this to work:
bucket = storage_client.get_bucket(bucket_name)
bucket.labels['example'] = 'label'
bucket.patch()
Turns out bucket.labels returns a deepcopy, so I have to do this instead:
bucket = storage_client.get_bucket(bucket_name)
labels = bucket.labels
labels['example'] = 'label'
bucket.labels = labels
bucket.patch()Activity
dhermes commented on Aug 1, 2017
I'm not sure what the right move is. There are definitely pros and cons here, i.e. I can imagine a user surprised they mutated the Bucket when they didn't intend to.
theacodes commented on Aug 1, 2017
ContributorAuthor
More actions
dhermes commented on Aug 1, 2017
Copy-pasta strikes again!
tseaver commented on Aug 1, 2017
The other properties which contain non-scalar values (cors, lifecycle_rules) also return copies. I'm working on a PR which adds the following note (modified to use values appropriate to each one):
.. note::
The getter for this property returns a list which contains
*copies* of the bucket's CORS policy mappings. Mutating the list
or one of its dicts has no effect unless you then re-assign the
dict via the setter. E.g.:
>>> cors = bucket.cors
>>> cors.append({'origin': '/foo', ...})
>>> del policies[0]
>>> bucket.cors = corsadded a commit that references this issue
on Aug 1, 2017This issue will close once commit ac23b77 is merged into the 'main' branch.ac23b77 added 3 commits that reference this issue
on Aug 21, 2017This issue will close once commit ee66cab is merged into the 'master' branch.ee66cab This issue will close once commit fbc3a57 is merged into the 'master' branch.fbc3a57 This issue will close once commit 24c5554 is merged into the 'master' branch.24c5554 added a commit that references this issue
on Jun 4, 2023This issue will close once commit 8d9e265 is merged into the 'main' branch.8d9e265 added a commit that references this issue
on Mar 9, 2026This issue will close once commit e2974a8 is merged into the 'main' branch.e2974a8
Metadata
Metadata
Development
No branches or pull requestsIssue actions