Fix requirements-txt-fixer reordering --index-url after --extra-index-url (#612) - #1270
Open
sudorm-rf0 wants to merge 1 commit into
Open
Fix requirements-txt-fixer reordering --index-url after --extra-index-url (#612)#1270sudorm-rf0 wants to merge 1 commit into
sudorm-rf0 wants to merge 1 commit into
Conversation
…-url (pre-commit#612) pip uses the first --index-url as the primary index and treats later --extra-index-url entries as additional indexes. If --extra-index-url sorts before --index-url (alphabetical order), the primary index URL is silently dropped. Keep --index-url ordered first. Fixes pre-commit#612.
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.
Summary
Fixes #612 —
requirements-txt-fixersorts requirements alphabetically, whichreorders
--index-urlafter--extra-index-url. That breaks pip: the primaryindex URL is silently dropped because pip only honours the first
--index-urlit sees.Before
A
requirements.txtwritten in the correct order:was "fixed" (reordered) to:
With
--extra-index-urlfirst, pip drops the primary index URL.After
--index-urlis now always kept before--extra-index-url, matching pip'sexpected semantics.
Root cause
In
requirements_txt_fixer.py, each line (including--index-urland--extra-index-url) is treated as aRequirementand sorted bynameviaRequirement.__lt__. Since'--extra-index-url' < '--index-url'alphabetically, the extra index ended up first.
Fix
Special-case the two options in
Requirement.__lt__so--index-urlalwayssorts before
--extra-index-url. All other ordering (packages, comments,duplicates) is unchanged.
Tests
Added two cases to
tests/requirements_txt_fixer_test.py:--index-urlalready first → left unchanged (PASS).--extra-index-urlfirst → reordered so--index-urlcomes first (FAIL,file rewritten).
Verification
pytest—tests/requirements_txt_fixer_test.py: 33 passed (including thetwo new cases).
pre-commit run --files ...— all hooks pass (flake8, mypy, pyupgrade,reorder-python-imports, add-trailing-comma, autopep8).