-
Notifications
You must be signed in to change notification settings - Fork 1
Fix inheritance: abstract base method and Cat.speak implementation #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jj-devhub
wants to merge
8
commits into
main
Choose a base branch
from
fix/buggy_decorator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19c45ff
Fix decorator to preserve function signature using functools.wraps
jj-devhub f81c60f
Fix inheritance: make base speak abstract and implement Cat.speak
jj-devhub c18b603
Fix decorator: use functools.wraps to preserve metadata
jj-devhub a4db42e
Fix context manager: handle open errors and ensure proper close in __…
jj-devhub d3977c0
Fix lambda filter to select even numbers
jj-devhub 1ca3001
Fix property getter to return stored name and correct indentation
jj-devhub b8c9a0c
Fix property getter and tests
jj-devhub ddbe31e
Update buggy_context_manager.py R replacing r
jj-devhub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the usage example (invalid mode and missing main guard)
open()rejects the uppercase mode'R', so the sample currently fails withValueErrorinstead 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.📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.13.1)
28-28: Do not catch blind exception:
Exception(BLE001)
🤖 Prompt for AI Agents