Add type hints to synapse._scripts (#11297)

pull/11302/head
Patrick Cloke 2021-11-10 12:55:32 -05:00 committed by GitHub
parent 5f277ffe89
commit 66c4b774fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 20 deletions

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

@ -0,0 +1 @@
Add type hints to `synapse._scripts`.

View File

@ -23,8 +23,6 @@ files =
# https://docs.python.org/3/library/re.html#re.X # https://docs.python.org/3/library/re.html#re.X
exclude = (?x) exclude = (?x)
^( ^(
|synapse/_scripts/register_new_matrix_user.py
|synapse/_scripts/review_recent_signups.py
|synapse/app/__init__.py |synapse/app/__init__.py
|synapse/app/_base.py |synapse/app/_base.py
|synapse/app/admin_cmd.py |synapse/app/admin_cmd.py

View File

@ -110,6 +110,7 @@ CONDITIONAL_REQUIREMENTS["mypy"] = [
"types-Pillow>=8.3.4", "types-Pillow>=8.3.4",
"types-pyOpenSSL>=20.0.7", "types-pyOpenSSL>=20.0.7",
"types-PyYAML>=5.4.10", "types-PyYAML>=5.4.10",
"types-requests>=2.26.0",
"types-setuptools>=57.4.0", "types-setuptools>=57.4.0",
] ]

View File

@ -1,5 +1,6 @@
# Copyright 2015, 2016 OpenMarket Ltd # Copyright 2015, 2016 OpenMarket Ltd
# Copyright 2018 New Vector # Copyright 2018 New Vector
# Copyright 2021 The Matrix.org Foundation C.I.C.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -19,22 +20,23 @@ import hashlib
import hmac import hmac
import logging import logging
import sys import sys
from typing import Callable, Optional
import requests as _requests import requests as _requests
import yaml import yaml
def request_registration( def request_registration(
user, user: str,
password, password: str,
server_location, server_location: str,
shared_secret, shared_secret: str,
admin=False, admin: bool = False,
user_type=None, user_type: Optional[str] = None,
requests=_requests, requests=_requests,
_print=print, _print: Callable[[str], None] = print,
exit=sys.exit, exit: Callable[[int], None] = sys.exit,
): ) -> None:
url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),) url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),)
@ -65,13 +67,13 @@ def request_registration(
mac.update(b"\x00") mac.update(b"\x00")
mac.update(user_type.encode("utf8")) mac.update(user_type.encode("utf8"))
mac = mac.hexdigest() hex_mac = mac.hexdigest()
data = { data = {
"nonce": nonce, "nonce": nonce,
"username": user, "username": user,
"password": password, "password": password,
"mac": mac, "mac": hex_mac,
"admin": admin, "admin": admin,
"user_type": user_type, "user_type": user_type,
} }
@ -91,10 +93,17 @@ def request_registration(
_print("Success!") _print("Success!")
def register_new_user(user, password, server_location, shared_secret, admin, user_type): def register_new_user(
user: str,
password: str,
server_location: str,
shared_secret: str,
admin: Optional[bool],
user_type: Optional[str],
) -> None:
if not user: if not user:
try: try:
default_user = getpass.getuser() default_user: Optional[str] = getpass.getuser()
except Exception: except Exception:
default_user = None default_user = None
@ -123,8 +132,8 @@ def register_new_user(user, password, server_location, shared_secret, admin, use
sys.exit(1) sys.exit(1)
if admin is None: if admin is None:
admin = input("Make admin [no]: ") admin_inp = input("Make admin [no]: ")
if admin in ("y", "yes", "true"): if admin_inp in ("y", "yes", "true"):
admin = True admin = True
else: else:
admin = False admin = False
@ -134,7 +143,7 @@ def register_new_user(user, password, server_location, shared_secret, admin, use
) )
def main(): def main() -> None:
logging.captureWarnings(True) logging.captureWarnings(True)

View File

@ -92,7 +92,7 @@ def get_recent_users(txn: LoggingTransaction, since_ms: int) -> List[UserInfo]:
return user_infos return user_infos
def main(): def main() -> None:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
"-c", "-c",
@ -142,7 +142,8 @@ def main():
engine = create_engine(database_config.config) engine = create_engine(database_config.config)
with make_conn(database_config, engine, "review_recent_signups") as db_conn: with make_conn(database_config, engine, "review_recent_signups") as db_conn:
user_infos = get_recent_users(db_conn.cursor(), since_ms) # This generates a type of Cursor, not LoggingTransaction.
user_infos = get_recent_users(db_conn.cursor(), since_ms) # type: ignore[arg-type]
for user_info in user_infos: for user_info in user_infos:
if exclude_users_with_email and user_info.emails: if exclude_users_with_email and user_info.emails: