Skip to content

Commit d721446

Browse files
committed
Add test for FP for py/should-use-with
1 parent 199fd86 commit d721446

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
| test.py:168:9:168:17 | Attribute() | Instance of context-manager class $@ is closed in a finally block. Consider using 'with' statement. | test.py:151:1:151:17 | Class CM | CM |
2+
| test.py:182:13:182:26 | Attribute() | Instance of context-manager class $@ is closed in a finally block. Consider using 'with' statement. | test.py:151:1:151:17 | Class CM | CM |

python/ql/test/query-tests/Statements/general/test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ def no_with():
167167
finally:
168168
f.close()
169169

170+
# Should use context manager, with the resource held in an instance attribute
171+
# (caught via instance-attribute type tracking).
172+
class HoldsCM(object):
173+
174+
def __init__(self):
175+
self.f = CM()
176+
177+
def no_with_attribute(self):
178+
try:
179+
self.f.write("Hello ")
180+
self.f.write(" World\n")
181+
finally:
182+
self.f.close()
183+
170184
#Assert without side-effect
171185
def assert_ok(seq):
172186
assert all(isinstance(element, (str, unicode)) for element in seq)

0 commit comments

Comments
 (0)