Skip to content
29 changes: 29 additions & 0 deletions buggy_context_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class FileManager:
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
def __enter__(self):
# Bug fixed: handle file open errors and return file object
try:
self.file = open(self.filename, self.mode)
return self.file
except Exception:
# Re-raise so caller can handle, but ensure attribute exists for __exit__
self.file = None
raise
def __exit__(self, exc_type, exc_val, exc_tb):
# Bug fixed: ensure file is closed if it was opened
try:
if getattr(self, 'file', None):
self.file.close()
except Exception:
pass
# Do not suppress exceptions; propagate by returning False
return False

# Usage example
try:
with FileManager('nonexistent.txt', 'R') as f:
print(f.read())
except Exception as e:
print(f"Error: {e}")
Comment on lines +24 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix the usage example (invalid mode and missing main guard)

open() rejects the uppercase mode 'R', so the sample currently fails with ValueError instead of demonstrating a missing-file error. While you’re touching it, please also wrap the demo in a __main__ guard so imports stay side-effect free.

-# Usage example
-try:
-    with FileManager('nonexistent.txt', 'R') as f:
-        print(f.read())
-except Exception as e:
-    print(f"Error: {e}")
+# Usage example
+if __name__ == "__main__":
+    try:
+        with FileManager('nonexistent.txt', 'r') as f:
+            print(f.read())
+    except Exception as e:
+        print(f"Error: {e}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Usage example
try:
with FileManager('nonexistent.txt', 'R') as f:
print(f.read())
except Exception as e:
print(f"Error: {e}")
# Usage example
if __name__ == "__main__":
try:
with FileManager('nonexistent.txt', 'r') as f:
print(f.read())
except Exception as e:
print(f"Error: {e}")
🧰 Tools
🪛 Ruff (0.13.1)

28-28: Do not catch blind exception: Exception

(BLE001)

🤖 Prompt for AI Agents
In buggy_context_manager.py around lines 24 to 29, the usage example incorrectly
uses uppercase mode 'R' (which raises ValueError) and runs at import time;
change the mode to lowercase 'r' and wrap the demo try/except block in a proper
if __name__ == "__main__": guard so the missing-file error is shown only when
executed as a script.

20 changes: 20 additions & 0 deletions buggy_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import functools
def log_function_call(func):
# Bug: decorator does not preserve function signature
@functools.wraps(func)
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
return wrapper

@log_function_call
def add(a, b):
return a + b

@log_function_call
def greet(name):
return f"Hello, {name}!"

print(add(2, 3))
print(greet("Alice"))
print(add.__name__) # Bug: should be 'add', but will be 'wrapper'
18 changes: 18 additions & 0 deletions buggy_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
# Bug fixed: base class should raise NotImplementedError
raise NotImplementedError("Subclasses must implement speak()")

class Dog(Animal):
def speak(self):
return "Woof!"

class Cat(Animal):
def speak(self):
return "Meow!"

animals = [Dog("Buddy"), Cat("Whiskers")]
for animal in animals:
print(f"{animal.name} says: {animal.speak()}")
4 changes: 4 additions & 0 deletions buggy_lambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
numbers = [1, 2, 3, 4, 5]
# Bug fixed: lambda predicate should check for even numbers (x % 2 == 0)
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(f"Even numbers: {even_numbers}")
18 changes: 18 additions & 0 deletions buggy_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Person:
def __init__(self, name):
self._name = name

@property
def name(self):
# Bug fixed: return the stored name
return self._name

@name.setter
def name(self, value):
self._name = value


p = Person("Alice")
print(p.name) # Should print 'Alice'
p.name = "Bob"
print(p.name) # Should print 'Bob'