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

Commit 8bac3e0

Browse files
author
David Robertson
authored
disallow-untyped-defs in docker and stubs directories (#12528)
1 parent 185da8f commit 8bac3e0

File tree

7 files changed

+52
-38
lines changed

7 files changed

+52
-38
lines changed

changelog.d/12528.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints so `docker` and `stubs` directories pass `mypy --disallow-untyped-defs`.

docker/configure_workers_and_start.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import os
3030
import subprocess
3131
import sys
32-
from typing import Any, Dict, Mapping, Set
32+
from typing import Any, Dict, List, Mapping, MutableMapping, NoReturn, Set
3333

3434
import jinja2
3535
import yaml
@@ -201,7 +201,7 @@
201201

202202

203203
# Utility functions
204-
def log(txt: str):
204+
def log(txt: str) -> None:
205205
"""Log something to the stdout.
206206
207207
Args:
@@ -210,7 +210,7 @@ def log(txt: str):
210210
print(txt)
211211

212212

213-
def error(txt: str):
213+
def error(txt: str) -> NoReturn:
214214
"""Log something and exit with an error code.
215215
216216
Args:
@@ -220,7 +220,7 @@ def error(txt: str):
220220
sys.exit(2)
221221

222222

223-
def convert(src: str, dst: str, **template_vars):
223+
def convert(src: str, dst: str, **template_vars: object) -> None:
224224
"""Generate a file from a template
225225
226226
Args:
@@ -290,7 +290,7 @@ def add_sharding_to_shared_config(
290290
shared_config.setdefault("media_instance_running_background_jobs", worker_name)
291291

292292

293-
def generate_base_homeserver_config():
293+
def generate_base_homeserver_config() -> None:
294294
"""Starts Synapse and generates a basic homeserver config, which will later be
295295
modified for worker support.
296296
@@ -302,12 +302,14 @@ def generate_base_homeserver_config():
302302
subprocess.check_output(["/usr/local/bin/python", "/start.py", "migrate_config"])
303303

304304

305-
def generate_worker_files(environ, config_path: str, data_dir: str):
305+
def generate_worker_files(
306+
environ: Mapping[str, str], config_path: str, data_dir: str
307+
) -> None:
306308
"""Read the desired list of workers from environment variables and generate
307309
shared homeserver, nginx and supervisord configs.
308310
309311
Args:
310-
environ: _Environ[str]
312+
environ: os.environ instance.
311313
config_path: The location of the generated Synapse main worker config file.
312314
data_dir: The location of the synapse data directory. Where log and
313315
user-facing config files live.
@@ -369,13 +371,13 @@ def generate_worker_files(environ, config_path: str, data_dir: str):
369371
nginx_locations = {}
370372

371373
# Read the desired worker configuration from the environment
372-
worker_types = environ.get("SYNAPSE_WORKER_TYPES")
373-
if worker_types is None:
374+
worker_types_env = environ.get("SYNAPSE_WORKER_TYPES")
375+
if worker_types_env is None:
374376
# No workers, just the main process
375377
worker_types = []
376378
else:
377379
# Split type names by comma
378-
worker_types = worker_types.split(",")
380+
worker_types = worker_types_env.split(",")
379381

380382
# Create the worker configuration directory if it doesn't already exist
381383
os.makedirs("/conf/workers", exist_ok=True)
@@ -547,7 +549,7 @@ def generate_worker_log_config(
547549
return log_config_filepath
548550

549551

550-
def main(args, environ):
552+
def main(args: List[str], environ: MutableMapping[str, str]) -> None:
551553
config_dir = environ.get("SYNAPSE_CONFIG_DIR", "/data")
552554
config_path = environ.get("SYNAPSE_CONFIG_PATH", config_dir + "/homeserver.yaml")
553555
data_dir = environ.get("SYNAPSE_DATA_DIR", "/data")

docker/start.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
import platform
77
import subprocess
88
import sys
9+
from typing import Any, Dict, List, Mapping, MutableMapping, NoReturn, Optional
910

1011
import jinja2
1112

1213

1314
# Utility functions
14-
def log(txt):
15+
def log(txt: str) -> None:
1516
print(txt, file=sys.stderr)
1617

1718

18-
def error(txt):
19+
def error(txt: str) -> NoReturn:
1920
log(txt)
2021
sys.exit(2)
2122

2223

23-
def convert(src, dst, environ):
24+
def convert(src: str, dst: str, environ: Mapping[str, object]) -> None:
2425
"""Generate a file from a template
2526
2627
Args:
27-
src (str): path to input file
28-
dst (str): path to file to write
29-
environ (dict): environment dictionary, for replacement mappings.
28+
src: path to input file
29+
dst: path to file to write
30+
environ: environment dictionary, for replacement mappings.
3031
"""
3132
with open(src) as infile:
3233
template = infile.read()
@@ -35,25 +36,30 @@ def convert(src, dst, environ):
3536
outfile.write(rendered)
3637

3738

38-
def generate_config_from_template(config_dir, config_path, environ, ownership):
39+
def generate_config_from_template(
40+
config_dir: str,
41+
config_path: str,
42+
os_environ: Mapping[str, str],
43+
ownership: Optional[str],
44+
) -> None:
3945
"""Generate a homeserver.yaml from environment variables
4046
4147
Args:
42-
config_dir (str): where to put generated config files
43-
config_path (str): where to put the main config file
44-
environ (dict): environment dictionary
45-
ownership (str|None): "<user>:<group>" string which will be used to set
48+
config_dir: where to put generated config files
49+
config_path: where to put the main config file
50+
os_environ: environment mapping
51+
ownership: "<user>:<group>" string which will be used to set
4652
ownership of the generated configs. If None, ownership will not change.
4753
"""
4854
for v in ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS"):
49-
if v not in environ:
55+
if v not in os_environ:
5056
error(
5157
"Environment variable '%s' is mandatory when generating a config file."
5258
% (v,)
5359
)
5460

5561
# populate some params from data files (if they exist, else create new ones)
56-
environ = environ.copy()
62+
environ: Dict[str, Any] = dict(os_environ)
5763
secrets = {
5864
"registration": "SYNAPSE_REGISTRATION_SHARED_SECRET",
5965
"macaroon": "SYNAPSE_MACAROON_SECRET_KEY",
@@ -127,12 +133,12 @@ def generate_config_from_template(config_dir, config_path, environ, ownership):
127133
subprocess.check_output(args)
128134

129135

130-
def run_generate_config(environ, ownership):
136+
def run_generate_config(environ: Mapping[str, str], ownership: Optional[str]) -> None:
131137
"""Run synapse with a --generate-config param to generate a template config file
132138
133139
Args:
134-
environ (dict): env var dict
135-
ownership (str|None): "userid:groupid" arg for chmod. If None, ownership will not change.
140+
environ: env vars from `os.enrivon`.
141+
ownership: "userid:groupid" arg for chmod. If None, ownership will not change.
136142
137143
Never returns.
138144
"""
@@ -178,7 +184,7 @@ def run_generate_config(environ, ownership):
178184
os.execv(sys.executable, args)
179185

180186

181-
def main(args, environ):
187+
def main(args: List[str], environ: MutableMapping[str, str]) -> None:
182188
mode = args[1] if len(args) > 1 else "run"
183189

184190
# if we were given an explicit user to switch to, do so

stubs/sortedcontainers/sorteddict.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SortedDict(Dict[_KT, _VT]):
103103
self,
104104
start: Optional[int] = ...,
105105
stop: Optional[int] = ...,
106-
reverse=bool,
106+
reverse: bool = ...,
107107
) -> Iterator[_KT]: ...
108108
def bisect_left(self, value: _KT) -> int: ...
109109
def bisect_right(self, value: _KT) -> int: ...

stubs/sortedcontainers/sortedlist.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class SortedList(MutableSequence[_T]):
8181
self,
8282
start: Optional[int] = ...,
8383
stop: Optional[int] = ...,
84-
reverse=bool,
84+
reverse: bool = ...,
8585
) -> Iterator[_T]: ...
8686
def _islice(
8787
self,
@@ -153,14 +153,14 @@ class SortedKeyList(SortedList[_T]):
153153
maximum: Optional[int] = ...,
154154
inclusive: Tuple[bool, bool] = ...,
155155
reverse: bool = ...,
156-
): ...
156+
) -> Iterator[_T]: ...
157157
def irange_key(
158158
self,
159159
min_key: Optional[Any] = ...,
160160
max_key: Optional[Any] = ...,
161161
inclusive: Tuple[bool, bool] = ...,
162162
reserve: bool = ...,
163-
): ...
163+
) -> Iterator[_T]: ...
164164
def bisect_left(self, value: _T) -> int: ...
165165
def bisect_right(self, value: _T) -> int: ...
166166
def bisect(self, value: _T) -> int: ...

stubs/sortedcontainers/sortedset.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SortedSet(MutableSet[_T], Sequence[_T]):
103103
self,
104104
start: Optional[int] = ...,
105105
stop: Optional[int] = ...,
106-
reverse=bool,
106+
reverse: bool = ...,
107107
) -> Iterator[_T]: ...
108108
def irange(
109109
self,

stubs/txredisapi.pyi

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ from typing import Any, List, Optional, Type, Union
1818

1919
from twisted.internet import protocol
2020
from twisted.internet.defer import Deferred
21+
from twisted.internet.interfaces import IAddress
22+
from twisted.python.failure import Failure
2123

2224
class RedisProtocol(protocol.Protocol):
2325
def publish(self, channel: str, message: bytes) -> "Deferred[None]": ...
@@ -34,11 +36,14 @@ class RedisProtocol(protocol.Protocol):
3436
def get(self, key: str) -> "Deferred[Any]": ...
3537

3638
class SubscriberProtocol(RedisProtocol):
37-
def __init__(self, *args, **kwargs): ...
39+
def __init__(self, *args: object, **kwargs: object): ...
3840
password: Optional[str]
39-
def subscribe(self, channels: Union[str, List[str]]): ...
40-
def connectionMade(self): ...
41-
def connectionLost(self, reason): ...
41+
def subscribe(self, channels: Union[str, List[str]]) -> "Deferred[None]": ...
42+
def connectionMade(self) -> None: ...
43+
# type-ignore: twisted.internet.protocol.Protocol provides a default argument for
44+
# `reason`. txredisapi's LineReceiver Protocol doesn't. But that's fine: it's what's
45+
# actually specified in twisted.internet.interfaces.IProtocol.
46+
def connectionLost(self, reason: Failure) -> None: ... # type: ignore[override]
4247

4348
def lazyConnection(
4449
host: str = ...,
@@ -74,7 +79,7 @@ class RedisFactory(protocol.ReconnectingClientFactory):
7479
replyTimeout: Optional[int] = None,
7580
convertNumbers: Optional[int] = True,
7681
): ...
77-
def buildProtocol(self, addr) -> RedisProtocol: ...
82+
def buildProtocol(self, addr: IAddress) -> RedisProtocol: ...
7883

7984
class SubscriberFactory(RedisFactory):
8085
def __init__(self) -> None: ...

0 commit comments

Comments
 (0)