Skip to content

Solution#2183

Open
gospodenkolubomir-oss wants to merge 1 commit intomate-academy:masterfrom
gospodenkolubomir-oss:develop
Open

Solution#2183
gospodenkolubomir-oss wants to merge 1 commit intomate-academy:masterfrom
gospodenkolubomir-oss:develop

Conversation

@gospodenkolubomir-oss
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

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

  1. Rate AI review example

Comment thread app/main.py
def cache(func: Callable) -> Callable:
# Write your code here
pass
def cache(func: Callable) -> Callable[..., None]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread app/main.py
local_cache = {}

@functools.wraps(func)
def wrapper(*args, **kwargs) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread app/main.py
def cache(func: Callable) -> Callable:
# Write your code here
pass
def cache(func: Callable) -> Callable[..., None]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread app/main.py
local_cache = {}

@functools.wraps(func)
def wrapper(*args, **kwargs) -> None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread app/main.py
Comment on lines +11 to +19
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants