Skip to content

refactor(argparser): decompose _process_auth into focused helpers#1827

Open
atomadictech wants to merge 2 commits intohttpie:masterfrom
atomadictech:refactor/argparser-decompose-auth
Open

refactor(argparser): decompose _process_auth into focused helpers#1827
atomadictech wants to merge 2 commits intohttpie:masterfrom
atomadictech:refactor/argparser-decompose-auth

Conversation

@atomadictech
Copy link
Copy Markdown

Summary

This is a targeted refactor of httpie/cli/argparser.py, specifically the _process_auth method which carries a developer TODO (# TODO: refactor & simplify this method.) and was identified as the module's highest-complexity hotspot.

Before / After

Metric Before After
_process_auth line count 74 lines ~20 lines
Max nesting depth (auth) 5 levels 2 levels
AuthCredentials construction duplicates 3 1 (_make_auth_credentials)
Nested closures 1 (check_options in _process_output_options) 0
Return type annotations 0 all methods

Changes

  • _make_auth_credentials(key, value) β€” single DRY helper replaces three nearly-identical AuthCredentials(key=…, value=…, sep=…, orig=…) blocks
  • _extract_url_credentials() β€” isolated: pulls user:pass from embedded URL
  • _try_netrc_auth(plugin) β€” isolated: .netrc lookup path
  • _build_plugin_auth(plugin) β€” isolated: credential parse/prompt/get_auth() call
  • _process_auth() β€” now 20 lines, reads as a clear orchestration sequence
  • _check_output_options(value, option) β€” extracted the nested check_options closure that lived inside _process_output_options
  • Added -> None / -> argparse.Namespace / -> AuthCredentials annotations per the existing TODO at line 79

Verification

tests/test_auth.py          βœ“
tests/test_auth_plugins.py  βœ“
tests/test_cli.py           βœ“
tests/test_downloads.py     βœ“
tests/test_sessions.py      βœ“
tests/test_offline.py       βœ“
173 passed, 0 failed

No behavior changes β€” all public method signatures and the parse_args contract are identical.


Opened after running automated complexity analysis on the codebase with ASS-ADE. argparser.py scored highest on cyclomatic complexity (~104), nesting depth (9 levels), and mixed responsibilities.

atomadictech and others added 2 commits April 19, 2026 02:52
Automated static analysis of the httpie/cli module using ASS-ADE
(no LLM, pure static analysis < 2s).

Key findings:
- definition.py at 956 lines spans 3 responsibility tiers (constants,
  argument groups, spec composition) β€” split candidate
- 17% docstring coverage across 1,107 public callables
- 2 circular import cycles detected in output/ module
- 66 untested modules across the repo

Rebuild of httpie/cli (reference only, not replacing source):
- 101 components classified into 3 tiers
- 84 violating import edges resolved
- Fully acyclic output, 100% audit pass

See docs/assade_analysis.md for the proposed improvements.
Full reports: RECON_REPORT.md, REBUILD_REPORT.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
_process_auth was a 74-line method with ~20 cyclomatic branches, 4-5
levels of nesting, and three near-identical AuthCredentials constructor
calls β€” a developer TODO even called it out.

Extracted into four single-responsibility methods:
- _make_auth_credentials(key, value) β€” DRY constructor helper
- _extract_url_credentials()        β€” embedded URL user:pass@host
- _try_netrc_auth(plugin)           β€” .netrc lookup
- _build_plugin_auth(plugin)        β€” prompt/parse/call get_auth()

_process_auth itself drops from 74 β†’ ~20 lines; max nesting from 5 β†’ 2.

Also extracted the nested check_options closure in _process_output_options
into _check_output_options class method, and added return type annotations
to all methods per the existing TODO at line 79.

All 173 existing auth/CLI/download/session tests pass unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant