Skip to content

Commit

Permalink
fix(storage): str() metadata for for blob (#9796)
Browse files Browse the repository at this point in the history
* fix(storage): fix stringify metadata for blob

* fix(storage): fix lint fail

* fix(storage): stringify labels for bucket

* fix(storage): fix lint
  • Loading branch information
HemangChothani authored and crwilcox committed Nov 22, 2019
1 parent 0588f19 commit 75dc3ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions storage/google/cloud/storage/blob.py
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()}
self._patch_property("metadata", value)

@property
Expand Down
1 change: 1 addition & 0 deletions storage/google/cloud/storage/bucket.py
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
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
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

0 comments on commit 75dc3ec

Please sign in to comment.