1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# pylint: disable=missing-function-docstring, missing-module-docstring, invalid-name, import-outside-toplevel
# pylint: disable=missing-class-docstring, too-few-public-methods, unused-variable, multiple-statements, line-too-long
"""
Previously, open was uninferable on PyPy so we moved all functional tests
to a separate file. This is no longer the case but the files remain split.
"""
from contextlib import contextmanager
from pathlib import Path
myfile = open("test.txt", encoding="utf-8") # [consider-using-with]
def test_open():
fh = open("test.txt", encoding="utf-8") # [consider-using-with]
fh.close()
with open("test.txt", encoding="utf-8") as fh: # must not trigger
fh.read()
def test_open_in_enter():
"""Message must not trigger if the resource is allocated in a context manager."""
class MyContextManager:
def __init__(self):
self.file_handle = None
def __enter__(self):
self.file_handle = open("foo.txt", "w", encoding="utf-8") # must not trigger
def __exit__(self, exc_type, exc_value, traceback):
self.file_handle.close()
@contextmanager
def test_open_in_with_contextlib():
"""Message must not trigger if the resource is allocated in a context manager."""
file_handle = open("foo.txt", "w", encoding="utf-8") # must not trigger
yield file_handle
file_handle.close()
def test_open_outside_assignment():
open("foo", encoding="utf-8").read() # [consider-using-with]
content = open("foo", encoding="utf-8").read() # [consider-using-with]
def test_open_inside_with_block():
with open("foo", encoding="utf-8") as fh:
open("bar", encoding="utf-8") # [consider-using-with]
def test_ternary_if_in_with_block(file1, file2, which):
"""Regression test for issue #4676 (false positive)"""
with (open(file1, encoding="utf-8") if which else open(file2, encoding="utf-8")) as input_file: # must not trigger
return input_file.read()
def test_single_line_with(file1):
with open(file1, encoding="utf-8"): return file1.read() # must not trigger
def test_multiline_with_items(file1, file2, which):
with (open(file1, encoding="utf-8") if which
else open(file2, encoding="utf-8")) as input_file: return input_file.read()
def test_suppress_on_return():
return open("foo", encoding="utf8") # must not trigger
class TestControlFlow:
"""
The message is triggered if a context manager is assigned to a variable, which name is later
reassigned without the variable being used inside a ``with`` first.
E.g. the following would trigger the message:
a = open("foo") # <-- would trigger here
a = "something new"
But it must not happen that the logic which checks if the same variable is assigned multiple
times in different code branches where only one of those assign statements is hit at runtime.
For example, the variable could be assigned in an if-else construct.
These tests check that the message is not triggered in those circumstances.
"""
def test_defined_in_if_and_else(self, predicate):
if predicate:
file_handle = open("foo", encoding="utf8") # must not trigger
else:
file_handle = open("bar", encoding="utf8") # must not trigger
with file_handle:
return file_handle.read()
def test_defined_in_else_only(self, predicate):
if predicate:
result = "shiny watermelon"
else:
file_handle = open("foo", encoding="utf8") # must not trigger
with file_handle:
result = file_handle.read()
return result
def test_defined_in_if_only(self, predicate):
if predicate:
file_handle = open("foo", encoding="utf8") # must not trigger
with file_handle:
result = file_handle.read()
else:
result = "shiny watermelon"
return result
def test_triggers_if_reassigned_after_if_else(self, predicate):
if predicate:
file_handle = open("foo", encoding="utf8")
else:
file_handle = open( # [consider-using-with]
"bar", encoding="utf8"
)
file_handle = None
return file_handle
def test_defined_in_try_and_except(self):
try:
file_handle = open("foo", encoding="utf8") # must not trigger
except FileNotFoundError:
file_handle = open("bar", encoding="utf8") # must not trigger
with file_handle:
return file_handle.read()
def test_defined_in_try_and_finally(self):
try:
file_handle = open("foo", encoding="utf8") # must not trigger
except FileNotFoundError:
Path("foo").touch()
finally:
# +1: [used-before-assignment]
file_handle.open("foo", encoding="utf") # must not trigger consider-using-with
with file_handle:
return file_handle.read()
def test_defined_in_different_except_handlers(self, a, b):
try:
result = a/b
except ZeroDivisionError:
logfile = open("math_errors.txt", encoding="utf8") # must not trigger
result = "Can't divide by zero"
except TypeError:
logfile = open("type_errors.txt", encoding="utf8") # must not trigger
result = "Wrong types"
else:
logfile = open("results.txt", encoding="utf8") # must not trigger
with logfile:
logfile.write(result)
def test_multiple_return_statements(self, predicate):
if predicate:
return open("foo", encoding="utf8") # must not trigger
return open("bar", encoding="utf8") # must not trigger
|