pre-commit-hooks: python3.6+

This commit is contained in:
Anthony Sottile
2020-02-05 11:10:42 -08:00
parent 551d1a07b3
commit f5c42a050b
60 changed files with 291 additions and 493 deletions

View File

@ -1,35 +1,38 @@
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import ast
import collections
import traceback
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Sequence
DEBUG_STATEMENTS = {'pdb', 'ipdb', 'pudb', 'q', 'rdb', 'rpdb'}
Debug = collections.namedtuple('Debug', ('line', 'col', 'name', 'reason'))
class Debug(NamedTuple):
line: int
col: int
name: str
reason: str
class DebugStatementParser(ast.NodeVisitor):
def __init__(self): # type: () -> None
self.breakpoints = [] # type: List[Debug]
def __init__(self) -> None:
self.breakpoints: List[Debug] = []
def visit_Import(self, node): # type: (ast.Import) -> None
def visit_Import(self, node: ast.Import) -> None:
for name in node.names:
if name.name in DEBUG_STATEMENTS:
st = Debug(node.lineno, node.col_offset, name.name, 'imported')
self.breakpoints.append(st)
def visit_ImportFrom(self, node): # type: (ast.ImportFrom) -> None
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
if node.module in DEBUG_STATEMENTS:
st = Debug(node.lineno, node.col_offset, node.module, 'imported')
self.breakpoints.append(st)
def visit_Call(self, node): # type: (ast.Call) -> None
def visit_Call(self, node: ast.Call) -> None:
"""python3.7+ breakpoint()"""
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
@ -37,12 +40,12 @@ class DebugStatementParser(ast.NodeVisitor):
self.generic_visit(node)
def check_file(filename): # type: (str) -> int
def check_file(filename: str) -> int:
try:
with open(filename, 'rb') as f:
ast_obj = ast.parse(f.read(), filename=filename)
except SyntaxError:
print('{} - Could not parse ast'.format(filename))
print(f'{filename} - Could not parse ast')
print()
print('\t' + traceback.format_exc().replace('\n', '\n\t'))
print()
@ -52,16 +55,12 @@ def check_file(filename): # type: (str) -> int
visitor.visit(ast_obj)
for bp in visitor.breakpoints:
print(
'{}:{}:{} - {} {}'.format(
filename, bp.line, bp.col, bp.name, bp.reason,
),
)
print(f'{filename}:{bp.line}:{bp.col} - {bp.name} {bp.reason}')
return int(bool(visitor.breakpoints))
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to run')
args = parser.parse_args(argv)