Skip to content

Commit

Permalink
Make 'Blob.bucket' a readonly property. (#9113)
Browse files Browse the repository at this point in the history
Closes #9107.
  • Loading branch information
tseaver authored Aug 27, 2019
1 parent f4007a7 commit 9289c52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def __init__(
super(Blob, self).__init__(name=name)

self.chunk_size = chunk_size # Check that setter accepts value.
self.bucket = bucket
self._bucket = bucket
self._acl = ObjectACL(self)
if encryption_key is not None and kms_key_name is not None:
raise ValueError(
Expand All @@ -187,6 +187,15 @@ def __init__(
if generation is not None:
self._properties["generation"] = generation

@property
def bucket(self):
"""Bucket which contains the object.
:rtype: :class:`~google.cloud.storage.bucket.Bucket`
:returns: The object's bucket.
"""
return self._bucket

@property
def chunk_size(self):
"""Get the blob's default chunk size.
Expand Down
8 changes: 8 additions & 0 deletions storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ def test_path_with_non_ascii(self):
blob = self._make_one(blob_name, bucket=bucket)
self.assertEqual(blob.path, "/b/name/o/Caf%C3%A9")

def test_bucket_readonly_property(self):
blob_name = "BLOB"
bucket = _Bucket()
other = _Bucket()
blob = self._make_one(blob_name, bucket=bucket)
with self.assertRaises(AttributeError):
blob.bucket = other

def test_client(self):
blob_name = "BLOB"
bucket = _Bucket()
Expand Down

0 comments on commit 9289c52

Please sign in to comment.