Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

python: Simplify away various unnecessary lists and list comprehensions.

Anders Kaseorga610bd19a1ea
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index ce032d66174..24e99e8292c 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -1937,9 +1937,7 @@ def get_recipient_from_user_profiles(recipient_profiles: Sequence[UserProfile],
sender: UserProfile) -> Recipient:
# Avoid mutating the passed in list of recipient_profiles.
- recipient_profiles_map = {}
- for user_profile in recipient_profiles:
- recipient_profiles_map[user_profile.id] = user_profile
+ recipient_profiles_map = {user_profile.id: user_profile for user_profile in recipient_profiles}
if forwarded_mirror_message:
# In our mirroring integrations with some third-party
@@ -1959,15 +1957,15 @@ def get_recipient_from_user_profiles(recipient_profiles: Sequence[UserProfile],
if (len(recipient_profiles_map) == 2 and sender.id in recipient_profiles_map):
del recipient_profiles_map[sender.id]
- assert len(recipient_profiles_map) != 0
+ assert recipient_profiles_map
if len(recipient_profiles_map) == 1:
- user_profile = list(recipient_profiles_map.values())[0]
+ [user_profile] = recipient_profiles_map.values()
return user_profile.recipient
# Otherwise, we need a huddle. Make sure the sender is included in huddle messages
recipient_profiles_map[sender.id] = sender
- user_ids: Set[int] = {user_id for user_id in recipient_profiles_map}
+ user_ids = set(recipient_profiles_map)
return get_huddle_recipient(user_ids)
def validate_recipient_user_profiles(user_profiles: Sequence[UserProfile],
@@ -2808,11 +2806,8 @@ def bulk_add_subscriptions(streams: Iterable[Stream],
users = list(users)
recipients_map: Dict[int, int] = {stream.id: stream.recipient_id for stream in streams}
- recipient_ids: List[int] = [recipient_id for recipient_id in recipients_map.values()]
-
- stream_map: Dict[int, Stream] = {}
- for stream in streams:
- stream_map[recipients_map[stream.id]] = stream
+ recipient_ids = list(recipients_map.values())
+ stream_map = {recipients_map[stream.id]: stream for stream in streams}
subs_by_user: Dict[int, List[Subscription]] = defaultdict(list)
all_subs_query = get_stream_subscriptions_for_users(users).select_related('user_profile')
@@ -3107,8 +3102,7 @@ def bulk_remove_subscriptions(users: Iterable[UserProfile],
for stream in streams:
send_peer_remove_event(stream=stream)
- new_vacant_streams = [stream for stream in
- set(occupied_streams_before) - set(occupied_streams_after)]
+ new_vacant_streams = set(occupied_streams_before) - set(occupied_streams_after)
new_vacant_private_streams = [stream for stream in new_vacant_streams
if stream.invite_only]
new_vacant_public_streams = [stream for stream in new_vacant_streams
diff --git a/zerver/lib/export.py b/zerver/lib/export.py
index d2e732a3e57..7cbc70fe269 100644
--- a/zerver/lib/export.py
+++ b/zerver/lib/export.py
@@ -1692,7 +1692,7 @@ def export_messages_single_user(user_profile: UserProfile, output_dir: Path,
while True:
actual_query = user_message_query.select_related(
"message", "message__sending_client").filter(id__gt=min_id)[0:chunk_size]
- user_message_chunk = [um for um in actual_query]
+ user_message_chunk = list(actual_query)
user_message_ids = {um.id for um in user_message_chunk}
if len(user_message_chunk) == 0:
diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.py
index a4db0146f5a..4afea31b43a 100644
--- a/zerver/tests/test_bots.py
+++ b/zerver/tests/test_bots.py
@@ -200,9 +200,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
users_result = self.client_get('/json/users')
members = orjson.loads(users_result.content)['members']
- bots = [m for m in members if m['email'] == '[email protected]']
- self.assertEqual(len(bots), 1)
- bot = bots[0]
+ [bot] = [m for m in members if m['email'] == '[email protected]']
self.assertEqual(bot['bot_owner_id'], self.example_user('hamlet').id)
self.assertEqual(bot['user_id'], self.get_bot_user(email).id)
@@ -332,9 +330,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
users_result = self.client_get('/json/users')
members = orjson.loads(users_result.content)['members']
- bots = [m for m in members if m['email'] == '[email protected]']
- self.assertEqual(len(bots), 1)
- bot = bots[0]
+ [bot] = [m for m in members if m['email'] == '[email protected]']
self.assertEqual(bot['bot_owner_id'], user.id)
self.assertEqual(bot['user_id'], self.get_bot_user(email).id)
@@ -574,9 +570,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_post("/json/bots", bot_info)
self.assert_json_success(result)
- all_bots = UserProfile.objects.filter(is_bot=True, bot_owner=user, is_active=True)
- bots = [bot for bot in all_bots]
- self.assertEqual(len(bots), 2)
+ self.assertEqual(UserProfile.objects.filter(is_bot=True, bot_owner=user, is_active=True).count(), 2)
result = self.client_delete('/json/users/me')
self.assert_json_success(result)
@@ -584,9 +578,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assertFalse(user.is_active)
self.login('iago')
- all_bots = UserProfile.objects.filter(is_bot=True, bot_owner=user, is_active=True)
- bots = [bot for bot in all_bots]
- self.assertEqual(len(bots), 0)
+ self.assertFalse(UserProfile.objects.filter(is_bot=True, bot_owner=user, is_active=True).exists())
def test_cannot_deactivate_other_realm_bot(self) -> None:
user = self.mit_user('starnine')
@@ -1439,10 +1431,8 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot_email = "[email protected]"
bot_realm = get_realm('zulip')
bot = get_user(bot_email, bot_realm)
- services = get_bot_services(bot.id)
- service = services[0]
+ [service] = get_bot_services(bot.id)
- self.assertEqual(len(services), 1)
self.assertEqual(service.name, "outgoingservicebot")
self.assertEqual(service.base_url, "http://127.0.0.1:5002")
self.assertEqual(service.user_profile, bot)
@@ -1506,11 +1496,9 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot_email = "[email protected]"
bot_realm = get_realm('zulip')
bot = get_user(bot_email, bot_realm)
- services = get_bot_services(bot.id)
- service = services[0]
+ [service] = get_bot_services(bot.id)
bot_config = get_bot_config(bot)
self.assertEqual(bot_config, bot_config_info)
- self.assertEqual(len(services), 1)
self.assertEqual(service.name, "followup")
self.assertEqual(service.user_profile, bot)
diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py
index 53a36f232dc..1ed3c409837 100644
--- a/zerver/tests/test_slack_importer.py
+++ b/zerver/tests/test_slack_importer.py
@@ -463,7 +463,7 @@ class SlackImporter(ZulipTestCase):
# We can't do an assertDictEqual since during the construction of Personal
# recipients, slack_user_id_to_zulip_user_id are iterated in different order in Python 3.5 and 3.6.
self.assertEqual(set(slack_recipient_name_to_zulip_recipient_id.keys()), slack_recipient_names)
- self.assertEqual(set(slack_recipient_name_to_zulip_recipient_id.values()), {i for i in range(11)})
+ self.assertEqual(set(slack_recipient_name_to_zulip_recipient_id.values()), set(range(11)))
# functioning of zerver subscriptions are already tested in the helper functions
# This is to check the concatenation of the output lists from the helper functions
@@ -472,7 +472,7 @@ class SlackImporter(ZulipTestCase):
zerver_recipient = realm["zerver_recipient"]
zerver_stream = realm["zerver_stream"]
- self.assertEqual(self.get_set(zerver_subscription, "recipient"), {i for i in range(11)})
+ self.assertEqual(self.get_set(zerver_subscription, "recipient"), set(range(11)))
self.assertEqual(self.get_set(zerver_subscription, "user_profile"), {1, 5, 7, 8})
self.assertEqual(self.get_set(zerver_recipient, "id"), self.get_set(zerver_subscription, "recipient"))