Skip to content

Commit 9bf684c

Browse files
authored
Merge pull request #297 from pre-commit/warnings
Fix resource warnings
2 parents 1fc25d9 + 5dc306b commit 9bf684c

File tree

7 files changed

+14
-7
lines changed

7 files changed

+14
-7
lines changed

pre_commit_hooks/autopep8_wrapper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def main(argv=None):
1414

1515
retv = 0
1616
for filename in args.files:
17-
original_contents = io.open(filename, encoding='UTF-8').read()
17+
with io.open(filename, encoding='UTF-8') as f:
18+
original_contents = f.read()
1819
new_contents = autopep8.fix_code(original_contents, args)
1920
if original_contents != new_contents:
2021
print('Fixing {}'.format(filename))

pre_commit_hooks/check_ast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def check_ast(argv=None):
1818
for filename in args.filenames:
1919

2020
try:
21-
ast.parse(open(filename, 'rb').read(), filename=filename)
21+
with open(filename, 'rb') as f:
22+
ast.parse(f.read(), filename=filename)
2223
except SyntaxError:
2324
print('{}: failed parsing with {} {}:'.format(
2425
filename,

pre_commit_hooks/check_builtin_literals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def visit_Call(self, node):
4747

4848

4949
def check_file_for_builtin_type_constructors(filename, ignore=None, allow_dict_kwargs=True):
50-
tree = ast.parse(open(filename, 'rb').read(), filename=filename)
50+
with open(filename, 'rb') as f:
51+
tree = ast.parse(f.read(), filename=filename)
5152
visitor = BuiltinTypeVisitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
5253
visitor.visit(tree)
5354
return visitor.builtin_type_calls

pre_commit_hooks/check_docstring_first.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def main(argv=None):
5858
retv = 0
5959

6060
for filename in args.filenames:
61-
contents = io.open(filename, encoding='UTF-8').read()
61+
with io.open(filename, encoding='UTF-8') as f:
62+
contents = f.read()
6263
retv |= check_docstring_first(contents, filename=filename)
6364

6465
return retv

pre_commit_hooks/debug_statement_hook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def visit_Call(self, node):
3636

3737
def check_file(filename):
3838
try:
39-
ast_obj = ast.parse(open(filename, 'rb').read(), filename=filename)
39+
with open(filename, 'rb') as f:
40+
ast_obj = ast.parse(f.read(), filename=filename)
4041
except SyntaxError:
4142
print('{} - Could not parse ast'.format(filename))
4243
print()

pre_commit_hooks/string_fixer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def get_line_offsets_by_line_no(src):
3232

3333

3434
def fix_strings(filename):
35-
contents = io.open(filename, encoding='UTF-8').read()
35+
with io.open(filename, encoding='UTF-8') as f:
36+
contents = f.read()
3637
line_offsets = get_line_offsets_by_line_no(contents)
3738

3839
# Basically a mutable string

tests/check_builtin_literals_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def test_dict_no_allow_kwargs_exprs(expression, calls):
9494

9595
def test_ignore_constructors():
9696
visitor = BuiltinTypeVisitor(ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'))
97-
visitor.visit(ast.parse(open(get_resource_path('builtin_constructors.py'), 'rb').read(), 'builtin_constructors.py'))
97+
with open(get_resource_path('builtin_constructors.py'), 'rb') as f:
98+
visitor.visit(ast.parse(f.read(), 'builtin_constructors.py'))
9899
assert visitor.builtin_type_calls == []
99100

100101

0 commit comments

Comments
 (0)