Skip to content
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

Storage: Make 'Blob.bucket' a readonly property. #9113

Merged
merged 1 commit into from
Aug 27, 2019
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
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):
tseaver marked this conversation as resolved.
Show resolved Hide resolved
"""Bucket which contains the object.

:rtype: :class:`~google.cloud.storage.bucket.Bucket`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Google-style docstrings on new methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems cleaner to make that a task to convert whole modules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy if it matches the file in which it is added. Agree that we likely have a backlog task to do this though.

: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