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.
This commit is contained in:
Eric Eastwood
2025-05-13 10:22:15 -05:00
committed by GitHub
parent 2db54c88ff
commit 6e910e2b2c
13 changed files with 37 additions and 26 deletions

1
changelog.d/18409.misc Normal file
View File

@@ -0,0 +1 @@
Fix a couple type annotations in the `RootConfig`/`Config`.

View File

@@ -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

View File

@@ -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: ...

View File

@@ -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()

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"]}
)

View File

@@ -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:

View File

@@ -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 = {

View File

@@ -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(

View File

@@ -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
)
)

View File

@@ -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)