ruff: Fix RUF019 Unnecessary key check before dictionary access.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 712917b2c9)
This commit is contained in:
Anders Kaseorg 2024-02-01 16:22:50 -08:00 committed by Tim Abbott
parent 3bf1934598
commit 1fa8862e48
4 changed files with 17 additions and 17 deletions

View File

@ -1318,7 +1318,7 @@ def fetch_team_icons(
records = [] records = []
team_icons_dict = team_info_dict["icon"] 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 [] return []
icon_url = ( icon_url = (

View File

@ -259,6 +259,6 @@ def generate_data_type(schema: Mapping[str, Any]) -> str:
data_type = "(" + generate_data_type(schema["items"]) + ")[]" data_type = "(" + generate_data_type(schema["items"]) + ")[]"
else: else:
data_type = schema["type"] data_type = schema["type"]
if "nullable" in schema and schema["nullable"]: if schema.get("nullable", False):
data_type = data_type + " | null" data_type = data_type + " | null"
return data_type return data_type

View File

@ -81,7 +81,7 @@ class GenericOutgoingWebhookService(OutgoingWebhookServiceInterface):
@override @override
def process_success(self, response_json: Dict[str, Any]) -> Optional[Dict[str, Any]]: 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 return None
if "response_string" in response_json: if "response_string" in response_json:

View File

@ -61,16 +61,16 @@ def api_slack_incoming_webhook(
user_specified_topic = "(no topic)" user_specified_topic = "(no topic)"
pieces: List[str] = [] pieces: List[str] = []
if "blocks" in payload and payload["blocks"]: if payload.get("blocks"):
pieces += map(render_block, payload["blocks"]) pieces += map(render_block, payload["blocks"])
if "attachments" in payload and payload["attachments"]: if payload.get("attachments"):
pieces += map(render_attachment, payload["attachments"]) pieces += map(render_attachment, payload["attachments"])
body = "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "") body = "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "")
if body == "" and "text" in payload and payload["text"]: if body == "" and payload.get("text"):
if "icon_emoji" in payload and payload["icon_emoji"]: if payload.get("icon_emoji"):
body = payload["icon_emoji"].tame(check_string) + " " body = payload["icon_emoji"].tame(check_string) + " "
body += payload["text"].tame(check_string) body += payload["text"].tame(check_string)
body = body.strip() 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 # rest of the fields we handle here are legacy fields. These fields are
# optional and may contain null values. # optional and may contain null values.
pieces = [] pieces = []
if "title" in attachment and attachment["title"]: if attachment.get("title"):
title = attachment["title"].tame(check_string) 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) title_link = attachment["title_link"].tame(check_url)
pieces.append(f"## [{title}]({title_link})") pieces.append(f"## [{title}]({title_link})")
else: else:
pieces.append(f"## {title}") pieces.append(f"## {title}")
if "pretext" in attachment and attachment["pretext"]: if attachment.get("pretext"):
pieces.append(attachment["pretext"].tame(check_string)) 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)) pieces.append(attachment["text"].tame(check_string))
if "fields" in attachment: if "fields" in attachment:
fields = [] fields = []
@ -210,20 +210,20 @@ def render_attachment(attachment: WildValue) -> str:
title = field["title"].tame(check_string) title = field["title"].tame(check_string)
value = field["value"].tame(check_string) value = field["value"].tame(check_string)
fields.append(f"*{title}*: {value}") fields.append(f"*{title}*: {value}")
elif "title" in field and field["title"]: elif field.get("title"):
title = field["title"].tame(check_string) title = field["title"].tame(check_string)
fields.append(f"*{title}*") fields.append(f"*{title}*")
elif "value" in field and field["value"]: elif field.get("value"):
value = field["value"].tame(check_string) value = field["value"].tame(check_string)
fields.append(f"{value}") fields.append(f"{value}")
pieces.append("\n".join(fields)) pieces.append("\n".join(fields))
if "blocks" in attachment and attachment["blocks"]: if attachment.get("blocks"):
pieces += map(render_block, attachment["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))) 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)) pieces.append(attachment["footer"].tame(check_string))
if "ts" in attachment and attachment["ts"]: if attachment.get("ts"):
time = attachment["ts"].tame(check_int) time = attachment["ts"].tame(check_int)
pieces.append(f"<time:{time}>") pieces.append(f"<time:{time}>")