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

Add most missing type hints to synapse.util #11328

Merged
merged 14 commits into from
Nov 16, 2021
87 changes: 3 additions & 84 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -181,92 +181,11 @@ disallow_untyped_defs = True
[mypy-synapse.streams.*]
disallow_untyped_defs = True

[mypy-synapse.util.batching_queue]
[mypy-synapse.util.*]
disallow_untyped_defs = True

[mypy-synapse.util.caches.cached_call]
disallow_untyped_defs = True

[mypy-synapse.util.caches.dictionary_cache]
disallow_untyped_defs = True

[mypy-synapse.util.caches.lrucache]
disallow_untyped_defs = True

[mypy-synapse.util.caches.response_cache]
disallow_untyped_defs = True

[mypy-synapse.util.caches.stream_change_cache]
disallow_untyped_defs = True

[mypy-synapse.util.caches.ttl_cache]
disallow_untyped_defs = True

[mypy-synapse.util.daemonize]
disallow_untyped_defs = True

[mypy-synapse.util.file_consumer]
disallow_untyped_defs = True

[mypy-synapse.util.frozenutils]
disallow_untyped_defs = True

[mypy-synapse.util.hash]
disallow_untyped_defs = True

[mypy-synapse.util.httpresourcetree]
disallow_untyped_defs = True

[mypy-synapse.util.iterutils]
disallow_untyped_defs = True

[mypy-synapse.util.linked_list]
disallow_untyped_defs = True

[mypy-synapse.util.logcontext]
disallow_untyped_defs = True

[mypy-synapse.util.logformatter]
disallow_untyped_defs = True

[mypy-synapse.util.macaroons]
disallow_untyped_defs = True

[mypy-synapse.util.manhole]
disallow_untyped_defs = True

[mypy-synapse.util.module_loader]
disallow_untyped_defs = True

[mypy-synapse.util.msisdn]
disallow_untyped_defs = True

[mypy-synapse.util.patch_inline_callbacks]
disallow_untyped_defs = True

[mypy-synapse.util.ratelimitutils]
disallow_untyped_defs = True

[mypy-synapse.util.retryutils]
disallow_untyped_defs = True

[mypy-synapse.util.rlimit]
disallow_untyped_defs = True

[mypy-synapse.util.stringutils]
disallow_untyped_defs = True

[mypy-synapse.util.templates]
disallow_untyped_defs = True

[mypy-synapse.util.threepids]
disallow_untyped_defs = True

[mypy-synapse.util.wheel_timer]
disallow_untyped_defs = True

[mypy-synapse.util.versionstring]
disallow_untyped_defs = True
[mypy-synapse.util.caches.treecache]
disallow_untyped_defs = False

[mypy-tests]
disallow_untyped_defs = True
Expand Down
32 changes: 17 additions & 15 deletions synapse/util/caches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import typing
from enum import Enum, auto
from sys import intern
from typing import Callable, Dict, Optional, Sized
from typing import Any, Callable, Dict, List, Optional, Sized, TypeVar

import attr
from prometheus_client.core import Gauge
Expand Down Expand Up @@ -58,20 +58,20 @@ class EvictionReason(Enum):
time = auto()


@attr.s(slots=True)
@attr.s(slots=True, auto_attribs=True)
class CacheMetric:

_cache = attr.ib()
_cache_type = attr.ib(type=str)
_cache_name = attr.ib(type=str)
_collect_callback = attr.ib(type=Optional[Callable])
_cache: Sized
_cache_type: str
_cache_name: str
_collect_callback: Optional[Callable]

hits = attr.ib(default=0)
misses = attr.ib(default=0)
hits: int = 0
misses: int = 0
eviction_size_by_reason: typing.Counter[EvictionReason] = attr.ib(
factory=collections.Counter
)
memory_usage = attr.ib(default=None)
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
memory_usage: Optional[int] = None

def inc_hits(self) -> None:
self.hits += 1
Expand All @@ -89,13 +89,14 @@ def inc_memory_usage(self, memory: int) -> None:
self.memory_usage += memory

def dec_memory_usage(self, memory: int) -> None:
assert self.memory_usage is not None
self.memory_usage -= memory

def clear_memory_usage(self) -> None:
if self.memory_usage is not None:
self.memory_usage = 0

def describe(self):
def describe(self) -> List[str]:
return []

def collect(self) -> None:
Expand All @@ -118,8 +119,9 @@ def collect(self) -> None:
self.eviction_size_by_reason[reason]
)
cache_total.labels(self._cache_name).set(self.hits + self.misses)
if getattr(self._cache, "max_size", None):
cache_max_size.labels(self._cache_name).set(self._cache.max_size)
max_size = getattr(self._cache, "max_size", None)
if max_size:
cache_max_size.labels(self._cache_name).set(max_size)

if TRACK_MEMORY_USAGE:
# self.memory_usage can be None if nothing has been inserted
Expand Down Expand Up @@ -193,7 +195,7 @@ def register_cache(
}


def intern_string(string):
def intern_string(string: Optional[str]) -> Optional[str]:
"""Takes a (potentially) unicode string and interns it if it's ascii"""
if string is None:
return None
Expand All @@ -204,15 +206,15 @@ def intern_string(string):
return string


def intern_dict(dictionary):
def intern_dict(dictionary: Dict[str, Any]) -> Dict[str, Any]:
"""Takes a dictionary and interns well known keys and their values"""
return {
KNOWN_KEYS.get(key, key): _intern_known_values(key, value)
for key, value in dictionary.items()
}


def _intern_known_values(key, value):
def _intern_known_values(key: str, value: Any) -> Any:
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
intern_keys = ("event_id", "room_id", "sender", "user_id", "type", "state_key")

if key in intern_keys:
Expand Down