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

Additional type hints for config module. #11465

Merged
merged 10 commits into from Dec 1, 2021
26 changes: 14 additions & 12 deletions synapse/config/cache.py
@@ -1,4 +1,4 @@
# Copyright 2019 Matrix.org Foundation C.I.C.
# Copyright 2019-2021 Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
import threading
from typing import Callable, Dict, Optional

import attr

from synapse.python_dependencies import DependencyException, check_requirements

from ._base import Config, ConfigError
Expand All @@ -34,13 +36,13 @@
_DEFAULT_EVENT_CACHE_SIZE = "10K"


@attr.s(slots=True, auto_attribs=True)
class CacheProperties:
def __init__(self):
# The default factor size for all caches
self.default_factor_size = float(
os.environ.get(_CACHE_PREFIX, _DEFAULT_FACTOR_SIZE)
)
self.resize_all_caches_func = None
# The default factor size for all caches
default_factor_size: float = float(
os.environ.get(_CACHE_PREFIX, _DEFAULT_FACTOR_SIZE)
)
resize_all_caches_func: Optional[Callable[[], None]] = None


properties = CacheProperties()
Expand All @@ -62,7 +64,7 @@ def _canonicalise_cache_name(cache_name: str) -> str:

def add_resizable_cache(
cache_name: str, cache_resize_callback: Callable[[float], None]
):
) -> None:
"""Register a cache that's size can dynamically change

Args:
Expand Down Expand Up @@ -91,7 +93,7 @@ class CacheConfig(Config):
_environ = os.environ

@staticmethod
def reset():
def reset() -> None:
"""Resets the caches to their defaults. Used for tests."""
properties.default_factor_size = float(
os.environ.get(_CACHE_PREFIX, _DEFAULT_FACTOR_SIZE)
Expand All @@ -100,7 +102,7 @@ def reset():
with _CACHES_LOCK:
_CACHES.clear()

def generate_config_section(self, **kwargs):
def generate_config_section(self, **kwargs) -> str:
return """\
## Caching ##

Expand Down Expand Up @@ -162,7 +164,7 @@ def generate_config_section(self, **kwargs):
#sync_response_cache_duration: 2m
"""

def read_config(self, config, **kwargs):
def read_config(self, config, **kwargs) -> None:
clokep marked this conversation as resolved.
Show resolved Hide resolved
self.event_cache_size = self.parse_size(
config.get("event_cache_size", _DEFAULT_EVENT_CACHE_SIZE)
)
Expand Down Expand Up @@ -232,7 +234,7 @@ def read_config(self, config, **kwargs):
# needing an instance of Config
properties.resize_all_caches_func = self.resize_all_caches

def resize_all_caches(self):
def resize_all_caches(self) -> None:
"""Ensure all cache sizes are up to date

For each cache, run the mapped callback function with either
Expand Down