Skip to content

Commit de5b775

Browse files
authored
Add 'stacklevel=2' to deprecation warnings. (googleapis#5897)
1 parent bcbc300 commit de5b775

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

storage/google/cloud/storage/blob.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,8 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
10261026
.. _lifecycle: https://cloud.google.com/storage/docs/lifecycle
10271027
"""
10281028
if num_retries is not None:
1029-
warnings.warn(_NUM_RETRIES_MESSAGE, DeprecationWarning)
1029+
warnings.warn(
1030+
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2)
10301031

10311032
_maybe_rewind(file_obj, rewind=rewind)
10321033
predefined_acl = ACL.validate_predefined(predefined_acl)

storage/google/cloud/storage/bucket.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT
4242

4343

44+
_LOCATION_SETTER_MESSAGE = (
45+
"Assignment to 'Bucket.location' is deprecated, as it is only "
46+
"valid before the bucket is created. Instead, pass the location "
47+
"to `Bucket.create`.")
48+
49+
4450
def _blobs_page_start(iterator, page, response):
4551
"""Grab prefixes after a :class:`~google.cloud.iterator.Page` started.
4652
@@ -1194,10 +1200,7 @@ def location(self, value):
11941200
to `Bucket.create`.
11951201
"""
11961202
warnings.warn(
1197-
"Assignment to 'Bucket.location' is deprecated, as it is only "
1198-
"valid before the bucket is created. Instead, pass the location "
1199-
"to `Bucket.create`.",
1200-
DeprecationWarning)
1203+
_LOCATION_SETTER_MESSAGE, DeprecationWarning, stacklevel=2)
12011204
self._location = value
12021205

12031206
def get_logging(self):

storage/tests/unit/test_blob.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,9 @@ def test_upload_from_file_with_retries(self, mock_warn):
16541654

16551655
self._upload_from_file_helper(num_retries=20)
16561656
mock_warn.assert_called_once_with(
1657-
blob_module._NUM_RETRIES_MESSAGE, DeprecationWarning)
1657+
blob_module._NUM_RETRIES_MESSAGE,
1658+
DeprecationWarning,
1659+
stacklevel=2)
16581660

16591661
def test_upload_from_file_with_rewind(self):
16601662
stream = self._upload_from_file_helper(rewind=True)

storage/tests/unit/test_bucket.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,12 +547,11 @@ def test_create_w_extra_properties(self):
547547
bucket = self._make_one(client=client, name=BUCKET_NAME)
548548
bucket.cors = CORS
549549
bucket.lifecycle_rules = LIFECYCLE_RULES
550-
bucket.location = LOCATION
551550
bucket.storage_class = STORAGE_CLASS
552551
bucket.versioning_enabled = True
553552
bucket.requester_pays = True
554553
bucket.labels = LABELS
555-
bucket.create()
554+
bucket.create(location=LOCATION)
556555

557556
kw, = connection._requested
558557
self.assertEqual(kw['method'], 'POST')
@@ -1094,13 +1093,20 @@ def test_location_getter(self):
10941093
bucket = self._make_one(name=NAME, properties=before)
10951094
self.assertEqual(bucket.location, 'AS')
10961095

1097-
def test_location_setter(self):
1096+
@mock.patch('warnings.warn')
1097+
def test_location_setter(self, mock_warn):
1098+
from google.cloud.storage import bucket as bucket_module
1099+
10981100
NAME = 'name'
10991101
bucket = self._make_one(name=NAME)
11001102
self.assertIsNone(bucket.location)
11011103
bucket.location = 'AS'
11021104
self.assertEqual(bucket.location, 'AS')
11031105
self.assertTrue('location' in bucket._changes)
1106+
mock_warn.assert_called_once_with(
1107+
bucket_module._LOCATION_SETTER_MESSAGE,
1108+
DeprecationWarning,
1109+
stacklevel=2)
11041110

11051111
def test_lifecycle_rules_getter_unknown_action_type(self):
11061112
NAME = 'name'

0 commit comments

Comments
 (0)