Apply typing to all of pre-commit-hooks

This commit is contained in:
Anthony Sottile 2019-01-31 19:19:10 -08:00
parent 63cc3414e9
commit 030bfac7e4
54 changed files with 401 additions and 264 deletions

View file

@ -1,6 +1,10 @@
from __future__ import print_function
import argparse
from typing import IO
from typing import List
from typing import Optional
from typing import Sequence
PASS = 0
@ -9,21 +13,23 @@ FAIL = 1
class Requirement(object):
def __init__(self):
def __init__(self): # type: () -> None
super(Requirement, self).__init__()
self.value = None
self.comments = []
self.value = None # type: Optional[bytes]
self.comments = [] # type: List[bytes]
@property
def name(self):
def name(self): # type: () -> bytes
assert self.value is not None, self.value
if self.value.startswith(b'-e '):
return self.value.lower().partition(b'=')[-1]
return self.value.lower().partition(b'==')[0]
def __lt__(self, requirement):
def __lt__(self, requirement): # type: (Requirement) -> int
# \n means top of file comment, so always return True,
# otherwise just do a string comparison with value.
assert self.value is not None, self.value
if self.value == b'\n':
return True
elif requirement.value == b'\n':
@ -32,10 +38,10 @@ class Requirement(object):
return self.name < requirement.name
def fix_requirements(f):
requirements = []
def fix_requirements(f): # type: (IO[bytes]) -> int
requirements = [] # type: List[Requirement]
before = tuple(f)
after = []
after = [] # type: List[bytes]
before_string = b''.join(before)
@ -46,6 +52,7 @@ def fix_requirements(f):
for line in before:
# If the most recent requirement object has a value, then it's
# time to start building the next requirement object.
if not len(requirements) or requirements[-1].value is not None:
requirements.append(Requirement())
@ -78,6 +85,7 @@ def fix_requirements(f):
for requirement in sorted(requirements):
after.extend(requirement.comments)
assert requirement.value, requirement.value
after.append(requirement.value)
after.extend(rest)
@ -92,7 +100,7 @@ def fix_requirements(f):
return FAIL
def fix_requirements_txt(argv=None):
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
args = parser.parse_args(argv)
@ -109,3 +117,7 @@ def fix_requirements_txt(argv=None):
retv |= ret_for_file
return retv
if __name__ == '__main__':
exit(main())