Merge pull request #297 from pre-commit/warnings

Fix resource warnings
This commit is contained in:
Anthony Sottile 2018-06-18 08:21:00 -07:00 committed by GitHub
commit 9bf684c131
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 7 deletions

View file

@ -14,7 +14,8 @@ def main(argv=None):
retv = 0
for filename in args.files:
original_contents = io.open(filename, encoding='UTF-8').read()
with io.open(filename, encoding='UTF-8') as f:
original_contents = f.read()
new_contents = autopep8.fix_code(original_contents, args)
if original_contents != new_contents:
print('Fixing {}'.format(filename))

View file

@ -18,7 +18,8 @@ def check_ast(argv=None):
for filename in args.filenames:
try:
ast.parse(open(filename, 'rb').read(), filename=filename)
with open(filename, 'rb') as f:
ast.parse(f.read(), filename=filename)
except SyntaxError:
print('{}: failed parsing with {} {}:'.format(
filename,

View file

@ -47,7 +47,8 @@ class BuiltinTypeVisitor(ast.NodeVisitor):
def check_file_for_builtin_type_constructors(filename, ignore=None, allow_dict_kwargs=True):
tree = ast.parse(open(filename, 'rb').read(), filename=filename)
with open(filename, 'rb') as f:
tree = ast.parse(f.read(), filename=filename)
visitor = BuiltinTypeVisitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
visitor.visit(tree)
return visitor.builtin_type_calls

View file

@ -58,7 +58,8 @@ def main(argv=None):
retv = 0
for filename in args.filenames:
contents = io.open(filename, encoding='UTF-8').read()
with io.open(filename, encoding='UTF-8') as f:
contents = f.read()
retv |= check_docstring_first(contents, filename=filename)
return retv

View file

@ -36,7 +36,8 @@ class DebugStatementParser(ast.NodeVisitor):
def check_file(filename):
try:
ast_obj = ast.parse(open(filename, 'rb').read(), filename=filename)
with open(filename, 'rb') as f:
ast_obj = ast.parse(f.read(), filename=filename)
except SyntaxError:
print('{} - Could not parse ast'.format(filename))
print()

View file

@ -32,7 +32,8 @@ def get_line_offsets_by_line_no(src):
def fix_strings(filename):
contents = io.open(filename, encoding='UTF-8').read()
with io.open(filename, encoding='UTF-8') as f:
contents = f.read()
line_offsets = get_line_offsets_by_line_no(contents)
# Basically a mutable string

View file

@ -94,7 +94,8 @@ def test_dict_no_allow_kwargs_exprs(expression, calls):
def test_ignore_constructors():
visitor = BuiltinTypeVisitor(ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'))
visitor.visit(ast.parse(open(get_resource_path('builtin_constructors.py'), 'rb').read(), 'builtin_constructors.py'))
with open(get_resource_path('builtin_constructors.py'), 'rb') as f:
visitor.visit(ast.parse(f.read(), 'builtin_constructors.py'))
assert visitor.builtin_type_calls == []