From 91e5ef39ebc994efd246f62288affdfaa29e94f2 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 21 Jul 2023 16:15:10 -0700 Subject: [PATCH] ruff: Fix PLR1714 Consider merging multiple comparisons. Signed-off-by: Anders Kaseorg (cherry picked from commit 2ae285af7c8e178ead8ff9e436161238a1b7efd6) --- analytics/views/user_activity.py | 2 +- scripts/zulip-puppet-apply | 4 ++-- tools/lib/sanity_check.py | 2 +- tools/test-backend | 2 +- zerver/actions/create_realm.py | 12 ++++++------ zerver/actions/custom_profile_fields.py | 11 ++++------- zerver/lib/email_notifications.py | 2 +- zerver/lib/logging_util.py | 2 +- zerver/lib/onboarding.py | 10 ++++------ zerver/models.py | 5 +---- zerver/views/custom_profile_fields.py | 2 +- zerver/webhooks/clubhouse/view.py | 2 +- zerver/webhooks/github/view.py | 2 +- zerver/webhooks/gitlab/view.py | 4 ++-- zerver/webhooks/pagerduty/view.py | 15 ++++++--------- zerver/webhooks/raygun/view.py | 8 ++------ zerver/webhooks/sonarr/view.py | 10 +++++----- zerver/webhooks/wordpress/view.py | 2 +- 18 files changed, 41 insertions(+), 56 deletions(-) diff --git a/analytics/views/user_activity.py b/analytics/views/user_activity.py index 012320b66c..5143f58c8f 100644 --- a/analytics/views/user_activity.py +++ b/analytics/views/user_activity.py @@ -60,7 +60,7 @@ def raw_user_activity_table(records: QuerySet[UserActivity]) -> str: def user_activity_summary_table(user_summary: Dict[str, Dict[str, Any]]) -> str: rows = [] for k, v in user_summary.items(): - if k == "name" or k == "user_profile_id": + if k in ("name", "user_profile_id"): continue client = k count = v["count"] diff --git a/scripts/zulip-puppet-apply b/scripts/zulip-puppet-apply index a5d38a867a..085c4ecbf2 100755 --- a/scripts/zulip-puppet-apply +++ b/scripts/zulip-puppet-apply @@ -101,7 +101,7 @@ if not args.noop and not args.force: sys.stdout.write("Apply changes? [y/N] ") sys.stdout.flush() do_apply = sys.stdin.readline().strip().lower() - if do_apply == "" or do_apply == "n": + if do_apply in ("", "n"): sys.exit(0) if args.noop and args.force: @@ -115,5 +115,5 @@ ret = subprocess.call([*puppet_cmd, "--detailed-exitcodes"], env=puppet_env) # ret = 2 => changes, no errors # ret = 4 => no changes, yes errors # ret = 6 => changes, yes errors -if ret != 0 and ret != 2: +if ret not in (0, 2): sys.exit(2) diff --git a/tools/lib/sanity_check.py b/tools/lib/sanity_check.py index b96875ee10..7a281512dd 100644 --- a/tools/lib/sanity_check.py +++ b/tools/lib/sanity_check.py @@ -14,7 +14,7 @@ def check_venv(filename: str) -> None: "to enter the development environment." ) - if user_name != "vagrant" and user_name != "zulipdev": + if user_name not in ("vagrant", "zulipdev"): print() print("If you are using Vagrant, first run `vagrant ssh` to enter the Vagrant guest.") sys.exit(1) diff --git a/tools/test-backend b/tools/test-backend index 15c2eb3fdf..525d0ca6bc 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -334,7 +334,7 @@ def main() -> None: if suite.startswith("test"): for root, dirs, files_names in os.walk(zerver_test_dir): for file_name in files_names: - if file_name == suite or file_name == suite + ".py": + if file_name in (suite, f"{suite}.py"): new_suite = os.path.join(root, file_name) args[i] = new_suite break diff --git a/zerver/actions/create_realm.py b/zerver/actions/create_realm.py index 434e87ed88..76cf24a81e 100644 --- a/zerver/actions/create_realm.py +++ b/zerver/actions/create_realm.py @@ -98,9 +98,9 @@ def set_realm_permissions_based_on_org_type(realm: Realm) -> None: # Custom configuration for educational organizations. The present # defaults are designed for a single class, not a department or # larger institution, since those are more common. - if ( - realm.org_type == Realm.ORG_TYPES["education_nonprofit"]["id"] - or realm.org_type == Realm.ORG_TYPES["education"]["id"] + if realm.org_type in ( + Realm.ORG_TYPES["education_nonprofit"]["id"], + Realm.ORG_TYPES["education"]["id"], ): # Limit user creation to administrators. realm.invite_to_realm_policy = Realm.POLICY_ADMINS_ONLY @@ -216,9 +216,9 @@ def do_create_realm( ) realm_default_email_address_visibility = RealmUserDefault.EMAIL_ADDRESS_VISIBILITY_EVERYONE - if ( - realm.org_type == Realm.ORG_TYPES["education_nonprofit"]["id"] - or realm.org_type == Realm.ORG_TYPES["education"]["id"] + if realm.org_type in ( + Realm.ORG_TYPES["education_nonprofit"]["id"], + Realm.ORG_TYPES["education"]["id"], ): # Email address of users should be initially visible to admins only. realm_default_email_address_visibility = ( diff --git a/zerver/actions/custom_profile_fields.py b/zerver/actions/custom_profile_fields.py index 3d3afb972c..5d83892c4c 100644 --- a/zerver/actions/custom_profile_fields.py +++ b/zerver/actions/custom_profile_fields.py @@ -61,9 +61,9 @@ def try_add_realm_custom_profile_field( display_in_profile_summary=display_in_profile_summary, ) custom_profile_field.hint = hint - if ( - custom_profile_field.field_type == CustomProfileField.SELECT - or custom_profile_field.field_type == CustomProfileField.EXTERNAL_ACCOUNT + if custom_profile_field.field_type in ( + CustomProfileField.SELECT, + CustomProfileField.EXTERNAL_ACCOUNT, ): custom_profile_field.field_data = orjson.dumps(field_data or {}).decode() @@ -109,10 +109,7 @@ def try_update_realm_custom_profile_field( field.name = name field.hint = hint field.display_in_profile_summary = display_in_profile_summary - if ( - field.field_type == CustomProfileField.SELECT - or field.field_type == CustomProfileField.EXTERNAL_ACCOUNT - ): + if field.field_type in (CustomProfileField.SELECT, CustomProfileField.EXTERNAL_ACCOUNT): if field.field_type == CustomProfileField.SELECT: assert field_data is not None remove_custom_profile_field_value_if_required(field, field_data) diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.py index b6d186912f..ae2260496f 100644 --- a/zerver/lib/email_notifications.py +++ b/zerver/lib/email_notifications.py @@ -515,7 +515,7 @@ def do_send_missedmessage_events_reply_in_zulip( { m["message"].sender for m in missed_messages - if m["trigger"] == "mentioned" or m["trigger"] == "wildcard_mentioned" + if m["trigger"] in ("mentioned", "wildcard_mentioned") } ) message = missed_messages[0]["message"] diff --git a/zerver/lib/logging_util.py b/zerver/lib/logging_util.py index 90c33c6351..7935639722 100644 --- a/zerver/lib/logging_util.py +++ b/zerver/lib/logging_util.py @@ -147,7 +147,7 @@ def find_log_origin(record: logging.LogRecord) -> str: if settings.LOGGING_SHOW_MODULE: module_name = find_log_caller_module(record) - if module_name == logger_name or module_name == record.name: + if module_name in (logger_name, record.name): # Abbreviate a bit. pass else: diff --git a/zerver/lib/onboarding.py b/zerver/lib/onboarding.py index b2b399d804..9227d7a501 100644 --- a/zerver/lib/onboarding.py +++ b/zerver/lib/onboarding.py @@ -41,12 +41,10 @@ def create_if_missing_realm_internal_bots() -> None: def send_initial_direct_message(user: UserProfile) -> None: # We adjust the initial Welcome Bot direct message for education organizations. - education_organization = False - if ( - user.realm.org_type == Realm.ORG_TYPES["education_nonprofit"]["id"] - or user.realm.org_type == Realm.ORG_TYPES["education"]["id"] - ): - education_organization = True + education_organization = user.realm.org_type in ( + Realm.ORG_TYPES["education_nonprofit"]["id"], + Realm.ORG_TYPES["education"]["id"], + ) # We need to override the language in this code path, because it's # called from account registration, which is a pre-account API diff --git a/zerver/models.py b/zerver/models.py index 602b01ca76..50675d7175 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -2022,10 +2022,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings): # type @property def is_realm_admin(self) -> bool: - return ( - self.role == UserProfile.ROLE_REALM_ADMINISTRATOR - or self.role == UserProfile.ROLE_REALM_OWNER - ) + return self.role in (UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER) @is_realm_admin.setter def is_realm_admin(self, value: bool) -> None: diff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.py index 1f51fe6e20..7618124734 100644 --- a/zerver/views/custom_profile_fields.py +++ b/zerver/views/custom_profile_fields.py @@ -80,7 +80,7 @@ def validate_display_in_profile_summary_field( # The LONG_TEXT field type doesn't make sense visually for profile # field summaries. The USER field type will require some further # client support. - if field_type == CustomProfileField.LONG_TEXT or field_type == CustomProfileField.USER: + if field_type in (CustomProfileField.LONG_TEXT, CustomProfileField.USER): raise JsonableError(_("Field type not supported for display in profile summary.")) diff --git a/zerver/webhooks/clubhouse/view.py b/zerver/webhooks/clubhouse/view.py index 72152320b9..7a74d53c1a 100644 --- a/zerver/webhooks/clubhouse/view.py +++ b/zerver/webhooks/clubhouse/view.py @@ -400,7 +400,7 @@ def get_story_create_github_entity_body(payload: WildValue, action: WildValue, e app_url=action["app_url"].tame(check_string), ), "name": pull_request_action["number"].tame(check_int) - if entity == "pull-request" or entity == "pull-request-comment" + if entity in ("pull-request", "pull-request-comment") else pull_request_action["name"].tame(check_string), "url": pull_request_action["url"].tame(check_string), "workflow_state_template": "", diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py index 0e32d08114..d68ccd7c22 100644 --- a/zerver/webhooks/github/view.py +++ b/zerver/webhooks/github/view.py @@ -75,7 +75,7 @@ def get_opened_or_update_pull_request_body(helper: Helper) -> str: description = pull_request["body"].tame(check_none_or(check_string)) target_branch = None base_branch = None - if action == "opened" or action == "merged": + if action in ("opened", "merged"): target_branch = pull_request["head"]["label"].tame(check_string) base_branch = pull_request["base"]["label"].tame(check_string) diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.py index d700708de8..b543d8c62c 100644 --- a/zerver/webhooks/gitlab/view.py +++ b/zerver/webhooks/gitlab/view.py @@ -454,7 +454,7 @@ def get_subject_based_on_event( ) -> str: if event == "Push Hook": return f"{get_repo_name(payload)} / {get_branch_name(payload)}" - elif event == "Job Hook" or event == "Build Hook": + elif event in ("Job Hook", "Build Hook"): return "{} / {}".format( payload["repository"]["name"].tame(check_string), get_branch_name(payload) ) @@ -479,7 +479,7 @@ def get_subject_based_on_event( id=payload["object_attributes"]["iid"].tame(check_int), title=payload["object_attributes"]["title"].tame(check_string), ) - elif event == "Note Hook Issue" or event == "Confidential Note Hook Issue": + elif event in ("Note Hook Issue", "Confidential Note Hook Issue"): return TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format( repo=get_repo_name(payload), type="issue", diff --git a/zerver/webhooks/pagerduty/view.py b/zerver/webhooks/pagerduty/view.py index 913baa2c53..17ba006914 100644 --- a/zerver/webhooks/pagerduty/view.py +++ b/zerver/webhooks/pagerduty/view.py @@ -213,15 +213,12 @@ def send_formatted_pagerduty( "incident.unacknowledged", ): template = INCIDENT_WITH_SERVICE_AND_ASSIGNEE - elif ( - message_type == "incident.resolve" or message_type == "incident.resolved" - ) and format_dict.get("agent_info") is not None: - template = INCIDENT_RESOLVED_WITH_AGENT - elif ( - message_type == "incident.resolve" or message_type == "incident.resolved" - ) and format_dict.get("agent_info") is None: - template = INCIDENT_RESOLVED - elif message_type == "incident.assign" or message_type == "incident.reassigned": + elif message_type in ("incident.resolve", "incident.resolved"): + if "agent_info" in format_dict: + template = INCIDENT_RESOLVED_WITH_AGENT + else: + template = INCIDENT_RESOLVED + elif message_type in ("incident.assign", "incident.reassigned"): template = INCIDENT_ASSIGNED else: template = INCIDENT_WITH_ASSIGNEE diff --git a/zerver/webhooks/raygun/view.py b/zerver/webhooks/raygun/view.py index aa0dfaa281..d8768d2831 100644 --- a/zerver/webhooks/raygun/view.py +++ b/zerver/webhooks/raygun/view.py @@ -227,7 +227,7 @@ def compose_notification_message(payload: WildValue) -> str: # We now split this main function again into two functions. One is for # "NewErrorOccurred" and "ErrorReoccurred", and one is for the rest. Both # functions will return a text message that is formatted for the chat. - if event_type == "NewErrorOccurred" or event_type == "ErrorReoccurred": + if event_type in ("NewErrorOccurred", "ErrorReoccurred"): return notification_message_error_occurred(payload) elif "FollowUp" in event_type: return notification_message_follow_up(payload) @@ -285,11 +285,7 @@ def compose_activity_message(payload: WildValue) -> str: # But, they all are almost identical and the only differences between them # are the keys at line 9 (check fixtures). So there's no need to split # the function like the notification one. - if ( - event_type == "StatusChanged" - or event_type == "AssignedToUser" - or event_type == "CommentAdded" - ): + if event_type in ("StatusChanged", "AssignedToUser", "CommentAdded"): return activity_message(payload) else: raise UnsupportedWebhookEventTypeError(event_type) diff --git a/zerver/webhooks/sonarr/view.py b/zerver/webhooks/sonarr/view.py index 9e1edccb63..9f2ca403f0 100644 --- a/zerver/webhooks/sonarr/view.py +++ b/zerver/webhooks/sonarr/view.py @@ -56,14 +56,14 @@ def api_sonarr_webhook( def get_subject_for_http_request(payload: WildValue) -> str: event_type = payload["eventType"].tame(check_string) - if event_type != "Test" and event_type != "Health": - topic = SONARR_TOPIC_TEMPLATE.format( - series_title=payload["series"]["title"].tame(check_string) - ) - elif event_type == "Test": + if event_type == "Test": topic = SONARR_TOPIC_TEMPLATE_TEST elif event_type == "Health": topic = SONARR_TOPIC_TEMPLATE_HEALTH_CHECK.format(level=payload["level"].tame(check_string)) + else: + topic = SONARR_TOPIC_TEMPLATE.format( + series_title=payload["series"]["title"].tame(check_string) + ) return topic diff --git a/zerver/webhooks/wordpress/view.py b/zerver/webhooks/wordpress/view.py index bdce82434d..d5cb8bad2a 100644 --- a/zerver/webhooks/wordpress/view.py +++ b/zerver/webhooks/wordpress/view.py @@ -43,7 +43,7 @@ def api_wordpress_webhook( # remove trailing whitespace (issue for some test fixtures) hook = hook.rstrip() - if hook == "publish_post" or hook == "publish_page": + if hook in ("publish_post", "publish_page"): data = PUBLISH_POST_OR_PAGE_TEMPLATE.format(type=post_type, title=post_title, url=post_url) elif hook == "user_register":