diff --git a/zilencer/management/commands/add_mock_conversation.py b/zilencer/management/commands/add_mock_conversation.py index 24cff6bae4..a718dba711 100644 --- a/zilencer/management/commands/add_mock_conversation.py +++ b/zilencer/management/commands/add_mock_conversation.py @@ -30,13 +30,11 @@ From image editing program: * Remove mute (and edit) icons from recipient bar """ - def set_avatar(self, user, filename): - # type: (UserProfile, str) -> None + def set_avatar(self, user: UserProfile, filename: str) -> None: upload_avatar_image(open(filename, 'rb'), user, user) do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER) - def add_message_formatting_conversation(self): - # type: () -> None + def add_message_formatting_conversation(self) -> None: realm = get_realm('zulip') stream, _ = create_stream_if_needed(realm, 'zulip features') @@ -106,6 +104,5 @@ From image editing program: # thumbs_up shows up second do_add_reaction_legacy(starr, preview_message, 'thumbs_up') - def handle(self, *args, **options): - # type: (*Any, **str) -> None + def handle(self, *args: Any, **options: str) -> None: self.add_message_formatting_conversation() diff --git a/zilencer/management/commands/add_new_realm.py b/zilencer/management/commands/add_new_realm.py index ccb4583599..7391d5a64f 100644 --- a/zilencer/management/commands/add_new_realm.py +++ b/zilencer/management/commands/add_new_realm.py @@ -9,8 +9,7 @@ from zerver.models import Realm, UserProfile class Command(ZulipBaseCommand): help = """Add a new realm and initial user for manual testing of the onboarding process.""" - def handle(self, **options): - # type: (**Any) -> None + def handle(self, **options: Any) -> None: string_id = 'realm%02d' % ( Realm.objects.filter(string_id__startswith='realm').count(),) realm = do_create_realm(string_id, string_id) diff --git a/zilencer/management/commands/add_new_user.py b/zilencer/management/commands/add_new_user.py index 70a5820e3c..d4d1d0deaa 100644 --- a/zilencer/management/commands/add_new_user.py +++ b/zilencer/management/commands/add_new_user.py @@ -11,12 +11,10 @@ class Command(ZulipBaseCommand): If realm is unspecified, will try to use a realm created by add_new_realm, and will otherwise fall back to the zulip realm.""" - def add_arguments(self, parser): - # type: (CommandParser) -> None + def add_arguments(self, parser: CommandParser) -> None: self.add_realm_args(parser) - def handle(self, **options): - # type: (**Any) -> None + def handle(self, **options: Any) -> None: realm = self.get_realm(options) if realm is None: realm = Realm.objects.filter(string_id__startswith='realm') \ diff --git a/zilencer/management/commands/compare_messages.py b/zilencer/management/commands/compare_messages.py index 5d377b5483..36d3764066 100644 --- a/zilencer/management/commands/compare_messages.py +++ b/zilencer/management/commands/compare_messages.py @@ -9,13 +9,11 @@ class Command(BaseCommand): Usage: ./manage.py render_messages <--amount> """ - def add_arguments(self, parser): - # type: (CommandParser) -> None + def add_arguments(self, parser: CommandParser) -> None: parser.add_argument('dump1', help='First file to compare') parser.add_argument('dump2', help='Second file to compare') - def handle(self, *args, **options): - # type: (*Any, **Any) -> None + def handle(self, *args: Any, **options: Any) -> None: total_count = 0 changed_count = 0 with open(options['dump1'], 'r') as dump1, open(options['dump2'], 'r') as dump2: diff --git a/zilencer/management/commands/mark_all_messages_unread.py b/zilencer/management/commands/mark_all_messages_unread.py index fbb0c542fe..0946bae5c6 100644 --- a/zilencer/management/commands/mark_all_messages_unread.py +++ b/zilencer/management/commands/mark_all_messages_unread.py @@ -11,8 +11,7 @@ from zerver.models import UserMessage, UserProfile class Command(BaseCommand): help = """Script to mark all messages as unread.""" - def handle(self, *args, **options): - # type: (*Any, **Any) -> None + def handle(self, *args: Any, **options: Any) -> None: assert settings.DEVELOPMENT UserMessage.objects.all().update(flags=F('flags').bitand(~UserMessage.flags.read)) UserProfile.objects.all().update(pointer=0) diff --git a/zilencer/management/commands/migrate_stream_notifications.py b/zilencer/management/commands/migrate_stream_notifications.py index 29308d30b8..32025cfa1a 100644 --- a/zilencer/management/commands/migrate_stream_notifications.py +++ b/zilencer/management/commands/migrate_stream_notifications.py @@ -8,8 +8,7 @@ from zerver.models import Subscription class Command(BaseCommand): help = """One-off script to migration users' stream notification settings.""" - def handle(self, *args, **options): - # type: (*Any, **Any) -> None + def handle(self, *args: Any, **options: Any) -> None: for subscription in Subscription.objects.all(): subscription.desktop_notifications = subscription.notifications subscription.audible_notifications = subscription.notifications diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 9b4c945bd2..0299bd10e9 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -30,8 +30,9 @@ settings.CACHES['default'] = { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache' } -def create_users(realm, name_list, bot_type=None, bot_owner=None): - # type: (Realm, Iterable[Tuple[Text, Text]], Optional[int], Optional[UserProfile]) -> None +def create_users(realm: Realm, name_list: Iterable[Tuple[Text, Text]], + bot_type: Optional[int]=None, + bot_owner: Optional[UserProfile]=None) -> None: user_set = set() # type: Set[Tuple[Text, Text, Text, bool]] for full_name, email in name_list: short_name = email_to_username(email) @@ -42,8 +43,7 @@ def create_users(realm, name_list, bot_type=None, bot_owner=None): class Command(BaseCommand): help = "Populate a test database" - def add_arguments(self, parser): - # type: (CommandParser) -> None + def add_arguments(self, parser: CommandParser) -> None: parser.add_argument('-n', '--num-messages', dest='num_messages', type=int, @@ -109,8 +109,7 @@ class Command(BaseCommand): action="store_true", help='Whether to delete all the existing messages.') - def handle(self, **options): - # type: (**Any) -> None + def handle(self, **options: Any) -> None: if options["percent_huddles"] + options["percent_personals"] > 100: self.stderr.write("Error! More than 100% of messages allocated.\n") return @@ -395,8 +394,7 @@ class Command(BaseCommand): self.stdout.write("Successfully populated test database.\n") recipient_hash = {} # type: Dict[int, Recipient] -def get_recipient_by_id(rid): - # type: (int) -> Recipient +def get_recipient_by_id(rid: int) -> Recipient: if rid in recipient_hash: return recipient_hash[rid] return Recipient.objects.get(id=rid) @@ -408,8 +406,8 @@ def get_recipient_by_id(rid): # - multiple personals converastions # - multiple messages per subject # - both single and multi-line content -def send_messages(data): - # type: (Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], Callable[[str], Any], int]) -> int +def send_messages(data: Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], + Callable[[str], Any], int]) -> int: (tot_messages, personals_pairs, options, output, random_seed) = data random.seed(random_seed) @@ -492,8 +490,7 @@ def send_messages(data): num_messages += 1 return tot_messages -def create_user_presences(user_profiles): - # type: (Iterable[UserProfile]) -> None +def create_user_presences(user_profiles: Iterable[UserProfile]) -> None: for user in user_profiles: status = 1 # type: int date = timezone_now() diff --git a/zilencer/management/commands/print_initial_password.py b/zilencer/management/commands/print_initial_password.py index deded48d20..cf8a2a3fad 100644 --- a/zilencer/management/commands/print_initial_password.py +++ b/zilencer/management/commands/print_initial_password.py @@ -9,14 +9,12 @@ class Command(ZulipBaseCommand): fmt = '%-30s %-16s %-32s' - def add_arguments(self, parser): - # type: (ArgumentParser) -> None + def add_arguments(self, parser: ArgumentParser) -> None: parser.add_argument('emails', metavar='', type=str, nargs='*', help="email of user to show password and API key for") self.add_realm_args(parser) - def handle(self, *args, **options): - # type: (*Any, **str) -> None + def handle(self, *args: Any, **options: str) -> None: realm = self.get_realm(options) print(self.fmt % ('email', 'password', 'API key')) for email in options['emails']: diff --git a/zilencer/management/commands/profile_request.py b/zilencer/management/commands/profile_request.py index 28aab92e20..4cd9b516b8 100644 --- a/zilencer/management/commands/profile_request.py +++ b/zilencer/management/commands/profile_request.py @@ -13,13 +13,11 @@ from zerver.views.messages import get_messages_backend request_logger = LogRequests() class MockSession: - def __init__(self): - # type: () -> None + def __init__(self) -> None: self.modified = False class MockRequest(HttpRequest): - def __init__(self, user): - # type: (UserProfile) -> None + def __init__(self, user: UserProfile) -> None: self.user = user self.path = '/' self.method = "POST" @@ -33,12 +31,10 @@ class MockRequest(HttpRequest): self.GET = {} # type: Dict[Any, Any] self.session = MockSession() - def get_full_path(self): - # type: () -> str + def get_full_path(self) -> str: return self.path -def profile_request(request): - # type: (HttpRequest) -> HttpResponse +def profile_request(request: HttpRequest) -> HttpResponse: request_logger.process_request(request) prof = cProfile.Profile() prof.enable() @@ -51,13 +47,11 @@ def profile_request(request): return ret class Command(ZulipBaseCommand): - def add_arguments(self, parser): - # type: (CommandParser) -> None + def add_arguments(self, parser: CommandParser) -> None: parser.add_argument("email", metavar="", type=str, help="Email address of the user") self.add_realm_args(parser) - def handle(self, *args, **options): - # type: (*Any, **Any) -> None + def handle(self, *args: Any, **options: Any) -> None: realm = self.get_realm(options) user = self.get_user(options["email"], realm) profile_request(MockRequest(user)) diff --git a/zilencer/management/commands/render_messages.py b/zilencer/management/commands/render_messages.py index 8acaec6a5d..eff27cfaca 100644 --- a/zilencer/management/commands/render_messages.py +++ b/zilencer/management/commands/render_messages.py @@ -8,8 +8,7 @@ from django.db.models import QuerySet from zerver.lib.message import render_markdown from zerver.models import Message -def queryset_iterator(queryset, chunksize=5000): - # type: (QuerySet, int) -> Iterator[Any] +def queryset_iterator(queryset: QuerySet, chunksize: int=5000) -> Iterator[Any]: queryset = queryset.order_by('id') while queryset.exists(): for row in queryset[:chunksize]: @@ -24,14 +23,12 @@ class Command(BaseCommand): Usage: ./manage.py render_messages [--amount=10000] """ - def add_arguments(self, parser): - # type: (CommandParser) -> None + def add_arguments(self, parser: CommandParser) -> None: parser.add_argument('destination', help='Destination file path') parser.add_argument('--amount', default=100000, help='Number of messages to render') parser.add_argument('--latest_id', default=0, help="Last message id to render") - def handle(self, *args, **options): - # type: (*Any, **Any) -> None + def handle(self, *args: Any, **options: Any) -> None: dest_dir = os.path.realpath(os.path.dirname(options['destination'])) amount = int(options['amount']) latest = int(options['latest_id']) or Message.objects.latest('id').id diff --git a/zilencer/management/commands/sync_api_key.py b/zilencer/management/commands/sync_api_key.py index 8a9b0ab3b3..6706a1b283 100644 --- a/zilencer/management/commands/sync_api_key.py +++ b/zilencer/management/commands/sync_api_key.py @@ -9,8 +9,7 @@ from zerver.models import UserProfile, get_realm, get_user class Command(BaseCommand): help = """Sync your API key from ~/.zuliprc into your development instance""" - def handle(self, *args, **options): - # type: (*Any, **Any) -> None + def handle(self, *args: Any, **options: Any) -> None: config_file = os.path.join(os.environ["HOME"], ".zuliprc") if not os.path.exists(config_file): raise RuntimeError("No ~/.zuliprc found") diff --git a/zilencer/models.py b/zilencer/models.py index 9d87820782..a1b7f3295a 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -5,8 +5,7 @@ from django.db import models import zerver.models -def get_remote_server_by_uuid(uuid): - # type: (Text) -> RemoteZulipServer +def get_remote_server_by_uuid(uuid: Text) -> 'RemoteZulipServer': return RemoteZulipServer.objects.get(uuid=uuid) class RemoteZulipServer(models.Model): diff --git a/zilencer/views.py b/zilencer/views.py index 6e9ffff25a..f2f0d0180e 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -15,13 +15,12 @@ from zerver.models import UserProfile from zerver.views.push_notifications import validate_token from zilencer.models import RemotePushDeviceToken, RemoteZulipServer -def validate_entity(entity): - # type: (Union[UserProfile, RemoteZulipServer]) -> None +def validate_entity(entity: Union[UserProfile, RemoteZulipServer]) -> None: if not isinstance(entity, RemoteZulipServer): raise JsonableError(_("Must validate with valid Zulip server API key")) -def validate_bouncer_token_request(entity, token, kind): - # type: (Union[UserProfile, RemoteZulipServer], bytes, int) -> None +def validate_bouncer_token_request(entity: Union[UserProfile, RemoteZulipServer], + token: bytes, kind: int) -> None: if kind not in [RemotePushDeviceToken.APNS, RemotePushDeviceToken.GCM]: raise JsonableError(_("Invalid token type")) validate_entity(entity)