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

fix(storage): str() metadata for for blob #9796

Merged
merged 4 commits into from
Nov 22, 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
1 change: 1 addition & 0 deletions storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,7 @@ def metadata(self, value):
:type value: dict
:param value: (Optional) The blob metadata to set.
"""
value = {k: str(v) for k, v in value.items()}
Copy link
Member

Choose a reason for hiding this comment

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

Thanks @HemangChothani!

Could you also add this to bucket labels given the similarity?

self._patch_property("metadata", value)

@property
Expand Down
1 change: 1 addition & 0 deletions storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ def labels(self, mapping):
existing = set([k for k in self.labels.keys()])
incoming = set([k for k in mapping.keys()])
self._label_removals = self._label_removals.union(existing.difference(incoming))
mapping = {k: str(v) for k, v in mapping.items()}

# Actually update the labels on the object.
self._patch_property("labels", copy.deepcopy(mapping))
Expand Down
10 changes: 10 additions & 0 deletions storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,16 @@ def test_metadata_setter(self):
blob.metadata = METADATA
self.assertEqual(blob.metadata, METADATA)

def test_metadata_setter_w_nan(self):
BLOB_NAME = "blob-name"
METADATA = {"foo": float("nan")}
bucket = _Bucket()
blob = self._make_one(BLOB_NAME, bucket=bucket)
self.assertIsNone(blob.metadata)
blob.metadata = METADATA
value = blob.metadata["foo"]
self.assertIsInstance(value, str)

def test_metageneration(self):
BUCKET = object()
METAGENERATION = 42
Expand Down
10 changes: 10 additions & 0 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,16 @@ def test_labels_setter(self):
self.assertIsNot(bucket._properties["labels"], LABELS)
self.assertIn("labels", bucket._changes)

def test_labels_setter_with_nan(self):
NAME = "name"
LABELS = {"color": "red", "foo": float("nan")}
bucket = self._make_one(name=NAME)

self.assertEqual(bucket.labels, {})
bucket.labels = LABELS
value = bucket.labels["foo"]
self.assertIsInstance(value, str)

def test_labels_setter_with_removal(self):
# Make sure the bucket labels look correct and follow the expected
# public structure.
Expand Down