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

Commit 961ee75

Browse files
author
David Robertson
authored
Disallow untyped defs in synapse._scripts (#12422)
Of note: * No untyped defs in `register_new_matrix_user` This one might be contraversial. `request_registration` has three dependency-injection arguments used for testing. I'm removing the injection of the `requests` module and using `unitest.mock.patch` in the test cases instead. Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the change: ``` synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any" synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any" ``` And after: ``` synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType" synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response" ``` * Drive-by comment in `synapse.storage.types` * No untyped defs in `synapse_port_db` This was by far the most painful. I'm happy to break this up into smaller pieces for review if it's not managable as-is.
1 parent 5f72ea1 commit 961ee75

14 files changed

+221
-140
lines changed

changelog.d/12422.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make `synapse._scripts` pass type checks.

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ exclude = (?x)
9393
|tests/utils.py
9494
)$
9595

96+
[mypy-synapse._scripts.*]
97+
disallow_untyped_defs = True
98+
9699
[mypy-synapse.api.*]
97100
disallow_untyped_defs = True
98101

synapse/_scripts/export_signing_key.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
import argparse
1616
import sys
1717
import time
18-
from typing import Optional
18+
from typing import NoReturn, Optional
1919

2020
from signedjson.key import encode_verify_key_base64, get_verify_key, read_signing_keys
2121
from signedjson.types import VerifyKey
2222

2323

24-
def exit(status: int = 0, message: Optional[str] = None):
24+
def exit(status: int = 0, message: Optional[str] = None) -> NoReturn:
2525
if message:
2626
print(message, file=sys.stderr)
2727
sys.exit(status)
2828

2929

30-
def format_plain(public_key: VerifyKey):
30+
def format_plain(public_key: VerifyKey) -> None:
3131
print(
3232
"%s:%s %s"
3333
% (
@@ -38,7 +38,7 @@ def format_plain(public_key: VerifyKey):
3838
)
3939

4040

41-
def format_for_config(public_key: VerifyKey, expiry_ts: int):
41+
def format_for_config(public_key: VerifyKey, expiry_ts: int) -> None:
4242
print(
4343
' "%s:%s": { key: "%s", expired_ts: %i }'
4444
% (
@@ -50,7 +50,7 @@ def format_for_config(public_key: VerifyKey, expiry_ts: int):
5050
)
5151

5252

53-
def main():
53+
def main() -> None:
5454
parser = argparse.ArgumentParser()
5555

5656
parser.add_argument(
@@ -94,7 +94,6 @@ def main():
9494
message="Error reading key from file %s: %s %s"
9595
% (file.name, type(e), e),
9696
)
97-
res = []
9897
for key in res:
9998
formatter(get_verify_key(key))
10099

synapse/_scripts/generate_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from synapse.config.homeserver import HomeServerConfig
88

99

10-
def main():
10+
def main() -> None:
1111
parser = argparse.ArgumentParser()
1212
parser.add_argument(
1313
"--config-dir",

synapse/_scripts/generate_log_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from synapse.config.logger import DEFAULT_LOG_CONFIG
2121

2222

23-
def main():
23+
def main() -> None:
2424
parser = argparse.ArgumentParser()
2525

2626
parser.add_argument(

synapse/_scripts/generate_signing_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from synapse.util.stringutils import random_string
2121

2222

23-
def main():
23+
def main() -> None:
2424
parser = argparse.ArgumentParser()
2525

2626
parser.add_argument(

synapse/_scripts/hash_password.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import yaml
1010

1111

12-
def prompt_for_pass():
12+
def prompt_for_pass() -> str:
1313
password = getpass.getpass("Password: ")
1414

1515
if not password:
@@ -23,7 +23,7 @@ def prompt_for_pass():
2323
return password
2424

2525

26-
def main():
26+
def main() -> None:
2727
bcrypt_rounds = 12
2828
password_pepper = ""
2929

synapse/_scripts/move_remote_media_to_new_store.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
logger = logging.getLogger()
4343

4444

45-
def main(src_repo, dest_repo):
45+
def main(src_repo: str, dest_repo: str) -> None:
4646
src_paths = MediaFilePaths(src_repo)
4747
dest_paths = MediaFilePaths(dest_repo)
4848
for line in sys.stdin:
@@ -55,14 +55,19 @@ def main(src_repo, dest_repo):
5555
move_media(parts[0], parts[1], src_paths, dest_paths)
5656

5757

58-
def move_media(origin_server, file_id, src_paths, dest_paths):
58+
def move_media(
59+
origin_server: str,
60+
file_id: str,
61+
src_paths: MediaFilePaths,
62+
dest_paths: MediaFilePaths,
63+
) -> None:
5964
"""Move the given file, and any thumbnails, to the dest repo
6065
6166
Args:
62-
origin_server (str):
63-
file_id (str):
64-
src_paths (MediaFilePaths):
65-
dest_paths (MediaFilePaths):
67+
origin_server:
68+
file_id:
69+
src_paths:
70+
dest_paths:
6671
"""
6772
logger.info("%s/%s", origin_server, file_id)
6873

@@ -91,7 +96,7 @@ def move_media(origin_server, file_id, src_paths, dest_paths):
9196
)
9297

9398

94-
def mkdir_and_move(original_file, dest_file):
99+
def mkdir_and_move(original_file: str, dest_file: str) -> None:
95100
dirname = os.path.dirname(dest_file)
96101
if not os.path.exists(dirname):
97102
logger.debug("mkdir %s", dirname)

synapse/_scripts/register_new_matrix_user.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import sys
2323
from typing import Callable, Optional
2424

25-
import requests as _requests
25+
import requests
2626
import yaml
2727

2828

@@ -33,7 +33,6 @@ def request_registration(
3333
shared_secret: str,
3434
admin: bool = False,
3535
user_type: Optional[str] = None,
36-
requests=_requests,
3736
_print: Callable[[str], None] = print,
3837
exit: Callable[[int], None] = sys.exit,
3938
) -> None:

0 commit comments

Comments
 (0)