Add cache decorator for function result memoization#2191
Add cache decorator for function result memoization#2191Petliuk wants to merge 3 commits intomate-academy:masterfrom
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your solution is approved as it correctly implements the cache decorator and fulfills the functional requirements: results are cached per decorated function, repeated calls with the same positional arguments reuse the stored result, and the required messages ("Calculating new result" and "Getting from cache") are printed at the right times. The closure-based storage ensures different functions do not share caches, and using the args tuple as the key makes the behavior consistent for varying numbers of positional arguments. There are a couple of minor polish items flagged by the linter: at app/main.py:7 you should add an explicit return type annotation to your public function (for example -> Any or the precise return type), and at app/main.py:17 you should ensure the file ends with a newline so style checks pass. As an optional improvement, you could extend the decorator to accept keyword arguments by including **kwargs in the wrapper and incorporating them into the cache key. Another optional enhancement is to wrap the inner wrapper with functools.wraps(func) to preserve the original function’s name and docstring for better introspection. Overall, you met the core requirements well; addressing the minor lint and optional enhancements will further strengthen your solution.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am requesting changes because, while the decorator implementation itself is correct and meets the functional requirements, there is a small but blocking style issue that causes the CI to fail. In app/main.py at the end of the file (reported by flake8 as app/main.py:17:19: W292 no newline at end of file), there is no trailing newline, which violates the style checklist and stops the linter step from passing. To fix this, open app/main.py, add a newline after the last line of code so there is one blank line at the very end of the file, then save, commit, and push. Functionally, your cache decorator behaves as required: it caches results per function and argument tuple, and prints the correct messages for new vs. cached values. As optional improvements (not required to pass), you could use functools.wraps on the inner wrapper to preserve metadata and consider supporting keyword arguments in the cache key if needed. Once the newline is added and CI passes, your solution will fully satisfy both the functional requirements and the style checklist.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your solution is approved because it correctly implements the caching decorator (per-function storage, using the positional-arguments tuple as the key, and printing the expected messages). This is an educational approval override so you can continue progressing, but please fix the remaining style issue that will currently cause CI to fail. Critical issue (must fix): app/main.py is missing a trailing newline after the last line (return wrapper, e.g., line 20), which triggers flake8 rule W292; add exactly one blank line at the end of the file, then save, commit, and push so the linter passes. You did well keeping the decorator simple and clear, with descriptive names, consistent double-quote usage, and no unnecessary duplication or nonlocal usage. Optional improvement: consider adding @functools.wraps(func) to the wrapper function so the original function's name and docstring are preserved for introspection and tests. Another optional enhancement for the future would be to support keyword arguments in the cache key (for example, combining args with a normalized representation of kwargs), but the current positional-argument-only version already satisfies the task requirements—nice work.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| storage[args] = result | ||
| return result | ||
|
|
||
| return wrapper |
There was a problem hiding this comment.
The repository previously reported flake8 W292 (no newline at end of file) for app/main.py. The file still appears to end without a trailing newline. Please add a newline after the last line (return wrapper) so there's exactly one blank line at the end of the file, then save, commit, and push.
No description provided.