From 918dc64fe79293d7de21d9bd450fb7b8f922b533 Mon Sep 17 00:00:00 2001 From: Louis Shawn Date: Thu, 5 Feb 2026 21:09:30 +0800 Subject: [PATCH] fix(requirements-txt-fixer): keep --index-url before --extra-index-url --- pre_commit_hooks/requirements_txt_fixer.py | 12 +++++++++++- tests/requirements_txt_fixer_test.py | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 8ce8ec64..7849767c 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -13,6 +13,10 @@ class Requirement: UNTIL_COMPARISON = re.compile(b'={2,3}|!=|~=|>=?|<=?') UNTIL_SEP = re.compile(rb'[^;\s]+') + _SPECIAL_ORDER = { + b'--index-url': 0, + b'--extra-index-url': 1, + } def __init__(self) -> None: self.value: bytes | None = None @@ -50,7 +54,13 @@ def __lt__(self, requirement: Requirement) -> bool: # with comments is kept) if self.name == requirement.name: return bool(self.comments) > bool(requirement.comments) - return self.name < requirement.name + return ( + self._SPECIAL_ORDER.get(self.name, 2), + self.name, + ) < ( + self._SPECIAL_ORDER.get(requirement.name, 2), + requirement.name, + ) def is_complete(self) -> bool: return ( diff --git a/tests/requirements_txt_fixer_test.py b/tests/requirements_txt_fixer_test.py index c0d2c65d..bb75daaf 100644 --- a/tests/requirements_txt_fixer_test.py +++ b/tests/requirements_txt_fixer_test.py @@ -107,6 +107,15 @@ PASS, b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n', ), + ( + b'--extra-index-url https://example.com/simple\n' + b'--index-url https://pypi.org/simple\n' + b'requests\n', + FAIL, + b'--index-url https://pypi.org/simple\n' + b'--extra-index-url https://example.com/simple\n' + b'requests\n', + ), ), ) def test_integration(input_s, expected_retval, output, tmpdir):