Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on the cache decorator! Your implementation is functionally correct and meets all the requirements of the task. The logic for caching results and handling different functions and arguments is spot on.
I'm approving your solution. As a point for future improvement, consider refining the type hints in app/main.py. The decorator and its inner wrapper function are currently hinted to return None, but they actually return the value from the decorated function. Using typing.Any for the return type would make the type hints more accurate and the code easier to understand for static analysis tools.
This is a minor detail in an otherwise solid solution. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| def cache(func: Callable) -> Callable: | ||
| # Write your code here | ||
| pass | ||
| def cache(func: Callable) -> Callable[..., None]: |
There was a problem hiding this comment.
The return type hint Callable[..., None] is incorrect. The decorator returns a wrapper function which in turn returns the result of the original function, not None. Consider using typing.Any to indicate that the wrapped function can return a value of any type.
| local_cache = {} | ||
|
|
||
| @functools.wraps(func) | ||
| def wrapper(*args, **kwargs) -> None: |
There was a problem hiding this comment.
The wrapper's return type is hinted as None, but the function returns a value on lines 13 and 18. This should be corrected to reflect the actual return type, for example by importing Any from typing and using it here.
| def cache(func: Callable) -> Callable: | ||
| # Write your code here | ||
| pass | ||
| def cache(func: Callable) -> Callable[..., None]: |
There was a problem hiding this comment.
The return type hint Callable[..., None] is incorrect. The decorator returns a wrapper function which in turn returns the result of the original function, not None. Consider using typing.Any to indicate that the wrapped function can return a value of any type.
| local_cache = {} | ||
|
|
||
| @functools.wraps(func) | ||
| def wrapper(*args, **kwargs) -> None: |
There was a problem hiding this comment.
The wrapper's return type is hinted as None, but the function returns a value on lines 13 and 18. This should be corrected to reflect the actual return type, for example by importing Any from typing and using it here.
| if key in local_cache: | ||
| print("Getting from cache") | ||
| return local_cache[key] | ||
| else: | ||
| print("Calculating new result") | ||
| result = func(*args, **kwargs) | ||
| local_cache[key] = result | ||
| return result | ||
| return wrapper |
There was a problem hiding this comment.
| if key in local_cache: | |
| print("Getting from cache") | |
| return local_cache[key] | |
| else: | |
| print("Calculating new result") | |
| result = func(*args, **kwargs) | |
| local_cache[key] = result | |
| return result | |
| return wrapper | |
| if key in local_cache: | |
| print("Getting from cache") | |
| else: | |
| print("Calculating new result") | |
| local_cache[key] = func(*args, **kwargs) | |
| return local_cache[key] | |
| return wrapper |
No description provided.