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

Commit

Permalink
Back out of generics due to python-attrs/attrs#313
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Sep 2, 2021
1 parent 9444ca1 commit a0aef0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
8 changes: 5 additions & 3 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,18 @@ def failure_cb(val):
return new_d


# This class can't be generic because it uses slots with attrs.
# See: https://github.com/python-attrs/attrs/issues/313
@attr.s(slots=True, frozen=True)
class DoneAwaitable(Generic[R]):
class DoneAwaitable: # should be: Generic[R]
"""Simple awaitable that returns the provided value."""

value = attr.ib(type="R")
value = attr.ib(type=Any) # should be: R

def __await__(self):
return self

def __iter__(self) -> "DoneAwaitable[R]":
def __iter__(self) -> "DoneAwaitable":
return self

def __next__(self) -> None:
Expand Down
18 changes: 9 additions & 9 deletions synapse/util/caches/dictionary_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import enum
import logging
import threading
from typing import Dict, Generic, Iterable, Optional, Set, TypeVar
from typing import Any, Dict, Generic, Iterable, Optional, Set, TypeVar

import attr

Expand All @@ -31,8 +31,10 @@
DV = TypeVar("DV")


# This class can't be generic because it uses slots with attrs.
# See: https://github.com/python-attrs/attrs/issues/313
@attr.s(slots=True)
class DictionaryEntry(Generic[DKT, DV]):
class DictionaryEntry: # should be: Generic[DKT, DV].
"""Returned when getting an entry from the cache
Attributes:
Expand All @@ -45,8 +47,8 @@ class DictionaryEntry(Generic[DKT, DV]):
"""

full = attr.ib(type=bool)
known_absent = attr.ib(type=Set[DKT])
value = attr.ib(type=Dict[DKT, DV])
known_absent = attr.ib(type=Set[Any]) # should be: Set[DKT]
value = attr.ib(type=Dict[Any, Any]) # should be: Dict[DKT, DV]

def __len__(self) -> int:
return len(self.value)
Expand All @@ -64,7 +66,7 @@ class DictionaryCache(Generic[KT, DKT, DV]):
"""

def __init__(self, name: str, max_entries: int = 1000):
self.cache: LruCache[KT, DictionaryEntry[DKT, DV]] = LruCache(
self.cache: LruCache[KT, DictionaryEntry] = LruCache(
max_size=max_entries, cache_name=name, size_callback=len
)

Expand All @@ -84,7 +86,7 @@ def check_thread(self) -> None:

def get(
self, key: KT, dict_keys: Optional[Iterable[DKT]] = None
) -> DictionaryEntry[DKT, DV]:
) -> DictionaryEntry:
"""Fetch an entry out of the cache
Args:
Expand Down Expand Up @@ -158,9 +160,7 @@ def _update_or_insert(
# We pop and reinsert as we need to tell the cache the size may have
# changed

entry: DictionaryEntry[DKT, DV] = self.cache.pop(
key, DictionaryEntry(False, set(), {})
)
entry: DictionaryEntry = self.cache.pop(key, DictionaryEntry(False, set(), {}))
entry.value.update(value)
entry.known_absent.update(known_absent)
self.cache[key] = entry
Expand Down

0 comments on commit a0aef0b

Please sign in to comment.