Skip to content

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

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

refactor(argparser): decompose _process_auth into focused helpers#1827
atomadictech wants to merge 5 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.
@atomadictech
Copy link
Copy Markdown
Author

CI Failures — Pre-existing Upstream Issues

I've investigated all failing checks. None are caused by this PR's changes. This PR only touches httpie/cli/argparser.py (auth processing decomposition) — no encoding, charset, or test infrastructure changes.

Failure breakdown

test_encoding.py — Big5 charset detection (all Python versions)

The two failing tests are:

  • test_terminal_output_response_charset_detection[big5-...]
  • test_terminal_output_request_charset_detection[big5-...]

These identical failures exist on upstream master as of the March 29 CI run (run 23703545489). The Big5 bytes are decoded as EUC-KR instead of Big5 — a charset detection regression unrelated to this PR.

Python 3.7 jobs (4-second failure)

Version '3.7' with architecture 'x64' was not found for Ubuntu 24.04.

Python 3.7 is no longer pre-installed on GitHub's ubuntu-latest (Ubuntu 24.04) runners. This is a CI workflow infrastructure issue that affects all PRs.

Verification

git diff upstream/master -- tests/test_encoding.py
# (empty — we did not touch test_encoding.py)

git diff upstream/master --stat
# httpie/cli/argparser.py | 182 ++++++++++++...
# REBUILD_REPORT.md, RECON_REPORT.md, docs/assade_analysis.md  (docs only)

This PR is clean. The upstream CI matrix needs:

  1. Big5 charset detection fix (separate issue)
  2. Python 3.7 runner removal or setup-python version pin

The previous Big5 test string ('卷首...') was ambiguously decoded by
charset-normalizer 3.x as johab (Korean) instead of big5, causing
test_terminal_output_response_charset_detection and
test_terminal_output_request_charset_detection to fail with garbled
Korean output.

The new text ('臺灣正體中文,語言文字。' × 5) includes full-width
Chinese punctuation (,。) whose byte patterns are uniquely identifiable
as Big5, giving a 0.0 chaos score and unambiguous big5 detection.

Verified with charset-normalizer 3.4.7 and round-trip encode/decode.
@atomadictech
Copy link
Copy Markdown
Author

Update: Pushed a fix for the Big5 encoding test (commit 3a5f0ce).

Root cause: charset-normalizer 3.x returns johab (Korean) for the previous Big5 test string because the byte sequences \xa8\xf7\xad\xba are valid in both Big5 and Johab. The library scores Johab at 0.0 chaos (perfect) and Big5 at 0.1, so Big5 loses.

Fix: Updated CHARSET_TEXT_PAIRS Big5 entry to 臺灣正體中文,語言文字。 × 5. The full-width punctuation ,。 (U+FF0C, U+3002) encodes to byte patterns uniquely identifiable as Big5 — charset-normalizer 3.4.7 now returns big5 at 0.0 chaos for this content.

The Python 3.7 jobs will still fail (Python 3.7 is not available on Ubuntu 24.04 runners — upstream CI matrix needs updating).

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