Skip to content

Improve follow_redirect() so that it has a singular checkpoint for limiting number of attempts and clearer logic#1634

Merged
yarikoptic merged 1 commit intodandi:masterfrom
candleindark:improve-follow_redirect
Jun 17, 2025
Merged

Improve follow_redirect() so that it has a singular checkpoint for limiting number of attempts and clearer logic#1634
yarikoptic merged 1 commit intodandi:masterfrom
candleindark:improve-follow_redirect

Conversation

@candleindark
Copy link
Copy Markdown
Member

@candleindark candleindark commented May 22, 2025

This PR is a follow up to #1632. It further improves follow_redirect() by implementing more clear logic. Particularly it uses singular a checkpoint for limiting number of attempts . Please see the commit message for more details.

@candleindark candleindark requested a review from Copilot May 22, 2025 19:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the follow_redirect() function to improve clarity in its retry logic and error handling.

  • Updates exception handling to raise FailedToConnectError with more detailed messages.
  • Adjusts test expectations in dandi/tests/test_dandiarchive.py to match the new error types and messages.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
dandi/tests/test_dandiarchive.py Updates the expected exception and regex for the FailedToConnectError.
dandi/dandiarchive.py Refines the retry logic and error messaging in follow_redirect().
Comments suppressed due to low confidence (2)

dandi/dandiarchive.py:897

  • [nitpick] Consider renaming the loop variable 'i' to a more descriptive name like 'attempt' to improve code clarity.
for i in range(retries):

dandi/tests/test_dandiarchive.py:446

  • Ensure that the regex pattern in the test fully captures both variants of the error message produced in follow_redirect(), considering the additional status code details now included.
with pytest.raises(FailedToConnectError, match=r"failed with \d+ attempts"):

@codecov
Copy link
Copy Markdown

codecov bot commented May 22, 2025

Codecov Report

❌ Patch coverage is 76.92308% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.84%. Comparing base (6d30523) to head (22910d8).
⚠️ Report is 108 commits behind head on master.

Files with missing lines Patch % Lines
dandi/dandiarchive.py 75.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1634      +/-   ##
==========================================
+ Coverage   88.77%   88.84%   +0.07%     
==========================================
  Files          82       82              
  Lines       11434    11439       +5     
==========================================
+ Hits        10150    10163      +13     
+ Misses       1284     1276       -8     
Flag Coverage Δ
unittests 88.84% <76.92%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@candleindark
Copy link
Copy Markdown
Member Author

@yarikoptic Should I add a patch label to this PR. It is mostly refactoring.

@candleindark candleindark requested a review from yarikoptic May 22, 2025 20:21
@candleindark candleindark marked this pull request as ready for review May 22, 2025 20:21
@yarikoptic yarikoptic added the internal Changes only affect the internal API label May 22, 2025
@yarikoptic
Copy link
Copy Markdown
Member

thanks, will review later. If you could make title more descriptive (as to why/what reorganization brings) - would be good since it would be included in the changelog at the end.

@yarikoptic yarikoptic added the enhancement New feature or request label May 22, 2025
@candleindark candleindark changed the title Reorganize follow_redirect() Improve follow_redirect() so that it has a singular checkpoint for limiting number of attempts May 22, 2025
@candleindark candleindark changed the title Improve follow_redirect() so that it has a singular checkpoint for limiting number of attempts Improve follow_redirect() so that it has a singular checkpoint for limiting number of attempts and clearer logic May 22, 2025
@candleindark candleindark force-pushed the improve-follow_redirect branch from 452b721 to 71b4f90 Compare May 22, 2025 22:07
Overall, this commit rewrites `follow_redirect()`
to bring the following improvements.
1. Make logic more clear
2. Consolidate checking of attempt index with
   max allow attempts into one single point.
   Eliminating the checks of `retry < retries`
   inside the `for retry in range(retries + 1)`
3. move code that doesn't need to be repeated
   out of the for-loop.
4. Repackage the `requests.ConnectionError` into
   a `FailedToConnectError` to offer better
   context
5. Rename `retries` in to `max_attempts` to
   better convey the purpose of the value
6. Correct the docstring to indicate that
   `FailedToConnectError` can be raised when
   number of allowed attempts have been exhausted.
@candleindark candleindark force-pushed the improve-follow_redirect branch from 71b4f90 to 22910d8 Compare May 22, 2025 23:59
@yarikoptic yarikoptic merged commit 03b7c07 into dandi:master Jun 17, 2025
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request internal Changes only affect the internal API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants