Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from typing import Callable
import functools


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.

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.

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.

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.

key = (args, tuple(sorted(kwargs.items())))
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
Comment on lines +11 to +19
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

Loading