From c4748298bb74180963beb49ebd9f99bff9aba993 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 31 Jul 2023 13:16:30 -0700 Subject: [PATCH] ruff: Fix PERF102 Using only the keys/values of a dict. Signed-off-by: Anders Kaseorg --- tools/setup/emoji/emoji_setup_utils.py | 2 +- zerver/data_import/slack.py | 2 +- zerver/lib/cache.py | 4 +--- zerver/lib/rate_limiter.py | 2 +- zerver/lib/streams.py | 2 +- zerver/lib/test_classes.py | 2 +- zerver/management/commands/compilemessages.py | 2 +- zerver/models.py | 2 +- zerver/tests/test_events.py | 2 +- zerver/tests/test_slack_importer.py | 3 +-- 10 files changed, 10 insertions(+), 13 deletions(-) diff --git a/tools/setup/emoji/emoji_setup_utils.py b/tools/setup/emoji/emoji_setup_utils.py index 628e57c510..cbb06d2ade 100644 --- a/tools/setup/emoji/emoji_setup_utils.py +++ b/tools/setup/emoji/emoji_setup_utils.py @@ -53,7 +53,7 @@ EMOTICON_CONVERSIONS = { def emoji_names_for_picker(emoji_name_maps: Dict[str, Dict[str, Any]]) -> List[str]: emoji_names: List[str] = [] - for emoji_code, name_info in emoji_name_maps.items(): + for name_info in emoji_name_maps.values(): emoji_names.append(name_info["canonical_name"]) emoji_names.extend(name_info["aliases"]) diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py index ba80420558..e14fdec317 100644 --- a/zerver/data_import/slack.py +++ b/zerver/data_import/slack.py @@ -366,7 +366,7 @@ def build_customprofile_field( # The name of the custom profile field is not provided in the Slack data # Hash keys of the fields are provided # Reference: https://api.slack.com/methods/users.profile.set - for field, value in fields.items(): + for field in fields: if field not in slack_custom_field_name_to_zulip_custom_field_id: slack_custom_fields = ["phone", "skype"] if field in slack_custom_fields: diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index e18bbe4df6..a0073a2fc3 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -367,9 +367,7 @@ def generic_bulk_cached_fetch( [cache_keys[object_id] for object_id in object_ids], ) - cached_objects: Dict[str, CacheItemT] = {} - for key, val in cached_objects_compressed.items(): - cached_objects[key] = extractor(cached_objects_compressed[key][0]) + cached_objects = {key: extractor(val[0]) for key, val in cached_objects_compressed.items()} needed_ids = [ object_id for object_id in object_ids if cache_keys[object_id] not in cached_objects ] diff --git a/zerver/lib/rate_limiter.py b/zerver/lib/rate_limiter.py index d3a8920181..338d07ea5a 100644 --- a/zerver/lib/rate_limiter.py +++ b/zerver/lib/rate_limiter.py @@ -282,7 +282,7 @@ class TornadoInMemoryRateLimiterBackend(RateLimiterBackend): @classmethod def clear_history(cls, entity_key: str) -> None: - for rule, reset_times_for_rule in cls.reset_times.items(): + for reset_times_for_rule in cls.reset_times.values(): reset_times_for_rule.pop(entity_key, None) cls.timestamps_blocked_until.pop(entity_key, None) diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index adb81a8345..a94d2fabe3 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -73,7 +73,7 @@ def get_stream_permission_policy_name( is_web_public: Optional[bool] = None, ) -> str: policy_name = None - for permission, permission_dict in Stream.PERMISSION_POLICIES.items(): + for permission_dict in Stream.PERMISSION_POLICIES.values(): if ( permission_dict["invite_only"] == invite_only and permission_dict["history_public_to_subscribers"] == history_public_to_subscribers diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 62a3b88fd8..ec5383e072 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -1558,7 +1558,7 @@ Output: """ directory = orjson.loads(self.fixture_data("directory.json", type="ldap")) - for dn, attrs in directory.items(): + for attrs in directory.values(): if "uid" in attrs: # Generate a password for the LDAP account: attrs["userPassword"] = [self.ldap_password(attrs["uid"][0])] diff --git a/zerver/management/commands/compilemessages.py b/zerver/management/commands/compilemessages.py index 4d92491bec..86f15ffac1 100644 --- a/zerver/management/commands/compilemessages.py +++ b/zerver/management/commands/compilemessages.py @@ -145,7 +145,7 @@ class Command(compilemessages.Command): # frontend stats with open(self.get_json_filename(locale_path, locale), "rb") as reader: - for key, value in orjson.loads(reader.read()).items(): + for value in orjson.loads(reader.read()).values(): total += 1 if value == "": not_translated += 1 diff --git a/zerver/models.py b/zerver/models.py index f033c116b8..efcdc0185a 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1064,7 +1064,7 @@ def avatar_changes_disabled(realm: Realm) -> bool: def get_org_type_display_name(org_type: int) -> str: - for realm_type, realm_type_details in Realm.ORG_TYPES.items(): + for realm_type_details in Realm.ORG_TYPES.values(): if realm_type_details["id"] == org_type: return realm_type_details["name"] diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 19a85c8aff..ba0165dbce 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -1959,7 +1959,7 @@ class NormalActionsTest(BaseAction): check_user_group_add_members("events[3]", events[3]) def test_change_notification_settings(self) -> None: - for notification_setting, v in self.user_profile.notification_setting_types.items(): + for notification_setting in self.user_profile.notification_setting_types: if notification_setting in [ "notification_sound", "desktop_icon_count_display", diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py index 0f766278e1..bc1209b576 100644 --- a/zerver/tests/test_slack_importer.py +++ b/zerver/tests/test_slack_importer.py @@ -1345,8 +1345,7 @@ class SlackImporter(ZulipTestCase): self.assertEqual(Message.objects.filter(realm=realm).count(), 82) # All auth backends are enabled initially. - for name, enabled in realm.authentication_methods_dict().items(): - self.assertTrue(enabled) + self.assertTrue(all(realm.authentication_methods_dict().values())) Realm.objects.filter(name=test_realm_subdomain).delete()