From 2665a3ce2b449ab3736f64f3b44f47f0ce2e867c Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 12 Sep 2023 14:19:57 -0700 Subject: [PATCH] python: Elide unnecessary list wrappers. Signed-off-by: Anders Kaseorg --- analytics/lib/time_utils.py | 3 ++- analytics/tests/test_counts.py | 8 ++++---- analytics/views/installation_activity.py | 5 +---- analytics/views/stats.py | 3 +-- docs/tutorials/new-feature-tutorial.md | 2 +- puppet/zulip/files/postgresql/wal-g-exporter | 2 +- scripts/log-search | 2 +- tools/droplets/create.py | 2 +- tools/setup/emoji/generate_emoji_names_table | 2 +- zerver/actions/invites.py | 10 ++++------ zerver/actions/message_edit.py | 2 +- zerver/actions/message_flags.py | 2 +- zerver/actions/realm_settings.py | 18 ++++++++--------- zerver/data_import/rocketchat.py | 6 ++---- zerver/data_import/slack.py | 2 +- zerver/lib/data_types.py | 2 +- zerver/lib/digest.py | 3 +-- zerver/lib/events.py | 9 +++------ zerver/lib/export.py | 20 +++++++++---------- zerver/lib/markdown/__init__.py | 14 +++++-------- zerver/lib/markdown/nested_code_blocks.py | 4 ++-- zerver/lib/message.py | 8 +++++--- zerver/lib/onboarding.py | 2 +- zerver/lib/push_notifications.py | 10 ++++------ zerver/lib/request.py | 6 +++--- zerver/lib/stream_subscription.py | 2 +- zerver/lib/streams.py | 5 +++-- zerver/lib/string_validation.py | 9 ++++++--- zerver/lib/typed_endpoint.py | 6 +++--- zerver/lib/user_counts.py | 2 +- zerver/lib/user_topics.py | 2 +- zerver/lib/users.py | 6 ++---- zerver/lib/validator.py | 2 +- zerver/management/commands/export_search.py | 2 +- ...84_convert_realm_admins_to_realm_owners.py | 2 +- .../0371_invalid_characters_in_topics.py | 9 ++++++--- ...0375_invalid_characters_in_stream_names.py | 9 ++++++--- ...voke_invitations_from_deactivated_users.py | 12 +++++------ zerver/tests/test_auth_backends.py | 8 ++++---- zerver/tests/test_message_fetch.py | 6 +++--- zerver/tests/test_user_groups.py | 12 +++++------ zerver/views/development/dev_login.py | 2 +- zerver/views/documentation.py | 2 +- zerver/views/realm.py | 12 +++++------ zerver/views/user_settings.py | 4 ++-- zerver/webhooks/github/view.py | 2 +- zerver/webhooks/gitlab/view.py | 2 +- zerver/webhooks/jotform/view.py | 4 ++-- zerver/webhooks/slack/view.py | 2 +- 49 files changed, 129 insertions(+), 142 deletions(-) diff --git a/analytics/lib/time_utils.py b/analytics/lib/time_utils.py index 888baf96d5..0afa73632e 100644 --- a/analytics/lib/time_utils.py +++ b/analytics/lib/time_utils.py @@ -30,4 +30,5 @@ def time_range( while current >= start: times.append(current) current -= step - return list(reversed(times)) + times.reverse() + return times diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.py index 00a9af7fbd..d182b71444 100644 --- a/analytics/tests/test_counts.py +++ b/analytics/tests/test_counts.py @@ -1509,11 +1509,11 @@ class TestDeleteStats(AnalyticsTestCase): FillState.objects.create(property="test", end_time=self.TIME_ZERO, state=FillState.DONE) analytics = apps.get_app_config("analytics") - for table in list(analytics.models.values()): + for table in analytics.models.values(): self.assertTrue(table._default_manager.exists()) do_drop_all_analytics_tables() - for table in list(analytics.models.values()): + for table in analytics.models.values(): self.assertFalse(table._default_manager.exists()) def test_do_drop_single_stat(self) -> None: @@ -1533,11 +1533,11 @@ class TestDeleteStats(AnalyticsTestCase): FillState.objects.create(property="to_save", end_time=self.TIME_ZERO, state=FillState.DONE) analytics = apps.get_app_config("analytics") - for table in list(analytics.models.values()): + for table in analytics.models.values(): self.assertTrue(table._default_manager.exists()) do_drop_single_stat("to_delete") - for table in list(analytics.models.values()): + for table in analytics.models.values(): self.assertFalse(table._default_manager.filter(property="to_delete").exists()) self.assertTrue(table._default_manager.filter(property="to_save").exists()) diff --git a/analytics/views/installation_activity.py b/analytics/views/installation_activity.py index 5588ddbf09..4d143160b3 100644 --- a/analytics/views/installation_activity.py +++ b/analytics/views/installation_activity.py @@ -254,10 +254,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str: row["string_id"] = realm_activity_link(row["string_id"]) # Count active sites - def meets_goal(row: Dict[str, int]) -> bool: - return row["dau_count"] >= 5 - - num_active_sites = len(list(filter(meets_goal, rows))) + num_active_sites = sum(row["dau_count"] >= 5 for row in rows) # create totals total_dau_count = 0 diff --git a/analytics/views/stats.py b/analytics/views/stats.py index 2b7e01ee38..203a0dedb8 100644 --- a/analytics/views/stats.py +++ b/analytics/views/stats.py @@ -438,8 +438,7 @@ def get_chart_data( def sort_by_totals(value_arrays: Dict[str, List[int]]) -> List[str]: - totals = [(sum(values), label) for label, values in value_arrays.items()] - totals.sort(reverse=True) + totals = sorted(((sum(values), label) for label, values in value_arrays.items()), reverse=True) return [label for total, label in totals] diff --git a/docs/tutorials/new-feature-tutorial.md b/docs/tutorials/new-feature-tutorial.md index 2d310e0b40..e871f2a552 100644 --- a/docs/tutorials/new-feature-tutorial.md +++ b/docs/tutorials/new-feature-tutorial.md @@ -343,7 +343,7 @@ field and send an event. For example: def do_set_realm_authentication_methods( realm: Realm, authentication_methods: Dict[str, bool], *, acting_user: Optional[UserProfile] ) -> None: - for key, value in list(authentication_methods.items()): + for key, value in authentication_methods.items(): index = getattr(realm.authentication_methods, key).number realm.authentication_methods.set_bit(index, int(value)) realm.save(update_fields=['authentication_methods']) diff --git a/puppet/zulip/files/postgresql/wal-g-exporter b/puppet/zulip/files/postgresql/wal-g-exporter index c0e7a9565c..edb2790717 100755 --- a/puppet/zulip/files/postgresql/wal-g-exporter +++ b/puppet/zulip/files/postgresql/wal-g-exporter @@ -36,7 +36,7 @@ class WalGPrometheusServer(BaseHTTPRequestHandler): def inner(value: float, labels: Optional[Mapping[str, str]] = None) -> None: label_str = "" if labels: - label_str = "{" + ",".join([f'{k}="{v}"' for k, v in labels.items()]) + "}" + label_str = "{" + ",".join(f'{k}="{v}"' for k, v in labels.items()) + "}" self.metric_values[name][label_str] = f"{self.METRIC_PREFIX}{name}{label_str} {value}" if default_value is not None: diff --git a/scripts/log-search b/scripts/log-search index bb6a6f5b30..57e5b17c07 100755 --- a/scripts/log-search +++ b/scripts/log-search @@ -472,7 +472,7 @@ def print_line( url, ] - print(color + " ".join([p for p in parts if p is not None]) + (ENDC if use_color else "")) + print(color + " ".join(p for p in parts if p is not None) + (ENDC if use_color else "")) if __name__ == "__main__": diff --git a/tools/droplets/create.py b/tools/droplets/create.py index 64aa03d347..1516a61cae 100644 --- a/tools/droplets/create.py +++ b/tools/droplets/create.py @@ -109,7 +109,7 @@ def assert_droplet_does_not_exist(my_token: str, droplet_name: str, recreate: bo def get_ssh_keys_string_from_github_ssh_key_dicts(userkey_dicts: List[Dict[str, Any]]) -> str: - return "\n".join([userkey_dict["key"] for userkey_dict in userkey_dicts]) + return "\n".join(userkey_dict["key"] for userkey_dict in userkey_dicts) def generate_dev_droplet_user_data( diff --git a/tools/setup/emoji/generate_emoji_names_table b/tools/setup/emoji/generate_emoji_names_table index 8be9fbce95..ea9b109e61 100755 --- a/tools/setup/emoji/generate_emoji_names_table +++ b/tools/setup/emoji/generate_emoji_names_table @@ -157,7 +157,7 @@ def generate_emoji_code_to_emoji_names_maps() -> None: def get_sorting_info(category: str, sort_order: int) -> str: - return " ".join([category, str(sort_order)]) + return f"{category} {sort_order}" def get_images_html(emoji_code: str) -> str: diff --git a/zerver/actions/invites.py b/zerver/actions/invites.py index b0fd6d29ae..82fcc480b5 100644 --- a/zerver/actions/invites.py +++ b/zerver/actions/invites.py @@ -422,12 +422,10 @@ def get_valid_invite_confirmations_generated_by_user( multiuse_invite_ids = MultiuseInvite.objects.filter(referred_by=user_profile).values_list( "id", flat=True ) - confirmations += list( - Confirmation.objects.filter( - type=Confirmation.MULTIUSE_INVITE, - object_id__in=multiuse_invite_ids, - ).filter(Q(expiry_date__gte=timezone_now()) | Q(expiry_date=None)) - ) + confirmations += Confirmation.objects.filter( + type=Confirmation.MULTIUSE_INVITE, + object_id__in=multiuse_invite_ids, + ).filter(Q(expiry_date__gte=timezone_now()) | Q(expiry_date=None)) return confirmations diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.py index ff64b1a9b2..c75298f087 100644 --- a/zerver/actions/message_edit.py +++ b/zerver/actions/message_edit.py @@ -774,7 +774,7 @@ def do_update_message( ) subscriber_ids = set(subscriptions.values_list("user_profile_id", flat=True)) - users_to_be_notified += list(map(subscriber_info, sorted(subscriber_ids))) + users_to_be_notified += map(subscriber_info, sorted(subscriber_ids)) # UserTopic updates and the content of notifications depend on # whether we've moved the entire topic, or just part of it. We diff --git a/zerver/actions/message_flags.py b/zerver/actions/message_flags.py index 1b4da063bf..4b3f9abe45 100644 --- a/zerver/actions/message_flags.py +++ b/zerver/actions/message_flags.py @@ -224,7 +224,7 @@ def do_clear_mobile_push_notifications_for_ids( assert len(user_profile_ids) == 1 or len(message_ids) == 1 messages_by_user = defaultdict(list) - notifications_to_update = list( + notifications_to_update = ( UserMessage.objects.filter( message_id__in=message_ids, user_profile_id__in=user_profile_ids, diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.py index ec453339c6..e5c20c720a 100644 --- a/zerver/actions/realm_settings.py +++ b/zerver/actions/realm_settings.py @@ -169,7 +169,7 @@ def do_set_realm_authentication_methods( ) -> None: old_value = realm.authentication_methods_dict() with transaction.atomic(): - for key, value in list(authentication_methods.items()): + for key, value in authentication_methods.items(): # This does queries in a loop, but this isn't a performance sensitive # path and is only run rarely. if value: @@ -409,15 +409,13 @@ def do_scrub_realm(realm: Realm, *, acting_user: Optional[UserProfile]) -> None: # more secure against bugs that may cause Message.realm to be incorrect for some # cross-realm messages to also determine the actual Recipients - to prevent # deletion of excessive messages. - all_recipient_ids_in_realm = ( - list(Stream.objects.filter(realm=realm).values_list("recipient_id", flat=True)) - + list(UserProfile.objects.filter(realm=realm).values_list("recipient_id", flat=True)) - + list( - Subscription.objects.filter( - recipient__type=Recipient.HUDDLE, user_profile__realm=realm - ).values_list("recipient_id", flat=True) - ) - ) + all_recipient_ids_in_realm = [ + *Stream.objects.filter(realm=realm).values_list("recipient_id", flat=True), + *UserProfile.objects.filter(realm=realm).values_list("recipient_id", flat=True), + *Subscription.objects.filter( + recipient__type=Recipient.HUDDLE, user_profile__realm=realm + ).values_list("recipient_id", flat=True), + ] cross_realm_bot_message_ids = list( Message.objects.filter( # Filtering by both message.recipient and message.realm is diff --git a/zerver/data_import/rocketchat.py b/zerver/data_import/rocketchat.py index 2ffbd73948..7a88ad2d6e 100644 --- a/zerver/data_import/rocketchat.py +++ b/zerver/data_import/rocketchat.py @@ -284,7 +284,7 @@ def build_custom_emoji( # Build custom emoji for rc_emoji in custom_emoji_data["emoji"]: # Subject to change with changes in database - emoji_file_id = ".".join([rc_emoji["name"], rc_emoji["extension"]]) + emoji_file_id = f'{rc_emoji["name"]}.{rc_emoji["extension"]}' emoji_file_info = emoji_file_data[emoji_file_id] @@ -849,9 +849,7 @@ def separate_channel_private_and_livechat_messages( private_messages: List[Dict[str, Any]], livechat_messages: List[Dict[str, Any]], ) -> None: - private_channels_list = list(direct_id_to_direct_map.keys()) + list( - huddle_id_to_huddle_map.keys() - ) + private_channels_list = [*direct_id_to_direct_map, *huddle_id_to_huddle_map] for message in messages: if not message.get("rid"): # Message does not belong to any channel (might be diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py index e14fdec317..4f1ae26b61 100644 --- a/zerver/data_import/slack.py +++ b/zerver/data_import/slack.py @@ -797,7 +797,7 @@ def get_messages_iterator( not read all the messages into memory at once, because for large imports that can OOM kill.""" - dir_names = list(added_channels.keys()) + list(added_mpims.keys()) + list(dm_members.keys()) + dir_names = [*added_channels, *added_mpims, *dm_members] all_json_names: Dict[str, List[str]] = defaultdict(list) for dir_name in dir_names: dir_path = os.path.join(slack_data_dir, dir_name) diff --git a/zerver/lib/data_types.py b/zerver/lib/data_types.py index 20ee07ace7..1336fb1f33 100644 --- a/zerver/lib/data_types.py +++ b/zerver/lib/data_types.py @@ -67,7 +67,7 @@ class DictType: def schema(self, var_name: str) -> str: # Our current schema is lossy, since our OpenAPI configs # aren't rigorous about "required" fields yet. - keys = sorted(list(self.required_keys) + list(self.optional_keys)) + keys = sorted([*self.required_keys, *self.optional_keys]) sub_schema = "\n".join(schema(name, data_type) for name, data_type in keys) return f"{var_name} (dict):\n{indent(sub_schema)}" diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py index e184947df3..24b84fca29 100644 --- a/zerver/lib/digest.py +++ b/zerver/lib/digest.py @@ -145,8 +145,7 @@ def _enqueue_emails_for_realm(realm: Realm, cutoff: datetime.datetime) -> None: .distinct() ) - user_ids = list(realm_user_ids - active_user_ids) - user_ids.sort() + user_ids = sorted(realm_user_ids - active_user_ids) # We process batches of 30. We want a big enough batch # to amortize work, but not so big that a single item diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 87f282bbec..7e5c2ff00f 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -1349,8 +1349,7 @@ def apply_event( for user_group in state["realm_user_groups"]: if user_group["id"] == event["group_id"]: members = set(user_group["members"]) - user_group["members"] = list(members - set(event["user_ids"])) - user_group["members"].sort() + user_group["members"] = sorted(members - set(event["user_ids"])) elif event["op"] == "add_subgroups": for user_group in state["realm_user_groups"]: if user_group["id"] == event["group_id"]: @@ -1360,10 +1359,9 @@ def apply_event( for user_group in state["realm_user_groups"]: if user_group["id"] == event["group_id"]: subgroups = set(user_group["direct_subgroup_ids"]) - user_group["direct_subgroup_ids"] = list( + user_group["direct_subgroup_ids"] = sorted( subgroups - set(event["direct_subgroup_ids"]) ) - user_group["direct_subgroup_ids"].sort() elif event["op"] == "remove": state["realm_user_groups"] = [ ug for ug in state["realm_user_groups"] if ug["id"] != event["group_id"] @@ -1597,8 +1595,7 @@ def post_process_state( See the note above; the same technique applies below. """ if "raw_users" in ret: - user_dicts = list(ret["raw_users"].values()) - user_dicts = sorted(user_dicts, key=lambda x: x["user_id"]) + user_dicts = sorted(ret["raw_users"].values(), key=lambda x: x["user_id"]) ret["realm_users"] = [d for d in user_dicts if d["is_active"]] ret["realm_non_active_users"] = [d for d in user_dicts if not d["is_active"]] diff --git a/zerver/lib/export.py b/zerver/lib/export.py index c1d44189f5..845deeaaaa 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -308,16 +308,16 @@ DATE_FIELDS: Dict[TableName, List[Field]] = { def sanity_check_output(data: TableData) -> None: # First, we verify that the export tool has a declared # configuration for every table declared in the `models.py` files. - target_models = ( - list(apps.get_app_config("analytics").get_models(include_auto_created=True)) - + list(apps.get_app_config("django_otp").get_models(include_auto_created=True)) - + list(apps.get_app_config("otp_static").get_models(include_auto_created=True)) - + list(apps.get_app_config("otp_totp").get_models(include_auto_created=True)) - + list(apps.get_app_config("phonenumber").get_models(include_auto_created=True)) - + list(apps.get_app_config("social_django").get_models(include_auto_created=True)) - + list(apps.get_app_config("two_factor").get_models(include_auto_created=True)) - + list(apps.get_app_config("zerver").get_models(include_auto_created=True)) - ) + target_models = [ + *apps.get_app_config("analytics").get_models(include_auto_created=True), + *apps.get_app_config("django_otp").get_models(include_auto_created=True), + *apps.get_app_config("otp_static").get_models(include_auto_created=True), + *apps.get_app_config("otp_totp").get_models(include_auto_created=True), + *apps.get_app_config("phonenumber").get_models(include_auto_created=True), + *apps.get_app_config("social_django").get_models(include_auto_created=True), + *apps.get_app_config("two_factor").get_models(include_auto_created=True), + *apps.get_app_config("zerver").get_models(include_auto_created=True), + ] all_tables_db = {model._meta.db_table for model in target_models} # These assertion statements will fire when we add a new database diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index 4808511222..01b1a24a11 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -333,10 +333,7 @@ def image_preview_enabled( def list_of_tlds() -> List[str]: # Skip a few overly-common false-positives from file extensions common_false_positives = {"java", "md", "mov", "py", "zip"} - tlds = list(tld_set - common_false_positives) - - tlds.sort(key=len, reverse=True) - return tlds + return sorted(tld_set - common_false_positives, key=len, reverse=True) def walk_tree( @@ -762,9 +759,7 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor): if image_info is None: image_info = {} image_info["is_image"] = True - parsed_url_list = list(parsed_url) - parsed_url_list[4] = "raw=1" # Replaces query - image_info["image"] = urllib.parse.urlunparse(parsed_url_list) + image_info["image"] = parsed_url._replace(query="raw=1").geturl() return image_info return None @@ -992,8 +987,9 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor): # Find the image size that is smaller than # TWITTER_MAX_IMAGE_HEIGHT px tall or the smallest - size_name_tuples = list(media_item["sizes"].items()) - size_name_tuples.sort(reverse=True, key=lambda x: x[1]["h"]) + size_name_tuples = sorted( + media_item["sizes"].items(), reverse=True, key=lambda x: x[1]["h"] + ) for size_name, size in size_name_tuples: if size["h"] < self.TWITTER_MAX_IMAGE_HEIGHT: break diff --git a/zerver/lib/markdown/nested_code_blocks.py b/zerver/lib/markdown/nested_code_blocks.py index 3be6cbf336..56f81e7d71 100644 --- a/zerver/lib/markdown/nested_code_blocks.py +++ b/zerver/lib/markdown/nested_code_blocks.py @@ -46,8 +46,8 @@ class NestedCodeBlocksRendererTreeProcessor(markdown.treeprocessors.Treeprocesso parent.tag == "p" and grandparent.tag == "li" and parent.text is None - and len(list(parent)) == 1 - and len(list(parent.itertext())) == 1 + and len(parent) == 1 + and sum(1 for text in parent.itertext()) == 1 ): # if the parent (

) has no text, and no children, # that means that the element inside is its diff --git a/zerver/lib/message.py b/zerver/lib/message.py index 0d2286257a..d85f373ff6 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -1401,9 +1401,11 @@ def format_unread_message_details( for message_id, huddle_message_details in raw_unread_data["huddle_dict"].items(): # The client wants a list of user_ids in the conversation, excluding ourself, # that is sorted in numerical order. - user_ids = [int(s) for s in huddle_message_details["user_ids_string"].split(",")] - user_ids = [user_id for user_id in user_ids if user_id != my_user_id] - user_ids.sort() + user_ids = sorted( + user_id + for s in huddle_message_details["user_ids_string"].split(",") + if (user_id := int(s)) != my_user_id + ) message_details = MessageDetailsDict( type="private", user_ids=user_ids, diff --git a/zerver/lib/onboarding.py b/zerver/lib/onboarding.py index a438c98bf2..0efda0b0a6 100644 --- a/zerver/lib/onboarding.py +++ b/zerver/lib/onboarding.py @@ -143,7 +143,7 @@ def bot_commands(no_help_command: bool = False) -> str: ] if not no_help_command: commands.append("help") - return ", ".join(["`" + command + "`" for command in commands]) + "." + return ", ".join("`" + command + "`" for command in commands) + "." def select_welcome_bot_response(human_response_lower: str) -> str: diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index 595c163a82..68853a0d00 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -264,11 +264,9 @@ def send_apple_push_notification( ) for device in devices ] - results = list( - await asyncio.gather( - *(apns_context.apns.send_notification(request) for request in requests), - return_exceptions=True, - ) + results = await asyncio.gather( + *(apns_context.apns.send_notification(request) for request in requests), + return_exceptions=True, ) return zip(devices, results) @@ -724,7 +722,7 @@ def get_mobile_push_content(rendered_content: str) -> str: def render_olist(ol: lxml.html.HtmlElement) -> str: items = [] counter = int(ol.get("start")) if ol.get("start") else 1 - nested_levels = len(list(ol.iterancestors("ol"))) + nested_levels = sum(1 for ancestor in ol.iterancestors("ol")) indent = ("\n" + " " * nested_levels) if nested_levels else "" for li in ol: diff --git a/zerver/lib/request.py b/zerver/lib/request.py index c03acacf18..932597e049 100644 --- a/zerver/lib/request.py +++ b/zerver/lib/request.py @@ -470,9 +470,9 @@ def has_request_variables( # which could lead to inaccurate output. and 200 <= return_value.status_code < 300 ): - ignored_parameters = set( - list(request.POST.keys()) + list(request.GET.keys()) - ).difference(request_notes.processed_parameters) + ignored_parameters = {*request.POST, *request.GET}.difference( + request_notes.processed_parameters + ) # This will be called each time a function decorated with # has_request_variables returns a MutableJsonResponse with a diff --git a/zerver/lib/stream_subscription.py b/zerver/lib/stream_subscription.py index 6f70a6718d..5a64eaf695 100644 --- a/zerver/lib/stream_subscription.py +++ b/zerver/lib/stream_subscription.py @@ -94,7 +94,7 @@ def get_used_colors_for_user_ids(user_ids: List[int]) -> Dict[int, Set[str]]: result: Dict[int, Set[str]] = defaultdict(set) - for row in list(query): + for row in query: assert row["color"] is not None result[row["user_profile_id"]].add(row["color"]) diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index 7908c3b180..136bcee3a8 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -964,8 +964,9 @@ def do_get_streams( stream_ids = {stream.id for stream in streams} recent_traffic = get_streams_traffic(stream_ids, user_profile.realm) - stream_dicts = [stream_to_dict(stream, recent_traffic) for stream in streams] - stream_dicts.sort(key=lambda elt: elt["name"]) + stream_dicts = sorted( + (stream_to_dict(stream, recent_traffic) for stream in streams), key=lambda elt: elt["name"] + ) if include_default: default_stream_ids = get_default_stream_ids_for_realm(user_profile.realm_id) diff --git a/zerver/lib/string_validation.py b/zerver/lib/string_validation.py index 1536b0a80f..09df29a835 100644 --- a/zerver/lib/string_validation.py +++ b/zerver/lib/string_validation.py @@ -10,9 +10,12 @@ from zerver.models import Stream # https://www.unicode.org/faq/private_use.html#nonchar4 unicode_non_chars = { chr(x) - for x in list(range(0xFDD0, 0xFDF0)) # FDD0 through FDEF, inclusive - + list(range(0xFFFE, 0x110000, 0x10000)) # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive - + list(range(0xFFFF, 0x110000, 0x10000)) # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive + for r in [ + range(0xFDD0, 0xFDF0), # FDD0 through FDEF, inclusive + range(0xFFFE, 0x110000, 0x10000), # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive + range(0xFFFF, 0x110000, 0x10000), # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive + ] + for x in r } diff --git a/zerver/lib/typed_endpoint.py b/zerver/lib/typed_endpoint.py index 29734f0b03..c44b491628 100644 --- a/zerver/lib/typed_endpoint.py +++ b/zerver/lib/typed_endpoint.py @@ -543,9 +543,9 @@ def typed_endpoint( # which could lead to inaccurate output. and 200 <= return_value.status_code < 300 ): - ignored_parameters = set( - list(request.POST.keys()) + list(request.GET.keys()) - ).difference(request_notes.processed_parameters) + ignored_parameters = {*request.POST, *request.GET}.difference( + request_notes.processed_parameters + ) # This will be called each time a function decorated with @typed_endpoint # returns a MutableJsonResponse with a success status_code. Because diff --git a/zerver/lib/user_counts.py b/zerver/lib/user_counts.py index a71781db16..9add4d77b8 100644 --- a/zerver/lib/user_counts.py +++ b/zerver/lib/user_counts.py @@ -17,7 +17,7 @@ def realm_user_count_by_role(realm: Realm) -> Dict[str, Any]: str(UserProfile.ROLE_MEMBER): 0, str(UserProfile.ROLE_GUEST): 0, } - for value_dict in list( + for value_dict in ( UserProfile.objects.filter(realm=realm, is_bot=False, is_active=True) .values("role") .annotate(Count("role")) diff --git a/zerver/lib/user_topics.py b/zerver/lib/user_topics.py index dcc31712e2..e99f96e5da 100644 --- a/zerver/lib/user_topics.py +++ b/zerver/lib/user_topics.py @@ -229,7 +229,7 @@ def exclude_topic_mutes( topic_cond = topic_match_sa(topic_name) return and_(stream_cond, topic_cond) - condition = not_(or_(*list(map(mute_cond, rows)))) + condition = not_(or_(*map(mute_cond, rows))) return [*conditions, condition] diff --git a/zerver/lib/users.py b/zerver/lib/users.py index 8b8be7670f..d62a25fe5e 100644 --- a/zerver/lib/users.py +++ b/zerver/lib/users.py @@ -182,10 +182,8 @@ def bulk_get_cross_realm_bots() -> Dict[str, UserProfile]: where_clause = ( "upper(zerver_userprofile.email::text) IN (SELECT upper(email) FROM unnest(%s) AS email)" ) - users = list( - UserProfile.objects.filter(realm__string_id=settings.SYSTEM_BOT_REALM).extra( - where=[where_clause], params=(emails,) - ) + users = UserProfile.objects.filter(realm__string_id=settings.SYSTEM_BOT_REALM).extra( + where=[where_clause], params=(emails,) ) return {user.email.lower(): user for user in users} diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index a1ece4dce6..d46bfc2d1b 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -316,7 +316,7 @@ def check_dict( delta_keys = set(val.keys()) - required_keys_set - optional_keys_set if len(delta_keys) != 0: raise ValidationError( - _("Unexpected arguments: {keys}").format(keys=", ".join(list(delta_keys))) + _("Unexpected arguments: {keys}").format(keys=", ".join(delta_keys)) ) return cast(Dict[str, ResultT], val) diff --git a/zerver/management/commands/export_search.py b/zerver/management/commands/export_search.py index 43b22679bf..0e79216762 100644 --- a/zerver/management/commands/export_search.py +++ b/zerver/management/commands/export_search.py @@ -187,7 +187,7 @@ This is most often used for legal compliance. .values_list("full_name", "delivery_email") ) - return ", ".join([format_sender(e[0], e[1]) for e in users]), False + return ", ".join(format_sender(e[0], e[1]) for e in users), False def transform_message(message: Message) -> Dict[str, str]: row = { diff --git a/zerver/migrations/0284_convert_realm_admins_to_realm_owners.py b/zerver/migrations/0284_convert_realm_admins_to_realm_owners.py index 211fdf8c1a..3e75ca94d0 100644 --- a/zerver/migrations/0284_convert_realm_admins_to_realm_owners.py +++ b/zerver/migrations/0284_convert_realm_admins_to_realm_owners.py @@ -34,7 +34,7 @@ def set_realm_admins_as_realm_owners( str(UserProfile.ROLE_MEMBER): 0, str(UserProfile.ROLE_GUEST): 0, } - for value_dict in list( + for value_dict in ( UserProfile.objects.filter(realm=realm, is_bot=False, is_active=True) .values("role") .annotate(Count("role")) diff --git a/zerver/migrations/0371_invalid_characters_in_topics.py b/zerver/migrations/0371_invalid_characters_in_topics.py index 80a31650e9..90dbb5c812 100644 --- a/zerver/migrations/0371_invalid_characters_in_topics.py +++ b/zerver/migrations/0371_invalid_characters_in_topics.py @@ -8,9 +8,12 @@ from django.db.migrations.state import StateApps # https://www.unicode.org/faq/private_use.html#nonchar4 unicode_non_chars = { chr(x) - for x in list(range(0xFDD0, 0xFDF0)) # FDD0 through FDEF, inclusive - + list(range(0xFFFE, 0x110000, 0x10000)) # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive - + list(range(0xFFFF, 0x110000, 0x10000)) # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive + for r in [ + range(0xFDD0, 0xFDF0), # FDD0 through FDEF, inclusive + range(0xFFFE, 0x110000, 0x10000), # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive + range(0xFFFF, 0x110000, 0x10000), # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive + ] + for x in r } diff --git a/zerver/migrations/0375_invalid_characters_in_stream_names.py b/zerver/migrations/0375_invalid_characters_in_stream_names.py index 1b4c6541be..ac497755a1 100644 --- a/zerver/migrations/0375_invalid_characters_in_stream_names.py +++ b/zerver/migrations/0375_invalid_characters_in_stream_names.py @@ -8,9 +8,12 @@ from django.db.migrations.state import StateApps # https://www.unicode.org/faq/private_use.html#nonchar4 unicode_non_chars = { chr(x) - for x in list(range(0xFDD0, 0xFDF0)) # FDD0 through FDEF, inclusive - + list(range(0xFFFE, 0x110000, 0x10000)) # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive - + list(range(0xFFFF, 0x110000, 0x10000)) # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive + for r in [ + range(0xFDD0, 0xFDF0), # FDD0 through FDEF, inclusive + range(0xFFFE, 0x110000, 0x10000), # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive + range(0xFFFF, 0x110000, 0x10000), # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive + ] + for x in r } diff --git a/zerver/migrations/0383_revoke_invitations_from_deactivated_users.py b/zerver/migrations/0383_revoke_invitations_from_deactivated_users.py index 671cb67fed..f2d9def5cc 100644 --- a/zerver/migrations/0383_revoke_invitations_from_deactivated_users.py +++ b/zerver/migrations/0383_revoke_invitations_from_deactivated_users.py @@ -36,13 +36,11 @@ def revoke_invitations(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) multiuse_invite_ids = MultiuseInvite.objects.filter( referred_by_id__in=user_ids ).values_list("id", flat=True) - confirmation_ids += list( - Confirmation.objects.filter( - type=Confirmation.MULTIUSE_INVITE, - expiry_date__gte=timezone_now(), - object_id__in=multiuse_invite_ids, - ).values_list("id", flat=True) - ) + confirmation_ids += Confirmation.objects.filter( + type=Confirmation.MULTIUSE_INVITE, + expiry_date__gte=timezone_now(), + object_id__in=multiuse_invite_ids, + ).values_list("id", flat=True) return confirmation_ids diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index c7011e2ce9..c218b64162 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -5275,7 +5275,7 @@ class TestDevAuthBackend(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertEqual(result["Location"], "http://zulip.testserver/") self.assert_logged_in_user_id(user_profile.id) - self.assertIn("otp_device_id", list(self.client.session.keys())) + self.assertIn("otp_device_id", self.client.session.keys()) def test_redirect_to_next_url(self) -> None: def do_local_login(formaction: str) -> "TestHttpResponse": @@ -5937,7 +5937,7 @@ class TestLDAP(ZulipLDAPTestCase): for key, value in ldap_dir.items(): self.assertTrue(regex.match(key)) self.assertCountEqual( - list(value.keys()), [*common_attrs, "uid", "thumbnailPhoto", "userAccountControl"] + value.keys(), [*common_attrs, "uid", "thumbnailPhoto", "userAccountControl"] ) ldap_dir = generate_dev_ldap_dir("b", 9) @@ -5945,14 +5945,14 @@ class TestLDAP(ZulipLDAPTestCase): regex = re.compile(r"(uid\=)+[a-zA-Z0-9_.+-]+(\,ou\=users\,dc\=zulip\,dc\=com)") for key, value in ldap_dir.items(): self.assertTrue(regex.match(key)) - self.assertCountEqual(list(value.keys()), [*common_attrs, "uid", "jpegPhoto"]) + self.assertCountEqual(value.keys(), [*common_attrs, "uid", "jpegPhoto"]) ldap_dir = generate_dev_ldap_dir("c", 8) self.assert_length(ldap_dir, 8) regex = re.compile(r"(uid\=)+[a-zA-Z0-9_.+-]+(\,ou\=users\,dc\=zulip\,dc\=com)") for key, value in ldap_dir.items(): self.assertTrue(regex.match(key)) - self.assertCountEqual(list(value.keys()), [*common_attrs, "uid", "email"]) + self.assertCountEqual(value.keys(), [*common_attrs, "uid", "email"]) @override_settings(AUTHENTICATION_BACKENDS=("zproject.backends.ZulipLDAPAuthBackend",)) def test_dev_ldap_fail_login(self) -> None: diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py index 78aac7dcdb..72048e12ea 100644 --- a/zerver/tests/test_message_fetch.py +++ b/zerver/tests/test_message_fetch.py @@ -2498,7 +2498,7 @@ class GetOldMessagesTest(ZulipTestCase): params = {k: orjson.dumps(v).decode() for k, v in raw_params.items()} result = self.client_get("/json/messages/matches_narrow", params) messages = self.assert_json_success(result)["messages"] - self.assert_length(list(messages.keys()), 1) + self.assert_length(messages, 1) message = messages[str(good_id)] self.assertEqual( message["match_content"], @@ -2514,7 +2514,7 @@ class GetOldMessagesTest(ZulipTestCase): params = {k: orjson.dumps(v).decode() for k, v in raw_params.items()} result = self.client_get("/json/messages/matches_narrow", params) messages = self.assert_json_success(result)["messages"] - self.assert_length(list(messages.keys()), 1) + self.assert_length(messages, 1) message = messages[str(good_id)] self.assertEqual( message["match_content"], @@ -2973,7 +2973,7 @@ class GetOldMessagesTest(ZulipTestCase): params = {k: orjson.dumps(v).decode() for k, v in raw_params.items()} result = self.client_get("/json/messages/matches_narrow", params) messages = self.assert_json_success(result)["messages"] - self.assert_length(list(messages.keys()), 1) + self.assert_length(messages, 1) message = messages[str(good_id)] self.assertIn("a href=", message["match_content"]) self.assertIn("http://foo.com", message["match_content"]) diff --git a/zerver/tests/test_user_groups.py b/zerver/tests/test_user_groups.py index a78d64cd78..4c45049ad1 100644 --- a/zerver/tests/test_user_groups.py +++ b/zerver/tests/test_user_groups.py @@ -135,14 +135,14 @@ class UserGroupTestCase(ZulipTestCase): list(get_recursive_group_members(everyone_group)), [desdemona, iago, shiva] ) - self.assertIn(leadership_group, list(get_recursive_membership_groups(desdemona))) - self.assertIn(staff_group, list(get_recursive_membership_groups(desdemona))) - self.assertIn(everyone_group, list(get_recursive_membership_groups(desdemona))) + self.assertIn(leadership_group, get_recursive_membership_groups(desdemona)) + self.assertIn(staff_group, get_recursive_membership_groups(desdemona)) + self.assertIn(everyone_group, get_recursive_membership_groups(desdemona)) - self.assertIn(staff_group, list(get_recursive_membership_groups(iago))) - self.assertIn(everyone_group, list(get_recursive_membership_groups(iago))) + self.assertIn(staff_group, get_recursive_membership_groups(iago)) + self.assertIn(everyone_group, get_recursive_membership_groups(iago)) - self.assertIn(everyone_group, list(get_recursive_membership_groups(shiva))) + self.assertIn(everyone_group, get_recursive_membership_groups(shiva)) def test_subgroups_of_role_based_system_groups(self) -> None: realm = get_realm("zulip") diff --git a/zerver/views/development/dev_login.py b/zerver/views/development/dev_login.py index 38cfa318d8..acf0e14d65 100644 --- a/zerver/views/development/dev_login.py +++ b/zerver/views/development/dev_login.py @@ -42,7 +42,7 @@ def get_dev_users(realm: Optional[Realm] = None, extra_users_count: int = 10) -> extra_users = users_query.filter(email__startswith="extrauser").order_by("email") # Limit the number of extra users we offer by default extra_users = extra_users[0:extra_users_count] - users = list(shakespearian_users) + list(extra_users) + users = [*shakespearian_users, *extra_users] return users diff --git a/zerver/views/documentation.py b/zerver/views/documentation.py index 18e484b423..95f7628f52 100644 --- a/zerver/views/documentation.py +++ b/zerver/views/documentation.py @@ -281,7 +281,7 @@ class MarkdownDirectoryView(ApiURLView): def add_integrations_context(context: Dict[str, Any]) -> None: alphabetical_sorted_categories = OrderedDict(sorted(CATEGORIES.items())) alphabetical_sorted_integration = OrderedDict(sorted(INTEGRATIONS.items())) - enabled_integrations_count = len(list(filter(lambda v: v.is_enabled(), INTEGRATIONS.values()))) + enabled_integrations_count = sum(v.is_enabled() for v in INTEGRATIONS.values()) # Subtract 1 so saying "Over X integrations" is correct. Then, # round down to the nearest multiple of 10. integrations_count_display = ((enabled_integrations_count - 1) // 10) * 10 diff --git a/zerver/views/realm.py b/zerver/views/realm.py index 33b8dd7bdb..c87747f381 100644 --- a/zerver/views/realm.py +++ b/zerver/views/realm.py @@ -166,7 +166,7 @@ def update_realm( if authentication_methods is not None: if not user_profile.is_realm_owner: raise OrganizationOwnerRequiredError - if True not in list(authentication_methods.values()): + if True not in authentication_methods.values(): raise JsonableError(_("At least one authentication method must be enabled.")) if video_chat_provider is not None and video_chat_provider not in { p["id"] for p in Realm.VIDEO_CHAT_PROVIDERS.values() @@ -285,7 +285,7 @@ def update_realm( req_vars = {} req_group_setting_vars = {} - for k, v in list(locals().items()): + for k, v in locals().items(): if k in realm.property_types: req_vars[k] = v @@ -293,7 +293,7 @@ def update_realm( if k == permissions_configuration.id_field_name: req_group_setting_vars[k] = v - for k, v in list(req_vars.items()): + for k, v in req_vars.items(): if v is not None and getattr(realm, k) != v: do_set_realm_property(realm, k, v, acting_user=user_profile) if isinstance(v, str): @@ -535,10 +535,8 @@ def update_realm_user_settings_defaults( check_settings_values(notification_sound, email_notifications_batching_period_seconds) realm_user_default = RealmUserDefault.objects.get(realm=user_profile.realm) - request_settings = { - k: v for k, v in list(locals().items()) if (k in RealmUserDefault.property_types) - } - for k, v in list(request_settings.items()): + request_settings = {k: v for k, v in locals().items() if (k in RealmUserDefault.property_types)} + for k, v in request_settings.items(): if v is not None and getattr(realm_user_default, k) != v: do_set_realm_user_default_setting(realm_user_default, k, v, acting_user=user_profile) diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index daf5db53aa..248002bd0b 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -361,8 +361,8 @@ def json_change_settings( check_change_full_name(user_profile, full_name, user_profile) # Loop over user_profile.property_types - request_settings = {k: v for k, v in list(locals().items()) if k in user_profile.property_types} - for k, v in list(request_settings.items()): + request_settings = {k: v for k, v in locals().items() if k in user_profile.property_types} + for k, v in request_settings.items(): if v is not None and getattr(user_profile, k) != v: do_change_user_setting(user_profile, k, v, acting_user=user_profile) diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py index 7ced36228a..53f30e5a44 100644 --- a/zerver/webhooks/github/view.py +++ b/zerver/webhooks/github/view.py @@ -892,7 +892,7 @@ def get_zulip_event_name( return "issue_milestoned_or_demilestoned" else: return "issues" - elif header_event in list(EVENT_FUNCTION_MAPPER.keys()): + elif header_event in EVENT_FUNCTION_MAPPER: return header_event elif header_event in IGNORED_EVENTS: return None diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.py index 750fe99a0d..184dcdd2ec 100644 --- a/zerver/webhooks/gitlab/view.py +++ b/zerver/webhooks/gitlab/view.py @@ -526,7 +526,7 @@ def get_event(request: HttpRequest, payload: WildValue, branches: Optional[str]) if branches.find(branch) == -1: return None - if event in list(EVENT_FUNCTION_MAPPER.keys()): + if event in EVENT_FUNCTION_MAPPER: return event raise UnsupportedWebhookEventTypeError(event) diff --git a/zerver/webhooks/jotform/view.py b/zerver/webhooks/jotform/view.py index b0781b33cc..2e86949978 100644 --- a/zerver/webhooks/jotform/view.py +++ b/zerver/webhooks/jotform/view.py @@ -20,10 +20,10 @@ def api_jotform_webhook( ) -> HttpResponse: topic = payload["formTitle"].tame(check_string) submission_id = payload["submissionID"].tame(check_string) - fields_dict = list(payload["pretty"].tame(check_string).split(", ")) + fields = payload["pretty"].tame(check_string).split(", ") form_response = f"A new submission (ID {submission_id}) was received:\n" - for field in fields_dict: + for field in fields: form_response += f"* {field}\n" message = form_response.strip() diff --git a/zerver/webhooks/slack/view.py b/zerver/webhooks/slack/view.py index 6128af02b0..da39daed62 100644 --- a/zerver/webhooks/slack/view.py +++ b/zerver/webhooks/slack/view.py @@ -26,7 +26,7 @@ def api_slack_webhook( stream: str = "slack", channels_map_to_topics: str = "1", ) -> HttpResponse: - if channels_map_to_topics not in list(VALID_OPTIONS.values()): + if channels_map_to_topics not in VALID_OPTIONS.values(): raise JsonableError(_("Error: channels_map_to_topics parameter other than 0 or 1")) if channels_map_to_topics == VALID_OPTIONS["SHOULD_BE_MAPPED"]: