Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
python: Remove unnecessary intermediate lists.
Anders Kaseorg3665deb93a12
diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.pyindex d3a6e052ac1..8602a6c76a9 100644--- a/corporate/views/upgrade.py+++ b/corporate/views/upgrade.py@@ -187,11 +187,11 @@ def initial_upgrade(request: HttpRequest) -> HttpResponse:},"realm_org_type": user.realm.org_type,"sorted_org_types": sorted(- [+ ([org_type_name, org_type]for (org_type_name, org_type) in Realm.ORG_TYPES.items()if not org_type.get("hidden")- ],+ ),key=lambda d: d[1]["display_order"],),}diff --git a/scripts/lib/clean_yarn_cache.py b/scripts/lib/clean_yarn_cache.pyindex a467618d32b..d05a342ad74 100644--- a/scripts/lib/clean_yarn_cache.py+++ b/scripts/lib/clean_yarn_cache.py@@ -21,13 +21,11 @@ def remove_unused_versions_dir(args: argparse.Namespace) -> None:"""current_version_dir = os.path.join(YARN_CACHE_PATH, CURRENT_VERSION)try:- dirs_to_purge = set(- [- os.path.join(YARN_CACHE_PATH, directory)- for directory in os.listdir(YARN_CACHE_PATH)- if directory != CURRENT_VERSION- ]- )+ dirs_to_purge = {+ os.path.join(YARN_CACHE_PATH, directory)+ for directory in os.listdir(YARN_CACHE_PATH)+ if directory != CURRENT_VERSION+ }except FileNotFoundError:returndiff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.pyindex bf161b56897..c6e379b6141 100755--- a/zerver/data_import/slack.py+++ b/zerver/data_import/slack.py@@ -1363,7 +1363,7 @@ def check_token_access(token: str) -> None:if data.status_code != 200 or not data.json()["ok"]:raise ValueError("Invalid Slack token: {}".format(token))has_scopes = set(data.headers.get("x-oauth-scopes", "").split(","))- required_scopes = set(["emoji:read", "users:read", "users:read.email", "team:read"])+ required_scopes = {"emoji:read", "users:read", "users:read.email", "team:read"}missing_scopes = required_scopes - has_scopesif missing_scopes:raise ValueError(diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 8d5df38e6e5..6910907cf7f 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -3640,7 +3640,7 @@ def bulk_get_subscriber_user_ids(target_stream_dicts.append(stream_dict)recip_to_stream_id = {stream["recipient_id"]: stream["id"] for stream in target_stream_dicts}- recipient_ids = sorted([stream["recipient_id"] for stream in target_stream_dicts])+ recipient_ids = sorted(stream["recipient_id"] for stream in target_stream_dicts)result: Dict[int, List[int]] = {stream["id"]: [] for stream in stream_dicts}if not recipient_ids:diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex a6e67e14ac0..f55714c89ac 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -307,7 +307,7 @@ def image_preview_enabled(def list_of_tlds() -> List[str]:# Skip a few overly-common false-positives from file extensions- common_false_positives = set(["java", "md", "mov", "py", "zip"])+ common_false_positives = {"java", "md", "mov", "py", "zip"}tlds = list(tld_set - common_false_positives)tlds.sort(key=len, reverse=True)diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.pyindex 5f7daa11d90..965dac044e6 100644--- a/zerver/openapi/openapi.py+++ b/zerver/openapi/openapi.py@@ -28,7 +28,7 @@ EXCLUDE_UNDOCUMENTED_ENDPOINTS = {}# Consists of endpoints with some documentation remaining.# These are skipped but return true as the validator cannot exclude objects-EXCLUDE_DOCUMENTED_ENDPOINTS: Set[Tuple[str, str]] = set([])+EXCLUDE_DOCUMENTED_ENDPOINTS: Set[Tuple[str, str]] = set()# Most of our code expects allOf to be preprocessed away because that is what# yamole did. Its algorithm for doing so is not standards compliant, but wediff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex a7d0bb8b83d..7f5138dc2ee 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -173,7 +173,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):email = "[email protected]"bot = self.get_bot_user(email)- (event,) = [e for e in events if e["event"]["type"] == "realm_bot"]+ (event,) = (e for e in events if e["event"]["type"] == "realm_bot")self.assertEqual(result["user_id"], bot.id)self.assertEqual(@@ -339,7 +339,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):email = "[email protected]"bot = self.get_bot_user(email)- (event,) = [e for e in events if e["event"]["type"] == "realm_bot"]+ (event,) = (e for e in events if e["event"]["type"] == "realm_bot")self.assertEqual(dict(type="realm_bot",@@ -435,7 +435,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):assert profile.default_sending_stream is not Noneself.assertEqual(profile.default_sending_stream.name, "Denmark")- (event,) = [e for e in events if e["event"]["type"] == "realm_bot"]+ (event,) = (e for e in events if e["event"]["type"] == "realm_bot")self.assertEqual(dict(type="realm_bot",@@ -507,7 +507,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):assert bot_profile.default_events_register_stream is not Noneself.assertEqual(bot_profile.default_events_register_stream.name, "Denmark")- (event,) = [e for e in events if e["event"]["type"] == "realm_bot"]+ (event,) = (e for e in events if e["event"]["type"] == "realm_bot")self.assertEqual(dict(type="realm_bot",diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.pyindex 5cb263d262e..d60fa90e1f8 100644--- a/zerver/tests/test_digest.py+++ b/zerver/tests/test_digest.py@@ -526,12 +526,12 @@ class TestDigestTopics(ZulipTestCase):lengthy_topic_d,]self.assertEqual(- get_hot_topics(topics, set([1, 0])),+ get_hot_topics(topics, {1, 0}),[diverse_topic_a, diverse_topic_b, lengthy_topic_a, lengthy_topic_b],)self.assertEqual(- get_hot_topics(topics, set([1, 2])),+ get_hot_topics(topics, {1, 2}),[diverse_topic_a, diverse_topic_c, lengthy_topic_a, lengthy_topic_d],)- self.assertEqual(get_hot_topics(topics, set([2])), [diverse_topic_c, lengthy_topic_d])+ self.assertEqual(get_hot_topics(topics, {2}), [diverse_topic_c, lengthy_topic_d])self.assertEqual(get_hot_topics(topics, set()), [])diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 676f0180c91..adc5b2fc81b 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -2147,7 +2147,7 @@ class GetOldMessagesTest(ZulipTestCase):'<p><a href="https://google.com">https://<span class="highlight">google.com</span></a></p>',)- (meeting_message,) = [m for m in messages if m[TOPIC_NAME] == "meetings"]+ (meeting_message,) = (m for m in messages if m[TOPIC_NAME] == "meetings")self.assertEqual(meeting_message[MATCH_TOPIC], "meetings")self.assertEqual(meeting_message["match_content"],@@ -2155,7 +2155,7 @@ class GetOldMessagesTest(ZulipTestCase):+ '<span class="highlight">lunch</span></p>',)- (lunch_message,) = [m for m in messages if m[TOPIC_NAME] == "lunch plans"]+ (lunch_message,) = (m for m in messages if m[TOPIC_NAME] == "lunch plans")self.assertEqual(lunch_message[MATCH_TOPIC], '<span class="highlight">lunch</span> plans')self.assertEqual(lunch_message["match_content"], "<p>I am hungry!</p>")@@ -2200,7 +2200,7 @@ class GetOldMessagesTest(ZulipTestCase):'<p>昨日、<span class="highlight">日本</span>' + " のお菓子を送りました。</p>",)- (english_message,) = [m for m in messages if m[TOPIC_NAME] == "english"]+ (english_message,) = (m for m in messages if m[TOPIC_NAME] == "english")self.assertEqual(english_message[MATCH_TOPIC], "english")self.assertIn(english_message["match_content"],@@ -3649,7 +3649,7 @@ WHERE user_profile_id = {hamlet_id} AND (content ILIKE '%jumping%' OR subject ILself.assert_length(result["messages"], 1)messages = result["messages"]- (hello_message,) = [m for m in messages if m[TOPIC_NAME] == "say hello"]+ (hello_message,) = (m for m in messages if m[TOPIC_NAME] == "say hello")self.assertEqual(hello_message[MATCH_TOPIC], "say hello")self.assertEqual(hello_message["match_content"],diff --git a/zerver/tests/test_notification_data.py b/zerver/tests/test_notification_data.pyindex 9605eab58ae..876ed4a8aea 100644--- a/zerver/tests/test_notification_data.py+++ b/zerver/tests/test_notification_data.py@@ -271,7 +271,7 @@ class TestNotificationData(ZulipTestCase):# Personal and user group mentioned. Test that we don't consider the user# group mention for Hamlet in this case.result = get_user_group_mentions_data(- mentioned_user_ids=set([hamlet.id]),+ mentioned_user_ids={hamlet.id},mentioned_user_group_ids=[hamlet_and_cordelia.id],mention_data=MentionData(realm.id, "hey @*hamlet_and_cordelia*!"),)diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.pyindex adf7afdaf15..12c95130e3e 100644--- a/zerver/tests/test_openapi.py+++ b/zerver/tests/test_openapi.py@@ -285,7 +285,7 @@ class OpenAPIArgumentsTest(ZulipTestCase):# Endpoints where the documentation is currently failing our# consistency tests. We aim to keep this list empty.- buggy_documentation_endpoints: Set[str] = set([])+ buggy_documentation_endpoints: Set[str] = set()def convert_regex_to_url_pattern(self, regex_pattern: str) -> str:"""Convert regular expressions style URL patterns to theirdiff --git a/zerver/tests/test_queue_worker.py b/zerver/tests/test_queue_worker.pyindex 692f9c579d1..072d66aa8a0 100644--- a/zerver/tests/test_queue_worker.py+++ b/zerver/tests/test_queue_worker.py@@ -68,7 +68,7 @@ class WorkerTest(ZulipTestCase):chunk = []def local_queue_size(self) -> int:- return sum([len(q) for q in self.queues.values()])+ return sum(len(q) for q in self.queues.values())def test_UserActivityWorker(self) -> None:fake_client = self.FakeClient()diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 89ac65ef69a..03c7595924d 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -2965,7 +2965,7 @@ class SubscriptionAPITest(ZulipTestCase):expected_stream_ids = {get_stream(stream, self.test_realm).id for stream in add_streams}- (peer_add_event,) = [event for event in events if event["event"].get("op") == "peer_add"]+ (peer_add_event,) = (event for event in events if event["event"].get("op") == "peer_add")self.assertEqual(set(peer_add_event["event"]["stream_ids"]), expected_stream_ids)self.assertEqual(set(peer_add_event["event"]["user_ids"]), {self.test_user.id})diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 39d926e0dbf..b9e872bc737 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1544,8 +1544,8 @@ class RecipientInfoTest(ZulipTestCase):stream_topic=stream_topic,possible_wildcard_mention=False,)- self.assertEqual(info["pm_mention_email_disabled_user_ids"], set([hamlet.id]))- self.assertEqual(info["pm_mention_push_disabled_user_ids"], set([hamlet.id]))+ self.assertEqual(info["pm_mention_email_disabled_user_ids"], {hamlet.id})+ self.assertEqual(info["pm_mention_push_disabled_user_ids"], {hamlet.id})hamlet.enable_offline_email_notifications = Truehamlet.enable_offline_push_notifications = Truehamlet.save()@@ -1883,7 +1883,7 @@ class GetProfileTest(ZulipTestCase):result = self.api_get(hamlet, "/api/v1/users")self.assert_json_success(result)- (my_user,) = [user for user in result.json()["members"] if user["email"] == hamlet.email]+ (my_user,) = (user for user in result.json()["members"] if user["email"] == hamlet.email)self.assertEqual(my_user["avatar_url"],diff --git a/zerver/tornado/django_api.py b/zerver/tornado/django_api.pyindex b83eaa5b7bc..f2b35aef8a3 100644--- a/zerver/tornado/django_api.py+++ b/zerver/tornado/django_api.py@@ -19,7 +19,7 @@ class TornadoAdapter(HTTPAdapter):def __init__(self) -> None:# All of the POST requests we make to Tornado are safe to# retry; allow retries of them, which is not the default.- retry_methods = Retry.DEFAULT_METHOD_WHITELIST | set(["POST"])+ retry_methods = Retry.DEFAULT_METHOD_WHITELIST | {"POST"}retry = Retry(total=3, backoff_factor=1, method_whitelist=retry_methods)super().__init__(max_retries=retry)