Skip to content

fix: replace bare assert with user-friendly error in _guess_method#1805

Closed
funsaized wants to merge 2 commits intohttpie:masterfrom
funsaized:fix/assert-error-in-guess-method
Closed

fix: replace bare assert with user-friendly error in _guess_method#1805
funsaized wants to merge 2 commits intohttpie:masterfrom
funsaized:fix/assert-error-in-guess-method

Conversation

@funsaized
Copy link
Copy Markdown

Summary

Fixes #1614

When _guess_method() encounters a state where args.method is None but args.request_items is non-empty, it crashes with a bare AssertionError. This can happen when users place optional flags between METHOD and URL (e.g. http POST --auth-type bearer --auth token URL), causing Python's argparse to misassign positional arguments — particularly on Python 3.13+.

Changes

  • httpie/cli/argparser.py: Replace assert not self.args.request_items with a self.error() call that prints the correct usage syntax and a link to the docs.
  • tests/test_cli.py: Add test_guess_when_method_not_set_but_request_items_present covering the previously-crashing code path.

Before

AssertionError

After

error:
    no HTTP method or URL detected but request items found.
    Please make sure that the URL comes right after the optional METHOD:
        http [METHOD] URL [REQUEST_ITEM ...]
    See https://httpie.io/docs/cli for more information.

- test_cli_ui: argparse in Python 3.12.4+ no longer quotes choices in
  error messages. Detect quoting behavior at runtime so the test passes
  on both old and new Python versions.
- test_encoding: replace repetitive big5 text with varied Traditional
  Chinese text so charset_normalizer can reliably distinguish big5 from
  other CJK encodings (johab/euc-kr).
@funsaized funsaized closed this Apr 5, 2026
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.

AssertionError when trying to POST with bearer auth

1 participant