Fix AssertionError when METHOD appears before options#1675
Open
ns-fboucault wants to merge 1 commit intohttpie:masterfrom
Open
Fix AssertionError when METHOD appears before options#1675ns-fboucault wants to merge 1 commit intohttpie:masterfrom
ns-fboucault wants to merge 1 commit intohttpie:masterfrom
Conversation
Fixes httpie#1614 When the HTTP method (POST, GET, etc.) was specified before optional arguments like --auth-type, argparse's nargs=OPTIONAL behavior caused incorrect argument parsing, resulting in an AssertionError. Example failing command: http POST --auth-type bearer --auth TOKEN https://example.org Argparse incorrectly parsed this as: - method=None - url='POST' - URL and request items ended up in unparsed arguments This commit adds a new _fix_argument_order() method that: 1. Detects when arguments were misparsed 2. Correctly reassigns method, url, and request_items 3. Validates all request items atomically before adding them 4. Properly initializes request_items if None The fix runs before _apply_no_options() to prevent errors from unrecognized arguments and maintains backward compatibility with existing usage patterns. Tests added: - test_fix_argument_order_method_before_options - test_fix_argument_order_method_before_options_with_items - test_bearer_auth_method_before_options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1614
Problem
When the HTTP method (POST, GET, etc.) was specified before optional arguments like
--auth-type, anAssertionErrorwas raised due to incorrect argument parsing.Example failing command:
Root Cause
Argparse's
nargs=OPTIONALfor the METHOD argument doesn't work well with intermixed arguments. When METHOD appears before optional flags, argparse incorrectly parsed:method=Noneurl='POST'(the method string)The assertion at line 416 in
_guess_method()failed because it expected norequest_itemswhenmethodisNone.Solution
Added a new
_fix_argument_order()method that:request_itemsif it'sNone_apply_no_options()to prevent errors from unrecognized argumentsChanges Made
_fix_argument_order()method (48 lines)Testing
✅ All 71 existing tests pass
✅ New tests verify the fix works correctly
✅ Tested with original failing command from issue #1614
✅ Backward compatibility maintained
Additional Notes
The fix handles two important edge cases:
request_itemsto[]ifNonebefore extendingExample Commands Now Working