Skip to content

🧹 Simplify deeply nested logic in _retry_request#807

Open
abhimehro wants to merge 1 commit into
mainfrom
fix-retry-request-nesting
Open

🧹 Simplify deeply nested logic in _retry_request#807
abhimehro wants to merge 1 commit into
mainfrom
fix-retry-request-nesting

Conversation

@abhimehro
Copy link
Copy Markdown
Owner

@abhimehro abhimehro commented May 14, 2026

🎯 What: Simplified deeply nested error handling logic within _retry_request in api_client.py by extracting repetitive code into helper functions _log_debug_response and _get_retry_after_seconds.
💡 Why: This reduces cognitive complexity and cyclomatic complexity of _retry_request, improving readability and maintainability without altering existing retry and logging functionality.
Verification: Verified the logic structurally. Formatted and checked the module with ruff. Ran the full test suite (uv run pytest) and pre-commit hooks to confirm no regressions or formatting issues were introduced.
Result: A cleaner, flatter retry loop that relies on modular, specific helper functions.


Open in Devin Review

Copilot AI review requested due to automatic review settings May 14, 2026 15:19
@trunk-io
Copy link
Copy Markdown

trunk-io Bot commented May 14, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@cursor
Copy link
Copy Markdown

cursor Bot commented May 14, 2026

PR Summary

Low Risk
Low risk refactor limited to _retry_request control flow; behavior should be unchanged but could subtly affect 429 wait handling or debug logging if the helper conditions differ.

Overview
Refactors api_client._retry_request to reduce nested error-handling by extracting two helpers: _get_retry_after_seconds() for parsing Retry-After on 429 responses and _log_debug_response() for conditional debug logging of sanitized response bodies.

This keeps the existing retry/backoff behavior but centralizes the repeated parsing/logging logic to improve readability and maintainability.

Reviewed by Cursor Bugbot for commit 330c7b8. Configure here.

Copy link
Copy Markdown

@codescene-delta-analysis codescene-delta-analysis Bot left a comment

Choose a reason for hiding this comment

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

Code Health Improved (1 files improve in Code Health)

Gates Passed
6 Quality Gates Passed

See analysis details in CodeScene

View Improvements
File Code Health Impact Categories Improved
api_client.py 7.43 → 7.47 Complex Method, Complex Conditional, Overall Code Complexity, Deep, Nested Complexity

Quality Gate Profile: Pay Down Tech Debt
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.

Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

Open in Devin Review

Comment thread api_client.py
retry_after = response.headers.get("Retry-After")
if not retry_after:
return None
import contextlib
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 Info: Redundant import contextlib inside _get_retry_after_seconds

contextlib is already imported at module level (api_client.py:23), so the local import contextlib at line 233 is unnecessary. Python caches module imports so there's no correctness issue, but it's inconsistent with the rest of the file (e.g., _extract_int_header at api_client.py:119 uses the top-level import directly). This appears to be a leftover from copy-paste or an attempt at lazy importing that doesn't apply here.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread api_client.py
and log.isEnabledFor(logging.DEBUG)
):
log.debug(f"Response content: {_sanitize_fn(e.response.text)}")
_log_debug_response(e)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 Info: Inconsistent use of _log_debug_response helper — only applied to one of two identical patterns

The new _log_debug_response helper replaces the inline debug-logging pattern at the 4xx error branch (line 302), but the identical pattern at the max-retries exhaustion branch (api_client.py:306-311) was left as inline code. Both perform the same hasattr/getattr/isEnabledFor check and log.debug call. Using the helper in both places would improve consistency and reduce duplication. Not a bug since behavior is preserved, but a missed opportunity in the refactoring.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants