Skip to content

Commit

Permalink
Cache get_pre_loads results
Browse files Browse the repository at this point in the history
  • Loading branch information
Pliner committed Dec 11, 2023
1 parent db2bd70 commit bffc8de
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions marshmallow_recipe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

_PRE_LOAD_KEY = "__marshmallow_recipe_pre_load__"
_PRE_LOAD_CLASS_KEY = "__marshmallow_recipe_pre_load_class__"
_PRE_LOADS_CACHE = {}


def pre_load(fn: collections.abc.Callable[..., Any]) -> collections.abc.Callable[..., Any]:
Expand All @@ -12,12 +13,17 @@ def pre_load(fn: collections.abc.Callable[..., Any]) -> collections.abc.Callable


def get_pre_loads(cls: type) -> list[collections.abc.Callable[..., Any]]:
existing = _PRE_LOADS_CACHE.get(cls)
if existing is not None:
return existing

result = []
for _, method in inspect.getmembers(cls):
if hasattr(method, _PRE_LOAD_KEY):
result.append(method)
for fn in getattr(cls, _PRE_LOAD_CLASS_KEY, []):
result.append(fn)
_PRE_LOADS_CACHE[cls] = result
return result


Expand Down

0 comments on commit bffc8de

Please sign in to comment.