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

Commit 0cc3bf9

Browse files
authored
Additional type hints for the config module, part 2. (#11480)
1 parent 941ebe4 commit 0cc3bf9

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

changelog.d/11480.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints to `synapse.config` module.

synapse/config/key.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import hashlib
1717
import logging
1818
import os
19-
from typing import Any, Dict
19+
from typing import Any, Dict, Iterator, List, Optional
2020

2121
import attr
2222
import jsonschema
2323
from signedjson.key import (
2424
NACL_ED25519,
25+
SigningKey,
26+
VerifyKey,
2527
decode_signing_key_base64,
2628
decode_verify_key_bytes,
2729
generate_signing_key,
@@ -31,6 +33,7 @@
3133
)
3234
from unpaddedbase64 import decode_base64
3335

36+
from synapse.types import JsonDict
3437
from synapse.util.stringutils import random_string, random_string_with_symbols
3538

3639
from ._base import Config, ConfigError
@@ -81,14 +84,13 @@
8184
logger = logging.getLogger(__name__)
8285

8386

84-
@attr.s
87+
@attr.s(slots=True, auto_attribs=True)
8588
class TrustedKeyServer:
86-
# string: name of the server.
87-
server_name = attr.ib()
89+
# name of the server.
90+
server_name: str
8891

89-
# dict[str,VerifyKey]|None: map from key id to key object, or None to disable
90-
# signature verification.
91-
verify_keys = attr.ib(default=None)
92+
# map from key id to key object, or None to disable signature verification.
93+
verify_keys: Optional[Dict[str, VerifyKey]] = None
9294

9395

9496
class KeyConfig(Config):
@@ -279,15 +281,15 @@ def generate_config_section(
279281
% locals()
280282
)
281283

282-
def read_signing_keys(self, signing_key_path, name):
284+
def read_signing_keys(self, signing_key_path: str, name: str) -> List[SigningKey]:
283285
"""Read the signing keys in the given path.
284286
285287
Args:
286-
signing_key_path (str)
287-
name (str): Associated config key name
288+
signing_key_path
289+
name: Associated config key name
288290
289291
Returns:
290-
list[SigningKey]
292+
The signing keys read from the given path.
291293
"""
292294

293295
signing_keys = self.read_file(signing_key_path, name)
@@ -296,7 +298,9 @@ def read_signing_keys(self, signing_key_path, name):
296298
except Exception as e:
297299
raise ConfigError("Error reading %s: %s" % (name, str(e)))
298300

299-
def read_old_signing_keys(self, old_signing_keys):
301+
def read_old_signing_keys(
302+
self, old_signing_keys: Optional[JsonDict]
303+
) -> Dict[str, VerifyKey]:
300304
if old_signing_keys is None:
301305
return {}
302306
keys = {}
@@ -340,7 +344,7 @@ def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
340344
write_signing_keys(signing_key_file, (key,))
341345

342346

343-
def _perspectives_to_key_servers(config):
347+
def _perspectives_to_key_servers(config: JsonDict) -> Iterator[JsonDict]:
344348
"""Convert old-style 'perspectives' configs into new-style 'trusted_key_servers'
345349
346350
Returns an iterable of entries to add to trusted_key_servers.
@@ -402,7 +406,9 @@ def _perspectives_to_key_servers(config):
402406
}
403407

404408

405-
def _parse_key_servers(key_servers, federation_verify_certificates):
409+
def _parse_key_servers(
410+
key_servers: List[Any], federation_verify_certificates: bool
411+
) -> Iterator[TrustedKeyServer]:
406412
try:
407413
jsonschema.validate(key_servers, TRUSTED_KEY_SERVERS_SCHEMA)
408414
except jsonschema.ValidationError as e:
@@ -444,7 +450,7 @@ def _parse_key_servers(key_servers, federation_verify_certificates):
444450
yield result
445451

446452

447-
def _assert_keyserver_has_verify_keys(trusted_key_server):
453+
def _assert_keyserver_has_verify_keys(trusted_key_server: TrustedKeyServer) -> None:
448454
if not trusted_key_server.verify_keys:
449455
raise ConfigError(INSECURE_NOTARY_ERROR)
450456

synapse/config/metrics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
@attr.s
2424
class MetricsFlags:
25-
known_servers = attr.ib(default=False, validator=attr.validators.instance_of(bool))
25+
known_servers: bool = attr.ib(
26+
default=False, validator=attr.validators.instance_of(bool)
27+
)
2628

2729
@classmethod
28-
def all_off(cls):
30+
def all_off(cls) -> "MetricsFlags":
2931
"""
3032
Instantiate the flags with all options set to off.
3133
"""

synapse/config/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
12571257
help="Turn on the twisted telnet manhole service on the given port.",
12581258
)
12591259

1260-
def read_gc_intervals(self, durations) -> Optional[Tuple[float, float, float]]:
1260+
def read_gc_intervals(self, durations: Any) -> Optional[Tuple[float, float, float]]:
12611261
"""Reads the three durations for the GC min interval option, returning seconds."""
12621262
if durations is None:
12631263
return None

synapse/config/tls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def read_config(self, config: dict, config_dir_path: str, **kwargs):
132132
self.tls_certificate: Optional[crypto.X509] = None
133133
self.tls_private_key: Optional[crypto.PKey] = None
134134

135-
def read_certificate_from_disk(self):
135+
def read_certificate_from_disk(self) -> None:
136136
"""
137137
Read the certificates and private key from disk.
138138
"""

0 commit comments

Comments
 (0)