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

Commit 66c4b77

Browse files
authored
Add type hints to synapse._scripts (#11297)
1 parent 5f277ff commit 66c4b77

5 files changed

Lines changed: 30 additions & 20 deletions

File tree

changelog.d/11297.misc

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

mypy.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ files =
2323
# https://docs.python.org/3/library/re.html#re.X
2424
exclude = (?x)
2525
^(
26-
|synapse/_scripts/register_new_matrix_user.py
27-
|synapse/_scripts/review_recent_signups.py
2826
|synapse/app/__init__.py
2927
|synapse/app/_base.py
3028
|synapse/app/admin_cmd.py

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def exec_file(path_segments):
110110
"types-Pillow>=8.3.4",
111111
"types-pyOpenSSL>=20.0.7",
112112
"types-PyYAML>=5.4.10",
113+
"types-requests>=2.26.0",
113114
"types-setuptools>=57.4.0",
114115
]
115116

synapse/_scripts/register_new_matrix_user.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2015, 2016 OpenMarket Ltd
22
# Copyright 2018 New Vector
3+
# Copyright 2021 The Matrix.org Foundation C.I.C.
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
56
# you may not use this file except in compliance with the License.
@@ -19,22 +20,23 @@
1920
import hmac
2021
import logging
2122
import sys
23+
from typing import Callable, Optional
2224

2325
import requests as _requests
2426
import yaml
2527

2628

2729
def request_registration(
28-
user,
29-
password,
30-
server_location,
31-
shared_secret,
32-
admin=False,
33-
user_type=None,
30+
user: str,
31+
password: str,
32+
server_location: str,
33+
shared_secret: str,
34+
admin: bool = False,
35+
user_type: Optional[str] = None,
3436
requests=_requests,
35-
_print=print,
36-
exit=sys.exit,
37-
):
37+
_print: Callable[[str], None] = print,
38+
exit: Callable[[int], None] = sys.exit,
39+
) -> None:
3840

3941
url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),)
4042

@@ -65,13 +67,13 @@ def request_registration(
6567
mac.update(b"\x00")
6668
mac.update(user_type.encode("utf8"))
6769

68-
mac = mac.hexdigest()
70+
hex_mac = mac.hexdigest()
6971

7072
data = {
7173
"nonce": nonce,
7274
"username": user,
7375
"password": password,
74-
"mac": mac,
76+
"mac": hex_mac,
7577
"admin": admin,
7678
"user_type": user_type,
7779
}
@@ -91,10 +93,17 @@ def request_registration(
9193
_print("Success!")
9294

9395

94-
def register_new_user(user, password, server_location, shared_secret, admin, user_type):
96+
def register_new_user(
97+
user: str,
98+
password: str,
99+
server_location: str,
100+
shared_secret: str,
101+
admin: Optional[bool],
102+
user_type: Optional[str],
103+
) -> None:
95104
if not user:
96105
try:
97-
default_user = getpass.getuser()
106+
default_user: Optional[str] = getpass.getuser()
98107
except Exception:
99108
default_user = None
100109

@@ -123,8 +132,8 @@ def register_new_user(user, password, server_location, shared_secret, admin, use
123132
sys.exit(1)
124133

125134
if admin is None:
126-
admin = input("Make admin [no]: ")
127-
if admin in ("y", "yes", "true"):
135+
admin_inp = input("Make admin [no]: ")
136+
if admin_inp in ("y", "yes", "true"):
128137
admin = True
129138
else:
130139
admin = False
@@ -134,7 +143,7 @@ def register_new_user(user, password, server_location, shared_secret, admin, use
134143
)
135144

136145

137-
def main():
146+
def main() -> None:
138147

139148
logging.captureWarnings(True)
140149

synapse/_scripts/review_recent_signups.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_recent_users(txn: LoggingTransaction, since_ms: int) -> List[UserInfo]:
9292
return user_infos
9393

9494

95-
def main():
95+
def main() -> None:
9696
parser = argparse.ArgumentParser()
9797
parser.add_argument(
9898
"-c",
@@ -142,7 +142,8 @@ def main():
142142
engine = create_engine(database_config.config)
143143

144144
with make_conn(database_config, engine, "review_recent_signups") as db_conn:
145-
user_infos = get_recent_users(db_conn.cursor(), since_ms)
145+
# This generates a type of Cursor, not LoggingTransaction.
146+
user_infos = get_recent_users(db_conn.cursor(), since_ms) # type: ignore[arg-type]
146147

147148
for user_info in user_infos:
148149
if exclude_users_with_email and user_info.emails:

0 commit comments

Comments
 (0)