From 1fa8862e48e5641e6db9d40bf6dbe15a256d1e85 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 1 Feb 2024 16:22:50 -0800 Subject: [PATCH] ruff: Fix RUF019 Unnecessary key check before dictionary access. Signed-off-by: Anders Kaseorg (cherry picked from commit 712917b2c9db16234a6a95049fc82fa4258d2fd9) --- zerver/data_import/slack.py | 2 +- .../markdown/api_arguments_table_generator.py | 2 +- zerver/lib/outgoing_webhook.py | 2 +- zerver/webhooks/slack_incoming/view.py | 28 +++++++++---------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py index e5855676fc..efa2d5008e 100644 --- a/zerver/data_import/slack.py +++ b/zerver/data_import/slack.py @@ -1318,7 +1318,7 @@ def fetch_team_icons( records = [] team_icons_dict = team_info_dict["icon"] - if "image_default" in team_icons_dict and team_icons_dict["image_default"]: + if team_icons_dict.get("image_default", False): return [] icon_url = ( diff --git a/zerver/lib/markdown/api_arguments_table_generator.py b/zerver/lib/markdown/api_arguments_table_generator.py index 9d30106346..d528dcf2e8 100644 --- a/zerver/lib/markdown/api_arguments_table_generator.py +++ b/zerver/lib/markdown/api_arguments_table_generator.py @@ -259,6 +259,6 @@ def generate_data_type(schema: Mapping[str, Any]) -> str: data_type = "(" + generate_data_type(schema["items"]) + ")[]" else: data_type = schema["type"] - if "nullable" in schema and schema["nullable"]: + if schema.get("nullable", False): data_type = data_type + " | null" return data_type diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.py index ff6ac8c668..b45decb172 100644 --- a/zerver/lib/outgoing_webhook.py +++ b/zerver/lib/outgoing_webhook.py @@ -81,7 +81,7 @@ class GenericOutgoingWebhookService(OutgoingWebhookServiceInterface): @override def process_success(self, response_json: Dict[str, Any]) -> Optional[Dict[str, Any]]: - if "response_not_required" in response_json and response_json["response_not_required"]: + if response_json.get("response_not_required", False): return None if "response_string" in response_json: diff --git a/zerver/webhooks/slack_incoming/view.py b/zerver/webhooks/slack_incoming/view.py index 9d8d09e11b..238abf5f74 100644 --- a/zerver/webhooks/slack_incoming/view.py +++ b/zerver/webhooks/slack_incoming/view.py @@ -61,16 +61,16 @@ def api_slack_incoming_webhook( user_specified_topic = "(no topic)" pieces: List[str] = [] - if "blocks" in payload and payload["blocks"]: + if payload.get("blocks"): pieces += map(render_block, payload["blocks"]) - if "attachments" in payload and payload["attachments"]: + if payload.get("attachments"): pieces += map(render_attachment, payload["attachments"]) body = "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "") - if body == "" and "text" in payload and payload["text"]: - if "icon_emoji" in payload and payload["icon_emoji"]: + if body == "" and payload.get("text"): + if payload.get("icon_emoji"): body = payload["icon_emoji"].tame(check_string) + " " body += payload["text"].tame(check_string) body = body.strip() @@ -192,16 +192,16 @@ def render_attachment(attachment: WildValue) -> str: # rest of the fields we handle here are legacy fields. These fields are # optional and may contain null values. pieces = [] - if "title" in attachment and attachment["title"]: + if attachment.get("title"): title = attachment["title"].tame(check_string) - if "title_link" in attachment and attachment["title_link"]: + if attachment.get("title_link"): title_link = attachment["title_link"].tame(check_url) pieces.append(f"## [{title}]({title_link})") else: pieces.append(f"## {title}") - if "pretext" in attachment and attachment["pretext"]: + if attachment.get("pretext"): pieces.append(attachment["pretext"].tame(check_string)) - if "text" in attachment and attachment["text"]: + if attachment.get("text"): pieces.append(attachment["text"].tame(check_string)) if "fields" in attachment: fields = [] @@ -210,20 +210,20 @@ def render_attachment(attachment: WildValue) -> str: title = field["title"].tame(check_string) value = field["value"].tame(check_string) fields.append(f"*{title}*: {value}") - elif "title" in field and field["title"]: + elif field.get("title"): title = field["title"].tame(check_string) fields.append(f"*{title}*") - elif "value" in field and field["value"]: + elif field.get("value"): value = field["value"].tame(check_string) fields.append(f"{value}") pieces.append("\n".join(fields)) - if "blocks" in attachment and attachment["blocks"]: + if attachment.get("blocks"): pieces += map(render_block, attachment["blocks"]) - if "image_url" in attachment and attachment["image_url"]: + if attachment.get("image_url"): pieces.append("[]({})".format(attachment["image_url"].tame(check_url))) - if "footer" in attachment and attachment["footer"]: + if attachment.get("footer"): pieces.append(attachment["footer"].tame(check_string)) - if "ts" in attachment and attachment["ts"]: + if attachment.get("ts"): time = attachment["ts"].tame(check_int) pieces.append(f"")