Add test for FP for py/should-use-with

This commit is contained in:
Owen Mansel-Chan
2026-06-17 12:55:17 +01:00
parent 199fd864ad
commit d72144646a
2 changed files with 15 additions and 0 deletions

View File

@@ -1 +1,2 @@
| 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 |
| 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 |

View File

@@ -167,6 +167,20 @@ def no_with():
finally:
f.close()
# Should use context manager, with the resource held in an instance attribute
# (caught via instance-attribute type tracking).
class HoldsCM(object):
def __init__(self):
self.f = CM()
def no_with_attribute(self):
try:
self.f.write("Hello ")
self.f.write(" World\n")
finally:
self.f.close()
#Assert without side-effect
def assert_ok(seq):
assert all(isinstance(element, (str, unicode)) for element in seq)