From 2cbf4dd4a4393d66fba993f8ad99377744be8c08 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 8 Mar 2023 12:21:53 +0000 Subject: Modify @cache_in_self decorator to accept self classes other than HomeServer We want to use this in conjunction with ModuleApi in order to save and reference singleton instances of module api callback classes. --- synapse/util/caches/cache_in_self_decorator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/synapse/util/caches/cache_in_self_decorator.py b/synapse/util/caches/cache_in_self_decorator.py index fea41b9d08..3b9ded2096 100644 --- a/synapse/util/caches/cache_in_self_decorator.py +++ b/synapse/util/caches/cache_in_self_decorator.py @@ -1,9 +1,11 @@ -from typing import TYPE_CHECKING, Callable, Dict, List, Optional, TypeVar, cast +import functools +from typing import Callable, TypeVar T = TypeVar("T") +U = TypeVar("U") -def cache_in_self(builder: Callable[["HomeServer"], T]) -> Callable[["HomeServer"], T]: +def cache_in_self(builder: Callable[[T], U]) -> Callable[[T], U]: """Wraps a function called e.g. `get_foo`, checking if `self.foo` exists and returning if so. If not, calls the given function and sets `self.foo` to it. @@ -22,7 +24,7 @@ def cache_in_self(builder: Callable[["HomeServer"], T]) -> Callable[["HomeServer building = [False] @functools.wraps(builder) - def _get(self: "HomeServer") -> T: + def _get(self: T) -> U: try: return getattr(self, depname) except AttributeError: -- cgit 1.5.1