Skip to content

Commit

Permalink
Make code cleaner and more Pythonic (use None, not "-1")
Browse files Browse the repository at this point in the history
Co-authored-by: John T. Wodder II <[email protected]>
  • Loading branch information
yarikoptic and jwodder authored Feb 3, 2025
1 parent 6c9fa51 commit b4b2ea1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dandi/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ def _check_attempts_and_sleep(
"""Check if we should retry the download, sleep if still allowed,
and return potentially adjusted 'attempts_allowed'
"""
sleep_amount: float = -1.0
sleep_amount: float | None = None
if os.environ.get("DANDI_DOWNLOAD_AGGRESSIVE_RETRY"):
# in such a case if we downloaded a little more --
# consider it a successful attempt
Expand Down Expand Up @@ -1133,7 +1133,7 @@ def _check_attempts_and_sleep(
sleep_amount,
exc,
)
if sleep_amount < 0:
if sleep_amount is None:
# it was not Retry-after set, so we come up with random duration to sleep
sleep_amount = random.random() * 5 * attempt
lgr.debug(
Expand Down
6 changes: 3 additions & 3 deletions dandi/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ def test_DownloadDirectory_exc(
assert dl.writefile.read_bytes() == b"456"


def test__check_if_more_attempts_allowed():
def test__check_attempts_and_sleep() -> None:
f = partial(_check_attempts_and_sleep, "some/path")

response403 = requests.Response()
Expand Down Expand Up @@ -1228,7 +1228,7 @@ def test__check_if_more_attempts_allowed():


@pytest.mark.parametrize("status_code", [429, 503])
def test__check_if_more_attempts_allowed_retries(status_code):
def test__check_attempts_and_sleep_retries(status_code: int) -> None:
f = partial(_check_attempts_and_sleep, "some/path")

response = requests.Response()
Expand All @@ -1253,7 +1253,7 @@ def test__check_if_more_attempts_allowed_retries(status_code):
assert mock_sleep.call_args.args[0] == 120

# we would still sleep some time if Retry-After is not decypherable
response.headers["Retry-After"] = "nondecypherable"
response.headers["Retry-After"] = "indecipherable"
with mock.patch("time.sleep") as mock_sleep:
assert (
_check_attempts_and_sleep(
Expand Down

0 comments on commit b4b2ea1

Please sign in to comment.