Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse more operators in requirements #483

Merged
merged 1 commit into from
May 18, 2020
Merged
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
17 changes: 15 additions & 2 deletions pre_commit_hooks/requirements_txt_fixer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import re
from typing import IO
from typing import List
from typing import Optional
Expand All @@ -10,18 +11,30 @@


class Requirement:
UNTIL_COMPARISON = re.compile(b'={2,3}|!=|~=|>=?|<=?')
asottile marked this conversation as resolved.
Show resolved Hide resolved
UNTIL_SEP = re.compile(rb'[^;\s]+')

def __init__(self) -> None:
self.value: Optional[bytes] = None
self.comments: List[bytes] = []

@property
def name(self) -> bytes:
assert self.value is not None, self.value
name = self.value.lower()
for egg in (b'#egg=', b'&egg='):
if egg in self.value:
return self.value.lower().partition(egg)[-1]
return name.partition(egg)[-1]

m = self.UNTIL_SEP.match(name)
assert m is not None

name = m.group()
m = self.UNTIL_COMPARISON.search(name)
if not m:
return name

return self.value.lower().partition(b'==')[0]
return name[:m.start()]

def __lt__(self, requirement: 'Requirement') -> int:
# \n means top of file comment, so always return True,
Expand Down
25 changes: 22 additions & 3 deletions tests/requirements_txt_fixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,28 @@
(b'\nfoo\nbar\n', FAIL, b'bar\n\nfoo\n'),
(b'\nbar\nfoo\n', PASS, b'\nbar\nfoo\n'),
(
b'pyramid==1\npyramid-foo==2\n',
PASS,
b'pyramid==1\npyramid-foo==2\n',
b'pyramid-foo==1\npyramid>=2\n',
FAIL,
b'pyramid>=2\npyramid-foo==1\n',
),
(
b'a==1\n'
b'c>=1\n'
b'bbbb!=1\n'
b'c-a>=1;python_version>="3.6"\n'
b'e>=2\n'
b'd>2\n'
b'g<2\n'
b'f<=2\n',
FAIL,
b'a==1\n'
b'bbbb!=1\n'
b'c>=1\n'
b'c-a>=1;python_version>="3.6"\n'
b'd>2\n'
b'e>=2\n'
b'f<=2\n'
b'g<2\n',
),
(b'ocflib\nDjango\nPyMySQL\n', FAIL, b'Django\nocflib\nPyMySQL\n'),
(
Expand Down