Skip to content

Commit 7508078

Browse files
authored
chore: clean up aliases used for older Python versions (#273)
1 parent 91c9145 commit 7508078

File tree

14 files changed

+62
-69
lines changed

14 files changed

+62
-69
lines changed

packages/google-resumable-media/google/_async_resumable_media/_upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ async def _process_response(self, response, bytes_sent):
635635
"""
636636
status_code = _helpers.require_status_code(
637637
response,
638-
(http.client.OK, _async_resumable_media.PERMANENT_REDIRECT),
638+
(http.client.OK, http.client.PERMANENT_REDIRECT),
639639
self._get_status_code,
640640
callback=self._make_invalid,
641641
)
@@ -776,7 +776,7 @@ def _process_recover_response(self, response):
776776
"""
777777
_helpers.require_status_code(
778778
response,
779-
(_async_resumable_media.PERMANENT_REDIRECT,),
779+
(http.client.PERMANENT_REDIRECT,),
780780
self._get_status_code,
781781
)
782782
headers = self._get_headers(response)

packages/google-resumable-media/google/_async_resumable_media/requests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,11 @@ def SimpleUpload(*args, **kwargs):
604604
upload._chunk_size = 4
605605
# Make three fake responses.
606606
fake_response0 = requests.Response()
607-
fake_response0.status_code = resumable_media.PERMANENT_REDIRECT
607+
fake_response0.status_code = http.client.PERMANENT_REDIRECT
608608
fake_response0.headers['range'] = 'bytes=0-3'
609609
610610
fake_response1 = requests.Response()
611-
fake_response1.status_code = resumable_media.PERMANENT_REDIRECT
611+
fake_response1.status_code = http.client.PERMANENT_REDIRECT
612612
fake_response1.headers['range'] = 'bytes=0-7'
613613
614614
fake_response2 = requests.Response()

packages/google-resumable-media/google/resumable_media/_upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def _process_response(self, response, bytes_sent):
670670
"""
671671
status_code = _helpers.require_status_code(
672672
response,
673-
(http.client.OK, resumable_media.PERMANENT_REDIRECT),
673+
(http.client.OK, http.client.PERMANENT_REDIRECT),
674674
self._get_status_code,
675675
callback=self._make_invalid,
676676
)
@@ -814,7 +814,7 @@ def _process_recover_response(self, response):
814814
.. _sans-I/O: https://sans-io.readthedocs.io/
815815
"""
816816
_helpers.require_status_code(
817-
response, (resumable_media.PERMANENT_REDIRECT,), self._get_status_code
817+
response, (http.client.PERMANENT_REDIRECT,), self._get_status_code
818818
)
819819
headers = self._get_headers(response)
820820
if _helpers.RANGE_HEADER in headers:

packages/google-resumable-media/google/resumable_media/common.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,39 @@
2525

2626
UPLOAD_CHUNK_SIZE = 262144 # 256 * 1024
2727
"""int: Chunks in a resumable upload must come in multiples of 256 KB."""
28-
PERMANENT_REDIRECT = 308
28+
29+
PERMANENT_REDIRECT = http.client.PERMANENT_REDIRECT
2930
"""int: Permanent redirect status code.
3031
32+
.. note::
33+
This is a backward-compatibility alias.
34+
3135
It is used by Google services to indicate some (but not all) of
3236
a resumable upload has been completed.
3337
34-
``http.client.PERMANENT_REDIRECT`` was added in Python 3.5, so
35-
can't be used in a "general" code base.
36-
3738
For more information, see `RFC 7238`_.
3839
3940
.. _RFC 7238: https://tools.ietf.org/html/rfc7238
4041
"""
41-
TOO_MANY_REQUESTS = 429
42+
43+
TOO_MANY_REQUESTS = http.client.TOO_MANY_REQUESTS
4244
"""int: Status code indicating rate-limiting.
4345
44-
``http.client.TOO_MANY_REQUESTS`` was added in Python 3.3, so
45-
can't be used in a "general" code base.
46+
.. note::
47+
This is a backward-compatibility alias.
4648
4749
For more information, see `RFC 6585`_.
4850
4951
.. _RFC 6585: https://tools.ietf.org/html/rfc6585#section-4
5052
"""
53+
5154
MAX_SLEEP = 64.0
5255
"""float: Maximum amount of time allowed between requests.
5356
5457
Used during the retry process for sleep after a failed request.
5558
Chosen since it is the power of two nearest to one minute.
5659
"""
60+
5761
MAX_CUMULATIVE_RETRY = 600.0
5862
"""float: Maximum total sleep time allowed during retry process.
5963
@@ -62,7 +66,7 @@
6266
"""
6367

6468
RETRYABLE = (
65-
TOO_MANY_REQUESTS, # 429
69+
http.client.TOO_MANY_REQUESTS, # 429
6670
http.client.REQUEST_TIMEOUT, # 408
6771
http.client.INTERNAL_SERVER_ERROR, # 500
6872
http.client.BAD_GATEWAY, # 502

packages/google-resumable-media/google/resumable_media/requests/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,11 @@ def SimpleUpload(*args, **kwargs):
604604
upload._chunk_size = 4
605605
# Make three fake responses.
606606
fake_response0 = requests.Response()
607-
fake_response0.status_code = resumable_media.PERMANENT_REDIRECT
607+
fake_response0.status_code = http.client.PERMANENT_REDIRECT
608608
fake_response0.headers['range'] = 'bytes=0-3'
609609
610610
fake_response1 = requests.Response()
611-
fake_response1.status_code = resumable_media.PERMANENT_REDIRECT
611+
fake_response1.status_code = http.client.PERMANENT_REDIRECT
612612
fake_response1.headers['range'] = 'bytes=0-7'
613613
614614
fake_response2 = requests.Response()
@@ -631,15 +631,15 @@ def SimpleUpload(*args, **kwargs):
631631
632632
>>> response0 = upload.transmit_next_chunk(transport)
633633
>>> response0
634-
<Response [308]>
634+
<Response [HTTPStatus.PERMANENT_REDIRECT]>
635635
>>> upload.finished
636636
False
637637
>>> upload.bytes_uploaded == upload.chunk_size
638638
True
639639
>>>
640640
>>> response1 = upload.transmit_next_chunk(transport)
641641
>>> response1
642-
<Response [308]>
642+
<Response [HTTPStatus.PERMANENT_REDIRECT]>
643643
>>> upload.finished
644644
False
645645
>>> upload.bytes_uploaded == 2 * upload.chunk_size

packages/google-resumable-media/google/resumable_media/requests/upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def transmit_next_chunk(
476476
...
477477
>>> error
478478
InvalidResponse('Request failed with status code', 400,
479-
'Expected one of', <HTTPStatus.OK: 200>, 308)
479+
'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PERMANENT_REDIRECT: 308>)
480480
>>> error.response
481481
<Response [400]>
482482
@@ -496,7 +496,7 @@ def transmit_next_chunk(
496496
497497
Raises:
498498
~google.resumable_media.common.InvalidResponse: If the status
499-
code is not 200 or 308.
499+
code is not 200 or http.client.PERMANENT_REDIRECT.
500500
~google.resumable_media.common.DataCorruption: If this is the final
501501
chunk, a checksum validation was requested, and the checksum
502502
does not match or is not available.

packages/google-resumable-media/tests/system/requests/test_upload.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def transmit_chunks(
182182
)
183183
else:
184184
assert upload.bytes_uploaded == num_chunks * upload.chunk_size
185-
assert response.status_code == resumable_media.PERMANENT_REDIRECT
185+
assert response.status_code == http.client.PERMANENT_REDIRECT
186186

187187
return num_chunks
188188

@@ -427,7 +427,7 @@ def sabotage_and_recover(upload, stream, transport, chunk_size):
427427
upload._bytes_uploaded = 0 # Make ``bytes_uploaded`` wrong as well.
428428
# Recover the (artifically) invalid upload.
429429
response = upload.recover(transport)
430-
assert response.status_code == resumable_media.PERMANENT_REDIRECT
430+
assert response.status_code == http.client.PERMANENT_REDIRECT
431431
assert not upload.invalid
432432
assert upload.bytes_uploaded == chunk_size
433433
assert stream.tell() == chunk_size
@@ -456,7 +456,7 @@ def _resumable_upload_recover_helper(
456456
check_initiate(response, upload, stream, authorized_transport, metadata)
457457
# Make the first request.
458458
response = upload.transmit_next_chunk(authorized_transport)
459-
assert response.status_code == resumable_media.PERMANENT_REDIRECT
459+
assert response.status_code == http.client.PERMANENT_REDIRECT
460460
# Call upload.recover().
461461
sabotage_and_recover(upload, stream, authorized_transport, chunk_size)
462462
# Now stream what remains.
@@ -508,7 +508,7 @@ def _check_partial(self, upload, response, chunk_size, num_chunks):
508508

509509
assert not upload.finished
510510
assert upload.bytes_uploaded == end_byte + 1
511-
assert response.status_code == resumable_media.PERMANENT_REDIRECT
511+
assert response.status_code == http.client.PERMANENT_REDIRECT
512512
assert response.content == b""
513513

514514
self._check_range_sent(response, start_byte, end_byte, "*")

packages/google-resumable-media/tests/unit/requests/test_upload.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import mock
2020

21-
from google import resumable_media
2221
import google.resumable_media.requests.upload as upload_mod
2322

2423

@@ -254,9 +253,7 @@ def test_transmit_next_chunk(self):
254253
upload._chunk_size = chunk_size
255254
# Make a fake 308 response.
256255
response_headers = {"range": "bytes=0-{:d}".format(chunk_size - 1)}
257-
transport = self._chunk_mock(
258-
resumable_media.PERMANENT_REDIRECT, response_headers
259-
)
256+
transport = self._chunk_mock(http.client.PERMANENT_REDIRECT, response_headers)
260257
# Check the state before the request.
261258
assert upload._bytes_uploaded == 0
262259

@@ -290,9 +287,7 @@ def test_transmit_next_chunk_w_custom_timeout(self):
290287

291288
# Make a fake 308 response.
292289
response_headers = {"range": "bytes=0-{:d}".format(chunk_size - 1)}
293-
transport = self._chunk_mock(
294-
resumable_media.PERMANENT_REDIRECT, response_headers
295-
)
290+
transport = self._chunk_mock(http.client.PERMANENT_REDIRECT, response_headers)
296291

297292
# Make request and check the return value (against the mock).
298293
upload.transmit_next_chunk(transport, timeout=12.6)
@@ -320,7 +315,7 @@ def test_recover(self):
320315

321316
end = 55555
322317
headers = {"range": "bytes=0-{:d}".format(end)}
323-
transport = self._chunk_mock(resumable_media.PERMANENT_REDIRECT, headers)
318+
transport = self._chunk_mock(http.client.PERMANENT_REDIRECT, headers)
324319

325320
ret_val = upload.recover(transport)
326321
assert ret_val is transport.request.return_value

packages/google-resumable-media/tests/unit/test__helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,11 @@ def test_retry_exceeds_max_cumulative(self, randint_mock, sleep_mock):
365365
status_codes = (
366366
http.client.SERVICE_UNAVAILABLE,
367367
http.client.GATEWAY_TIMEOUT,
368-
common.TOO_MANY_REQUESTS,
368+
http.client.TOO_MANY_REQUESTS,
369369
http.client.INTERNAL_SERVER_ERROR,
370370
http.client.SERVICE_UNAVAILABLE,
371371
http.client.BAD_GATEWAY,
372-
common.TOO_MANY_REQUESTS,
372+
http.client.TOO_MANY_REQUESTS,
373373
)
374374
responses = [_make_response(status_code) for status_code in status_codes]
375375

@@ -407,11 +407,11 @@ def test_retry_exceeds_max_retries(self, randint_mock, sleep_mock):
407407
status_codes = (
408408
http.client.SERVICE_UNAVAILABLE,
409409
http.client.GATEWAY_TIMEOUT,
410-
common.TOO_MANY_REQUESTS,
410+
http.client.TOO_MANY_REQUESTS,
411411
http.client.INTERNAL_SERVER_ERROR,
412412
http.client.SERVICE_UNAVAILABLE,
413413
http.client.BAD_GATEWAY,
414-
common.TOO_MANY_REQUESTS,
414+
http.client.TOO_MANY_REQUESTS,
415415
)
416416
responses = [_make_response(status_code) for status_code in status_codes]
417417
randint_mock.side_effect = [75 * i for i in range(len(responses))]
@@ -451,7 +451,7 @@ def test_retry_zero_max_retries(self, randint_mock, sleep_mock):
451451
status_codes = (
452452
http.client.SERVICE_UNAVAILABLE,
453453
http.client.GATEWAY_TIMEOUT,
454-
common.TOO_MANY_REQUESTS,
454+
http.client.TOO_MANY_REQUESTS,
455455
)
456456
responses = [_make_response(status_code) for status_code in status_codes]
457457
randint_mock.side_effect = [125 * i for i in range(len(status_codes))]

packages/google-resumable-media/tests/unit/test__upload.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import mock
2020
import pytest
2121

22-
from google import resumable_media
2322
from google.resumable_media import _helpers
2423
from google.resumable_media import _upload
2524
from google.resumable_media import common
@@ -721,7 +720,7 @@ def test__process_response_bad_status(self):
721720
assert len(error.args) == 5
722721
assert error.args[1] == response.status_code
723722
assert error.args[3] == http.client.OK
724-
assert error.args[4] == resumable_media.PERMANENT_REDIRECT
723+
assert error.args[4] == http.client.PERMANENT_REDIRECT
725724
# Make sure the upload is invalid after the failure.
726725
assert upload.invalid
727726

@@ -754,7 +753,7 @@ def test__process_response_partial_no_range(self):
754753
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
755754
_fix_up_virtual(upload)
756755

757-
response = _make_response(status_code=resumable_media.PERMANENT_REDIRECT)
756+
response = _make_response(status_code=http.client.PERMANENT_REDIRECT)
758757
# Make sure the upload is valid before the failure.
759758
assert not upload.invalid
760759
with pytest.raises(common.InvalidResponse) as exc_info:
@@ -776,7 +775,7 @@ def test__process_response_partial_bad_range(self):
776775
assert not upload.invalid
777776
headers = {"range": "nights 1-81"}
778777
response = _make_response(
779-
status_code=resumable_media.PERMANENT_REDIRECT, headers=headers
778+
status_code=http.client.PERMANENT_REDIRECT, headers=headers
780779
)
781780
with pytest.raises(common.InvalidResponse) as exc_info:
782781
upload._process_response(response, 81)
@@ -797,7 +796,7 @@ def test__process_response_partial(self):
797796
assert upload._bytes_uploaded == 0
798797
headers = {"range": "bytes=0-171"}
799798
response = _make_response(
800-
status_code=resumable_media.PERMANENT_REDIRECT, headers=headers
799+
status_code=http.client.PERMANENT_REDIRECT, headers=headers
801800
)
802801
ret_val = upload._process_response(response, 172)
803802
assert ret_val is None
@@ -975,7 +974,7 @@ def test__process_recover_response_bad_status(self):
975974
assert error.response is response
976975
assert len(error.args) == 4
977976
assert error.args[1] == response.status_code
978-
assert error.args[3] == resumable_media.PERMANENT_REDIRECT
977+
assert error.args[3] == http.client.PERMANENT_REDIRECT
979978
# Make sure still invalid.
980979
assert upload.invalid
981980

@@ -988,7 +987,7 @@ def test__process_recover_response_no_range(self):
988987
upload._bytes_uploaded = mock.sentinel.not_zero
989988
assert upload.bytes_uploaded != 0
990989

991-
response = _make_response(status_code=resumable_media.PERMANENT_REDIRECT)
990+
response = _make_response(status_code=http.client.PERMANENT_REDIRECT)
992991
ret_val = upload._process_recover_response(response)
993992
assert ret_val is None
994993
# Check the state of ``upload`` after.
@@ -1006,7 +1005,7 @@ def test__process_recover_response_bad_range(self):
10061005

10071006
headers = {"range": "bites=9-11"}
10081007
response = _make_response(
1009-
status_code=resumable_media.PERMANENT_REDIRECT, headers=headers
1008+
status_code=http.client.PERMANENT_REDIRECT, headers=headers
10101009
)
10111010
with pytest.raises(common.InvalidResponse) as exc_info:
10121011
upload._process_recover_response(response)
@@ -1032,7 +1031,7 @@ def test__process_recover_response_with_range(self):
10321031
end = 11
10331032
headers = {"range": "bytes=0-{:d}".format(end)}
10341033
response = _make_response(
1035-
status_code=resumable_media.PERMANENT_REDIRECT, headers=headers
1034+
status_code=http.client.PERMANENT_REDIRECT, headers=headers
10361035
)
10371036
ret_val = upload._process_recover_response(response)
10381037
assert ret_val is None

0 commit comments

Comments
 (0)