Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pre_commit_hooks/requirements_txt_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down
9 changes: 9 additions & 0 deletions tests/requirements_txt_fixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down