Skip to content

Commit 918dc64

Browse files
author
Louis Shawn
committed
fix(requirements-txt-fixer): keep --index-url before --extra-index-url
1 parent f1dff44 commit 918dc64

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pre_commit_hooks/requirements_txt_fixer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
class Requirement:
1414
UNTIL_COMPARISON = re.compile(b'={2,3}|!=|~=|>=?|<=?')
1515
UNTIL_SEP = re.compile(rb'[^;\s]+')
16+
_SPECIAL_ORDER = {
17+
b'--index-url': 0,
18+
b'--extra-index-url': 1,
19+
}
1620

1721
def __init__(self) -> None:
1822
self.value: bytes | None = None
@@ -50,7 +54,13 @@ def __lt__(self, requirement: Requirement) -> bool:
5054
# with comments is kept)
5155
if self.name == requirement.name:
5256
return bool(self.comments) > bool(requirement.comments)
53-
return self.name < requirement.name
57+
return (
58+
self._SPECIAL_ORDER.get(self.name, 2),
59+
self.name,
60+
) < (
61+
self._SPECIAL_ORDER.get(requirement.name, 2),
62+
requirement.name,
63+
)
5464

5565
def is_complete(self) -> bool:
5666
return (

tests/requirements_txt_fixer_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@
107107
PASS,
108108
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
109109
),
110+
(
111+
b'--extra-index-url https://example.com/simple\n'
112+
b'--index-url https://pypi.org/simple\n'
113+
b'requests\n',
114+
FAIL,
115+
b'--index-url https://pypi.org/simple\n'
116+
b'--extra-index-url https://example.com/simple\n'
117+
b'requests\n',
118+
),
110119
),
111120
)
112121
def test_integration(input_s, expected_retval, output, tmpdir):

0 commit comments

Comments
 (0)