← 返回首页
Fix requirements-txt-fixer reordering pip options by Bahtya · Pull Request #1249 · pre-commit/pre-commit-hooks · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Fix requirements-txt-fixer reordering pip options#1249

Closed
Bahtya wants to merge 1 commit into
pre-commit:mainfrom
Bahtya:fix-requirements-txt-fixer-pip-options-ordering
Closed

Fix requirements-txt-fixer reordering pip options#1249
Bahtya wants to merge 1 commit into
pre-commit:mainfrom
Bahtya:fix-requirements-txt-fixer-pip-options-ordering

Conversation

Copy link
Copy Markdown

Bahtya commented Apr 9, 2026

Summary

Fixes #612

The requirements-txt-fixer hook was alphabetically sorting all lines in requirements.txt, including pip options like --index-url and --extra-index-url. This caused --extra-index-url to be sorted above --index-url (since "extra" < "index" alphabetically), which breaks pip's index resolution order.

Changes

  • pre_commit_hooks/requirements_txt_fixer.py: Added a check in Requirement.__lt__ so that when both requirements being compared start with -- (i.e., are pip options), they maintain their original relative order instead of being sorted alphabetically. Regular package requirements continue to be sorted alphabetically.

  • tests/requirements_txt_fixer_test.py: Added 3 test cases:

    • --index-url before --extra-index-url stays in order (PASS)
    • --extra-index-url before --index-url stays in order (PASS, original order preserved)
    • Packages are still sorted while pip options remain in place (FAIL triggers sort, pip options unchanged)

Test plan

  • All 416 existing tests pass
  • 3 new test cases cover the reported bug
  • Verified that --index-url / --extra-index-url ordering is preserved
  • Verified that regular package sorting still works correctly alongside pip options

Bahtya

The requirements-txt-fixer was alphabetically sorting all lines, including pip options like --index-url and --extra-index-url. This caused --extra-index-url to be sorted above --index-url, breaking pip's index resolution order since --index-url must come first. Fix by preserving the original relative order of lines that start with `--` (pip options), while still sorting regular package requirements alphabetically. Closes #612 Bahtya
Copy link
Copy Markdown
Member

asottile commented Apr 9, 2026

ai slop prs are not acceptable

asottile closed this Apr 9, 2026
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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

requirements-txt-fixer ordering breaks using --index-url and --extra-index-url together in requirements.txt

2 participants

Footer

© 2026 GitHub, Inc.