FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix requirements-txt-fixer reordering --index-url after --extra-index-url (#612) by sudorm-rf0 · Pull Request #1270 · pre-commit/pre-commit-hooks · GitHub

Repository navigation

Fix requirements-txt-fixer reordering --index-url after --extra-index-url (#612) - #1270

Open
sudorm-rf0 wants to merge 1 commit into
pre-commit:mainfrom
sudorm-rf0:fix-index-url-ordering-612
Open

sudorm-rf0 wants to merge 1 commit into
pre-commit:mainfrom
sudorm-rf0:fix-index-url-ordering-612

Conversation

Copy link
Copy Markdown

Summary

Fixes #612 — requirements-txt-fixer sorts requirements alphabetically, which
reorders --index-url after --extra-index-url. That breaks pip: the primary
index URL is silently dropped because pip only honours the first
--index-url it sees.

Before

A requirements.txt written in the correct order:

--index-url https://primary.example.com
--extra-index-url https://extra.example.com
foo==1.0

was "fixed" (reordered) to:

--extra-index-url https://extra.example.com
--index-url https://primary.example.com
foo==1.0

With --extra-index-url first, pip drops the primary index URL.

After

--index-url is now always kept before --extra-index-url, matching pip's
expected semantics.

Root cause

In requirements_txt_fixer.py, each line (including --index-url and
--extra-index-url) is treated as a Requirement and sorted by name via
Requirement.__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-url always
sorts before --extra-index-url. All other ordering (packages, comments,
duplicates) is unchanged.

Tests

Added two cases to tests/requirements_txt_fixer_test.py:

  • --index-url already first → left unchanged (PASS).
  • --extra-index-url first → reordered so --index-url comes first (FAIL,
    file rewritten).

Verification

  • pytest — tests/requirements_txt_fixer_test.py: 33 passed (including the
    two new cases).
  • pre-commit run --files ... — all hooks pass (flake8, mypy, pyupgrade,
    reorder-python-imports, add-trailing-comma, autopep8).

…-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

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


Back | FazBrowse Home | New Git URL