chore: refactor and type memoize utility function

This commit is contained in:
Greyson Lalonde
2025-10-13 13:09:10 -04:00
parent 620df71763
commit 6bc8818ae9

View File

@@ -1,14 +1,38 @@
"""Utility functions for the crewai project module."""
from collections.abc import Callable
from functools import wraps
from typing import Any, ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def memoize(func):
cache = {}
def memoize(meth: Callable[P, R]) -> Callable[P, R]:
"""Memoize a method by caching its results based on arguments.
@wraps(func)
def memoized_func(*args, **kwargs):
Args:
meth: The method to memoize.
Returns:
A memoized version of the method that caches results.
"""
cache: dict[Any, R] = {}
@wraps(meth)
def memoized_func(*args: P.args, **kwargs: P.kwargs) -> R:
"""Memoized wrapper method.
Args:
*args: Positional arguments to pass to the method.
**kwargs: Keyword arguments to pass to the method.
Returns:
The cached or computed result of the method.
"""
key = (args, tuple(kwargs.items()))
if key not in cache:
cache[key] = func(*args, **kwargs)
cache[key] = meth(*args, **kwargs)
return cache[key]
return memoized_func