summary refs log tree commit diff
diff options
context:
space:
mode:
authorEric Eastwood <erice@element.io>2025-05-13 10:22:15 -0500
committerGitHub <noreply@github.com>2025-05-13 10:22:15 -0500
commit6e910e2b2c5cef393473dcc6bf957a8671a1186e (patch)
tree4c957dc2556ec4e33bd33bca2ed689e0a21e9c9d
parentExplicitly enable pypy for cibuildwheel (#18417) (diff)
downloadsynapse-6e910e2b2c5cef393473dcc6bf957a8671a1186e.tar.xz
Fix a couple type annotations in the `RootConfig`/`Config` (#18409)
Fix a couple type annotations in the `RootConfig`/`Config`. Discovered
while cribbing this code for another project.

It's really sucks that `mypy` type checking doesn't catch this. I assume
this is because we also have a `synapse/config/_base.pyi` that overrides
all of this. Still unclear to me why the `Iterable[str]` vs
`StrSequence` issue wasn't caught as that's what `ConfigError` expects.
Diffstat (limited to '')
-rw-r--r--changelog.d/18409.misc1
-rw-r--r--synapse/config/_base.py6
-rw-r--r--synapse/config/_base.pyi4
-rw-r--r--synapse/config/experimental.py6
-rw-r--r--synapse/config/key.py6
-rw-r--r--synapse/config/workers.py2
-rw-r--r--tests/config/test_api.py3
-rw-r--r--tests/config/test_appservice.py7
-rw-r--r--tests/config/test_cache.py3
-rw-r--r--tests/config/test_database.py5
-rw-r--r--tests/config/test_room_directory.py5
-rw-r--r--tests/config/test_server.py10
-rw-r--r--tests/events/test_auto_accept_invites.py5
13 files changed, 37 insertions, 26 deletions
diff --git a/changelog.d/18409.misc b/changelog.d/18409.misc
new file mode 100644

index 0000000000..bbb9bdbb1b --- /dev/null +++ b/changelog.d/18409.misc
@@ -0,0 +1 @@ +Fix a couple type annotations in the `RootConfig`/`Config`. diff --git a/synapse/config/_base.py b/synapse/config/_base.py
index 132ba26af9..d367d45fea 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py
@@ -170,7 +170,7 @@ class Config: section: ClassVar[str] - def __init__(self, root_config: "RootConfig" = None): + def __init__(self, root_config: "RootConfig"): self.root = root_config # Get the path to the default Synapse template directory @@ -445,7 +445,7 @@ class RootConfig: return res @classmethod - def invoke_all_static(cls, func_name: str, *args: Any, **kwargs: any) -> None: + def invoke_all_static(cls, func_name: str, *args: Any, **kwargs: Any) -> None: """ Invoke a static function on config objects this RootConfig is configured to use. @@ -1047,7 +1047,7 @@ class RoutableShardedWorkerHandlingConfig(ShardedWorkerHandlingConfig): return self._get_instance(key) -def read_file(file_path: Any, config_path: Iterable[str]) -> str: +def read_file(file_path: Any, config_path: StrSequence) -> str: """Check the given file exists, and read it into a string If it does not, emit an error indicating the problem diff --git a/synapse/config/_base.pyi b/synapse/config/_base.pyi
index 55b0e2cbf4..9c4ec8f713 100644 --- a/synapse/config/_base.pyi +++ b/synapse/config/_base.pyi
@@ -179,7 +179,7 @@ class RootConfig: class Config: root: RootConfig default_template_dir: str - def __init__(self, root_config: Optional[RootConfig] = ...) -> None: ... + def __init__(self, root_config: RootConfig = ...) -> None: ... @staticmethod def parse_size(value: Union[str, int]) -> int: ... @staticmethod @@ -212,4 +212,4 @@ class ShardedWorkerHandlingConfig: class RoutableShardedWorkerHandlingConfig(ShardedWorkerHandlingConfig): def get_instance(self, key: str) -> str: ... # noqa: F811 -def read_file(file_path: Any, config_path: Iterable[str]) -> str: ... +def read_file(file_path: Any, config_path: StrSequence) -> str: ... diff --git a/synapse/config/experimental.py b/synapse/config/experimental.py
index 0a963b121a..1226eaa58a 100644 --- a/synapse/config/experimental.py +++ b/synapse/config/experimental.py
@@ -21,7 +21,7 @@ import enum from functools import cache -from typing import TYPE_CHECKING, Any, Iterable, Optional +from typing import TYPE_CHECKING, Any, Optional import attr import attr.validators @@ -29,7 +29,7 @@ import attr.validators from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersions from synapse.config import ConfigError from synapse.config._base import Config, RootConfig, read_file -from synapse.types import JsonDict +from synapse.types import JsonDict, StrSequence # Determine whether authlib is installed. try: @@ -45,7 +45,7 @@ if TYPE_CHECKING: @cache -def read_secret_from_file_once(file_path: Any, config_path: Iterable[str]) -> str: +def read_secret_from_file_once(file_path: Any, config_path: StrSequence) -> str: """Returns the memoized secret read from file.""" return read_file(file_path, config_path).strip() diff --git a/synapse/config/key.py b/synapse/config/key.py
index 337f98dbc1..29c558448b 100644 --- a/synapse/config/key.py +++ b/synapse/config/key.py
@@ -191,7 +191,7 @@ class KeyConfig(Config): if macaroon_secret_key: raise ConfigError(CONFLICTING_MACAROON_SECRET_KEY_OPTS_ERROR) macaroon_secret_key = read_file( - macaroon_secret_key_path, "macaroon_secret_key_path" + macaroon_secret_key_path, ("macaroon_secret_key_path",) ).strip() if not macaroon_secret_key: macaroon_secret_key = self.root.registration.registration_shared_secret @@ -216,7 +216,9 @@ class KeyConfig(Config): if form_secret_path: if form_secret: raise ConfigError(CONFLICTING_FORM_SECRET_OPTS_ERROR) - self.form_secret = read_file(form_secret_path, "form_secret_path").strip() + self.form_secret = read_file( + form_secret_path, ("form_secret_path",) + ).strip() else: self.form_secret = form_secret diff --git a/synapse/config/workers.py b/synapse/config/workers.py
index 5af50ee952..2dfeb47c2e 100644 --- a/synapse/config/workers.py +++ b/synapse/config/workers.py
@@ -263,7 +263,7 @@ class WorkerConfig(Config): if worker_replication_secret: raise ConfigError(CONFLICTING_WORKER_REPLICATION_SECRET_OPTS_ERROR) self.worker_replication_secret = read_file( - worker_replication_secret_path, "worker_replication_secret_path" + worker_replication_secret_path, ("worker_replication_secret_path",) ).strip() else: self.worker_replication_secret = worker_replication_secret diff --git a/tests/config/test_api.py b/tests/config/test_api.py
index 6773c9a277..e6cc3e21ed 100644 --- a/tests/config/test_api.py +++ b/tests/config/test_api.py
@@ -3,6 +3,7 @@ from unittest import TestCase as StdlibTestCase import yaml from synapse.config import ConfigError +from synapse.config._base import RootConfig from synapse.config.api import ApiConfig from synapse.types.state import StateFilter @@ -19,7 +20,7 @@ DEFAULT_PREJOIN_STATE_PAIRS = { class TestRoomPrejoinState(StdlibTestCase): def read_config(self, source: str) -> ApiConfig: - config = ApiConfig() + config = ApiConfig(RootConfig()) config.read_config(yaml.safe_load(source)) return config diff --git a/tests/config/test_appservice.py b/tests/config/test_appservice.py
index e3021b59d8..2572681224 100644 --- a/tests/config/test_appservice.py +++ b/tests/config/test_appservice.py
@@ -19,6 +19,7 @@ # # +from synapse.config._base import RootConfig from synapse.config.appservice import AppServiceConfig, ConfigError from tests.unittest import TestCase @@ -36,12 +37,12 @@ class AppServiceConfigTest(TestCase): ["foo", "bar", False], ]: with self.assertRaises(ConfigError): - AppServiceConfig().read_config( + AppServiceConfig(RootConfig()).read_config( {"app_service_config_files": invalid_value} ) def test_valid_app_service_config_files(self) -> None: - AppServiceConfig().read_config({"app_service_config_files": []}) - AppServiceConfig().read_config( + AppServiceConfig(RootConfig()).read_config({"app_service_config_files": []}) + AppServiceConfig(RootConfig()).read_config( {"app_service_config_files": ["/not/a/real/path", "/not/a/real/path/2"]} ) diff --git a/tests/config/test_cache.py b/tests/config/test_cache.py
index 631263b5ca..aead73e059 100644 --- a/tests/config/test_cache.py +++ b/tests/config/test_cache.py
@@ -19,6 +19,7 @@ # # +from synapse.config._base import RootConfig from synapse.config.cache import CacheConfig, add_resizable_cache from synapse.types import JsonDict from synapse.util.caches.lrucache import LruCache @@ -29,7 +30,7 @@ from tests.unittest import TestCase class CacheConfigTests(TestCase): def setUp(self) -> None: # Reset caches before each test since there's global state involved. - self.config = CacheConfig() + self.config = CacheConfig(RootConfig()) self.config.reset() def tearDown(self) -> None: diff --git a/tests/config/test_database.py b/tests/config/test_database.py
index b46519f84a..3fa5fff2b2 100644 --- a/tests/config/test_database.py +++ b/tests/config/test_database.py
@@ -20,6 +20,7 @@ import yaml +from synapse.config._base import RootConfig from synapse.config.database import DatabaseConfig from tests import unittest @@ -28,7 +29,9 @@ from tests import unittest class DatabaseConfigTestCase(unittest.TestCase): def test_database_configured_correctly(self) -> None: conf = yaml.safe_load( - DatabaseConfig().generate_config_section(data_dir_path="/data_dir_path") + DatabaseConfig(RootConfig()).generate_config_section( + data_dir_path="/data_dir_path" + ) ) expected_database_conf = { diff --git a/tests/config/test_room_directory.py b/tests/config/test_room_directory.py
index e25f7787f4..5208381279 100644 --- a/tests/config/test_room_directory.py +++ b/tests/config/test_room_directory.py
@@ -24,6 +24,7 @@ from twisted.test.proto_helpers import MemoryReactor import synapse.rest.admin import synapse.rest.client.login import synapse.rest.client.room +from synapse.config._base import RootConfig from synapse.config.room_directory import RoomDirectoryConfig from synapse.server import HomeServer from synapse.util import Clock @@ -63,7 +64,7 @@ class RoomDirectoryConfigTestCase(unittest.HomeserverTestCase): """ ) - rd_config = RoomDirectoryConfig() + rd_config = RoomDirectoryConfig(RootConfig()) rd_config.read_config(config) self.assertFalse( @@ -123,7 +124,7 @@ class RoomDirectoryConfigTestCase(unittest.HomeserverTestCase): """ ) - rd_config = RoomDirectoryConfig() + rd_config = RoomDirectoryConfig(RootConfig()) rd_config.read_config(config) self.assertFalse( diff --git a/tests/config/test_server.py b/tests/config/test_server.py
index 74073cfdc5..5eb2540439 100644 --- a/tests/config/test_server.py +++ b/tests/config/test_server.py
@@ -20,7 +20,7 @@ import yaml -from synapse.config._base import ConfigError +from synapse.config._base import ConfigError, RootConfig from synapse.config.server import ServerConfig, generate_ip_set, is_threepid_reserved from tests import unittest @@ -40,7 +40,7 @@ class ServerConfigTestCase(unittest.TestCase): def test_unsecure_listener_no_listeners_open_private_ports_false(self) -> None: conf = yaml.safe_load( - ServerConfig().generate_config_section( + ServerConfig(RootConfig()).generate_config_section( "CONFDIR", "/data_dir_path", "che.org", False, None ) ) @@ -60,7 +60,7 @@ class ServerConfigTestCase(unittest.TestCase): def test_unsecure_listener_no_listeners_open_private_ports_true(self) -> None: conf = yaml.safe_load( - ServerConfig().generate_config_section( + ServerConfig(RootConfig()).generate_config_section( "CONFDIR", "/data_dir_path", "che.org", True, None ) ) @@ -94,7 +94,7 @@ class ServerConfigTestCase(unittest.TestCase): ] conf = yaml.safe_load( - ServerConfig().generate_config_section( + ServerConfig(RootConfig()).generate_config_section( "CONFDIR", "/data_dir_path", "this.one.listens", True, listeners ) ) @@ -128,7 +128,7 @@ class ServerConfigTestCase(unittest.TestCase): expected_listeners[1]["bind_addresses"] = ["::1", "127.0.0.1"] conf = yaml.safe_load( - ServerConfig().generate_config_section( + ServerConfig(RootConfig()).generate_config_section( "CONFDIR", "/data_dir_path", "this.one.listens", True, listeners ) ) diff --git a/tests/events/test_auto_accept_invites.py b/tests/events/test_auto_accept_invites.py
index d4e87b1b7f..d2100e9903 100644 --- a/tests/events/test_auto_accept_invites.py +++ b/tests/events/test_auto_accept_invites.py
@@ -31,6 +31,7 @@ from twisted.test.proto_helpers import MemoryReactor from synapse.api.constants import EventTypes from synapse.api.errors import SynapseError +from synapse.config._base import RootConfig from synapse.config.auto_accept_invites import AutoAcceptInvitesConfig from synapse.events.auto_accept_invites import InviteAutoAccepter from synapse.federation.federation_base import event_from_pdu_json @@ -690,7 +691,7 @@ class InviteAutoAccepterInternalTestCase(TestCase): "only_from_local_users": True, } } - parsed_config = AutoAcceptInvitesConfig() + parsed_config = AutoAcceptInvitesConfig(RootConfig()) parsed_config.read_config(config) self.assertTrue(parsed_config.enabled) @@ -830,7 +831,7 @@ def create_module( if config_override is None: config_override = {} - config = AutoAcceptInvitesConfig() + config = AutoAcceptInvitesConfig(RootConfig()) config.read_config(config_override) return InviteAutoAccepter(config, module_api)