python: Simplify away various unnecessary lists and list comprehensions.

Loosely inspired by the flake8-comprehensions plugin.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-09-01 23:13:11 -07:00
committed by Tim Abbott
parent 9048f79e53
commit a610bd19a1
4 changed files with 16 additions and 34 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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'] == 'hambot-bot@zulip.testserver']
self.assertEqual(len(bots), 1)
bot = bots[0]
[bot] = [m for m in members if m['email'] == 'hambot-bot@zulip.testserver']
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'] == 'hambot-bot@zulip.testserver']
self.assertEqual(len(bots), 1)
bot = bots[0]
[bot] = [m for m in members if m['email'] == 'hambot-bot@zulip.testserver']
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 = "outgoingservicebot-bot@zulip.testserver"
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 = "embeddedservicebot-bot@zulip.testserver"
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)

View File

@@ -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"))