lint: Make custom_rules.py pass all lint checks.

The functions here weren't checked by all linters when they were in
 `custom_check.py`. Hence, modified to pass all lint checks.
This commit is contained in:
Aman Agrawal
2019-05-29 09:14:54 +05:30
committed by Tim Abbott
parent 90c3578a88
commit a1fadc28ba

View File

@@ -8,13 +8,15 @@ import traceback
from zulint.printer import print_err from zulint.printer import print_err
from typing import Any, Dict, List, Optional, Tuple, Iterable if False:
from typing import Any, Dict, List, Optional, Tuple, Iterable
Rule = Dict[str, Any] Rule = Dict[str, Any]
RuleList = List[Dict[str, Any]] RuleList = List[Dict[str, Any]]
LineTup = Tuple[int, str, str, str] LineTup = Tuple[int, str, str, str]
def get_line_info_from_file(fn: str) -> List[LineTup]: def get_line_info_from_file(fn):
# type: (str) -> List[LineTup]
line_tups = [] line_tups = []
for i, line in enumerate(open(fn)): for i, line in enumerate(open(fn)):
line_newline_stripped = line.strip('\n') line_newline_stripped = line.strip('\n')
@@ -25,7 +27,8 @@ def get_line_info_from_file(fn: str) -> List[LineTup]:
line_tups.append(tup) line_tups.append(tup)
return line_tups return line_tups
def get_rules_applying_to_fn(fn: str, rules: RuleList) -> RuleList: def get_rules_applying_to_fn(fn, rules):
# type: (str, RuleList) -> RuleList
rules_to_apply = [] rules_to_apply = []
for rule in rules: for rule in rules:
excluded = False excluded = False
@@ -46,11 +49,12 @@ def get_rules_applying_to_fn(fn: str, rules: RuleList) -> RuleList:
return rules_to_apply return rules_to_apply
def check_file_for_pattern(fn: str, def check_file_for_pattern(fn,
line_tups: List[LineTup], line_tups,
identifier: str, identifier,
color: Optional[Iterable[str]], color,
rule: Rule) -> bool: rule):
# type: (str, List[LineTup], str, Optional[Iterable[str]], Rule) -> bool
''' '''
DO NOT MODIFY THIS FUNCTION WITHOUT PROFILING. DO NOT MODIFY THIS FUNCTION WITHOUT PROFILING.
@@ -103,9 +107,10 @@ def check_file_for_pattern(fn: str,
return ok return ok
def check_file_for_long_lines(fn: str, def check_file_for_long_lines(fn,
max_length: int, max_length,
line_tups: List[LineTup]) -> bool: line_tups):
# type: (str, int, List[LineTup]) -> bool
ok = True ok = True
for (i, line, line_newline_stripped, line_fully_stripped) in line_tups: for (i, line, line_newline_stripped, line_fully_stripped) in line_tups:
if isinstance(line, bytes): if isinstance(line, bytes):
@@ -124,11 +129,12 @@ def check_file_for_long_lines(fn: str,
ok = False ok = False
return ok return ok
def custom_check_file(fn: str, def custom_check_file(fn,
identifier: str, identifier,
rules: RuleList, rules,
color: Optional[Iterable[str]], color,
max_length: Optional[int]=None) -> bool: max_length=None):
# type: (str, str, RuleList, Optional[Iterable[str]], Optional[int]) -> bool
failed = False failed = False
line_tups = get_line_info_from_file(fn=fn) line_tups = get_line_info_from_file(fn=fn)
@@ -171,7 +177,8 @@ def custom_check_file(fn: str,
# that NixOS provides at a fixed path (outside a # that NixOS provides at a fixed path (outside a
# buildFHSUserEnv sandbox). # buildFHSUserEnv sandbox).
{'pattern': '^#!(?! *(?:/usr/bin/env|/bin/sh)(?: |$))', {'pattern': '^#!(?! *(?:/usr/bin/env|/bin/sh)(?: |$))',
'description': "Use `#!/usr/bin/env foo` instead of `#!/path/foo` for interpreters other than sh."}, 'description': "Use `#!/usr/bin/env foo` instead of `#!/path/foo`"
" for interpreters other than sh."},
{'pattern': '^#!/usr/bin/env python$', {'pattern': '^#!/usr/bin/env python$',
'description': "Use `#!/usr/bin/env python3` instead of `#!/usr/bin/env python`."} 'description': "Use `#!/usr/bin/env python3` instead of `#!/usr/bin/env python`."}
] # type: RuleList ] # type: RuleList