Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix ExpiringCache Sized complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Sep 7, 2023
1 parent b12a786 commit a611a80
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions synapse/util/caches/expiringcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import logging
from collections import OrderedDict
from typing import Any, Generic, Optional, TypeVar, Union, overload
from typing import Any, Generic, Iterable, Optional, TypeVar, Union, overload

import attr
from typing_extensions import Literal
Expand Down Expand Up @@ -100,7 +100,10 @@ def evict(self) -> None:
while self._max_size and len(self) > self._max_size:
_key, value = self._cache.popitem(last=False)
if self.iterable:
self.metrics.inc_evictions(EvictionReason.size, len(value.value))
# type-ignore, here and below: if self.iterable is true, then the value
# type VT should be Sized (i.e. have a __len__ method). We don't enforce
# this via the type system at present.
self.metrics.inc_evictions(EvictionReason.size, len(value.value)) # type: ignore[arg-type]
else:
self.metrics.inc_evictions(EvictionReason.size)

Expand Down Expand Up @@ -134,7 +137,7 @@ def pop(self, key: KT, default: T = SENTINEL) -> Union[VT, T]:
return default

if self.iterable:
self.metrics.inc_evictions(EvictionReason.invalidation, len(value.value))
self.metrics.inc_evictions(EvictionReason.invalidation, len(value.value)) # type: ignore[arg-type]
else:
self.metrics.inc_evictions(EvictionReason.invalidation)

Expand Down Expand Up @@ -182,7 +185,7 @@ async def _prune_cache(self) -> None:
for k in keys_to_delete:
value = self._cache.pop(k)
if self.iterable:
self.metrics.inc_evictions(EvictionReason.time, len(value.value))
self.metrics.inc_evictions(EvictionReason.time, len(value.value)) # type: ignore[arg-type]
else:
self.metrics.inc_evictions(EvictionReason.time)

Expand All @@ -195,7 +198,8 @@ async def _prune_cache(self) -> None:

def __len__(self) -> int:
if self.iterable:
return sum(len(entry.value) for entry in self._cache.values())
g: Iterable[int] = (len(entry.value) for entry in self._cache.values()) # type: ignore[arg-type]
return sum(g)
else:
return len(self._cache)

Expand Down

0 comments on commit a611a80

Please sign in to comment.