Skip to content

Commit ac23b77

Browse files
authored
Document deepcopy semantics of complex bucket properties. (#3712)
'cors', 'labels', and 'lifecycle_rules' all return copies of the values: changes to them have no effect until the copy is reassigned via the property's setter. Closes #3710
1 parent 96f0cc3 commit ac23b77

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

storage/google/cloud/storage/bucket.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,19 @@ def cors(self):
544544
See http://www.w3.org/TR/cors/ and
545545
https://cloud.google.com/storage/docs/json_api/v1/buckets
546546
547+
.. note::
548+
549+
The getter for this property returns a list which contains
550+
*copies* of the bucket's CORS policy mappings. Mutating the list
551+
or one of its dicts has no effect unless you then re-assign the
552+
dict via the setter. E.g.:
553+
554+
>>> policies = bucket.cors
555+
>>> policies.append({'origin': '/foo', ...})
556+
>>> policies[1]['maxAgeSeconds'] = 3600
557+
>>> del policies[0]
558+
>>> bucket.cors = policies
559+
547560
:setter: Set CORS policies for this bucket.
548561
:getter: Gets the CORS policies for this bucket.
549562
@@ -567,11 +580,22 @@ def cors(self, entries):
567580

568581
@property
569582
def labels(self):
570-
"""Retrieve or set CORS policies configured for this bucket.
583+
"""Retrieve or set labels assigned to this bucket.
571584
572585
See
573586
https://cloud.google.com/storage/docs/json_api/v1/buckets#labels
574587
588+
.. note::
589+
590+
The getter for this property returns a dict which is a *copy*
591+
of the bucket's labels. Mutating that dict has no effect unless
592+
you then re-assign the dict via the setter. E.g.:
593+
594+
>>> labels = bucket.labels
595+
>>> labels['new_key'] = 'some-label'
596+
>>> del labels['old_key']
597+
>>> bucket.labels = labels
598+
575599
:setter: Set labels for this bucket.
576600
:getter: Gets the labels for this bucket.
577601
@@ -585,7 +609,7 @@ def labels(self):
585609

586610
@labels.setter
587611
def labels(self, mapping):
588-
"""Set CORS policies configured for this bucket.
612+
"""Set labels assigned to this bucket.
589613
590614
See
591615
https://cloud.google.com/storage/docs/json_api/v1/buckets#labels
@@ -627,6 +651,19 @@ def lifecycle_rules(self):
627651
See https://cloud.google.com/storage/docs/lifecycle and
628652
https://cloud.google.com/storage/docs/json_api/v1/buckets
629653
654+
.. note::
655+
656+
The getter for this property returns a list which contains
657+
*copies* of the bucket's lifecycle rules mappings. Mutating the
658+
list or one of its dicts has no effect unless you then re-assign
659+
the dict via the setter. E.g.:
660+
661+
>>> rules = bucket.lifecycle_rules
662+
>>> rules.append({'origin': '/foo', ...})
663+
>>> rules[1]['rule']['action']['type'] = 'Delete'
664+
>>> del rules[0]
665+
>>> bucket.lifecycle_rules = rules
666+
630667
:setter: Set lifestyle rules for this bucket.
631668
:getter: Gets the lifestyle rules for this bucket.
632669

0 commit comments

Comments
 (0)