feat: Refactor timeout handling in HTTPAdapter with new _prepare_timeout method #7062
+113
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors timeout handling in the
HTTPAdapterclass by extracting the timeout conversion logic into a dedicated_prepare_timeoutmethod. This improves code organization, maintainability, and makes the timeout handling logic reusable for subclasses.Changes Made
Code Refactoring
New Method: Added
_prepare_timeout()method toHTTPAdapterclass insrc/requests/adapters.pyImproved Error Handling: Enhanced timeout validation with exception chaining
raise ... from excto preserve the original ValueError contextCode Simplification: Refactored the
send()method to use the new_prepare_timeout()methodTesting
tests/test_adapters.pyFiles Changed
src/requests/adapters.py(+25, -13 lines)tests/test_adapters.py(+88 lines)Benefits
_prepare_timeout()for custom timeout handlingBackward Compatibility
✅ This change is fully backward compatible. The public API remains unchanged; this is purely an internal refactoring that improves code quality without affecting existing functionality.