Skip to content

Commit 58a3026

Browse files
committed
ruff: Fix SIM102 Use a single if statement instead of nested if statements.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent c75f5b3 commit 58a3026

File tree

5 files changed

+53
-61
lines changed

5 files changed

+53
-61
lines changed

zulip/integrations/zephyr/zephyr_mirror_backend.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,13 @@ def process_notice(
489489
# Only forward mail zephyrs if forwarding them is enabled.
490490
return
491491

492-
if is_personal:
493-
if body.startswith("CC:"):
494-
is_huddle = True
495-
# Map "CC: user1 user2" => "[email protected], [email protected]"
496-
huddle_recipients = [
497-
to_zulip_username(x.strip()) for x in body.split("\n")[0][4:].split()
498-
]
499-
if zephyr_sender not in huddle_recipients:
500-
huddle_recipients.append(to_zulip_username(zephyr_sender))
501-
body = body.split("\n", 1)[1]
492+
if is_personal and body.startswith("CC:"):
493+
is_huddle = True
494+
# Map "CC: user1 user2" => "[email protected], [email protected]"
495+
huddle_recipients = [to_zulip_username(x.strip()) for x in body.split("\n")[0][4:].split()]
496+
if zephyr_sender not in huddle_recipients:
497+
huddle_recipients.append(to_zulip_username(zephyr_sender))
498+
body = body.split("\n", 1)[1]
502499

503500
if (
504501
options.forward_class_messages
@@ -1045,13 +1042,12 @@ def add_zulip_subscriptions(verbose: bool) -> None:
10451042
", ".join(unauthorized),
10461043
)
10471044

1048-
if len(skipped) > 0:
1049-
if verbose:
1050-
logger.info(
1051-
"\n%s\n",
1052-
"\n".join(
1053-
textwrap.wrap(
1054-
"""\
1045+
if len(skipped) > 0 and verbose:
1046+
logger.info(
1047+
"\n%s\n",
1048+
"\n".join(
1049+
textwrap.wrap(
1050+
"""\
10551051
You have some lines in ~/.zephyr.subs that could not be
10561052
synced to your Zulip subscriptions because they do not
10571053
use "*" as both the instance and recipient and not one of
@@ -1061,30 +1057,29 @@ def add_zulip_subscriptions(verbose: bool) -> None:
10611057
stream, so this tool has not created a corresponding
10621058
Zulip subscription to these lines in ~/.zephyr.subs:
10631059
"""
1064-
)
1065-
),
1066-
)
1060+
)
1061+
),
1062+
)
10671063

10681064
for cls, instance, recipient, reason in skipped:
10691065
if verbose:
10701066
if reason != "":
10711067
logger.info(" [%s,%s,%s] (%s)", cls, instance, recipient, reason)
10721068
else:
10731069
logger.info(" [%s,%s,%s]", cls, instance, recipient)
1074-
if len(skipped) > 0:
1075-
if verbose:
1076-
logger.info(
1077-
"\n%s\n",
1078-
"\n".join(
1079-
textwrap.wrap(
1080-
"""\
1070+
if len(skipped) > 0 and verbose:
1071+
logger.info(
1072+
"\n%s\n",
1073+
"\n".join(
1074+
textwrap.wrap(
1075+
"""\
10811076
If you wish to be subscribed to any Zulip streams related
10821077
to these .zephyrs.subs lines, please do so via the Zulip
10831078
web interface.
10841079
"""
1085-
)
1086-
),
1087-
)
1080+
)
1081+
),
1082+
)
10881083

10891084

10901085
def valid_stream_name(name: str) -> bool:

zulip/zulip/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,8 @@ def __init__(
501501
else: # we have a client cert
502502
if not os.path.isfile(client_cert):
503503
raise ConfigNotFoundError(f"client cert '{client_cert}' does not exist")
504-
if client_cert_key is not None:
505-
if not os.path.isfile(client_cert_key):
506-
raise ConfigNotFoundError(f"client cert key '{client_cert_key}' does not exist")
504+
if client_cert_key is not None and not os.path.isfile(client_cert_key):
505+
raise ConfigNotFoundError(f"client cert key '{client_cert_key}' does not exist")
507506
self.client_cert = client_cert
508507
self.client_cert_key = client_cert_key
509508

@@ -652,10 +651,11 @@ def end_error_retry(succeeded: bool) -> None:
652651
self.has_connected = True
653652

654653
# On 50x errors, try again after a short sleep
655-
if str(res.status_code).startswith("5"):
656-
if error_retry(f" (server {res.status_code})"):
657-
continue
658-
# Otherwise fall through and process the python-requests error normally
654+
if str(res.status_code).startswith("5") and error_retry(
655+
f" (server {res.status_code})"
656+
):
657+
continue
658+
# Otherwise fall through and process the python-requests error normally
659659
except (requests.exceptions.Timeout, requests.exceptions.SSLError) as e:
660660
# Timeouts are either a Timeout or an SSLError; we
661661
# want the later exception handlers to deal with any

zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ def get_bot_result(message_content: str, config: Dict[str, str], sender_id: str)
2929
res_json["status"]["code"], res_json["status"]["errorDetails"]
3030
)
3131
if res_json["result"]["fulfillment"]["speech"] == "":
32-
if "alternateResult" in res_json.keys():
33-
if res_json["alternateResult"]["fulfillment"]["speech"] != "":
34-
return res_json["alternateResult"]["fulfillment"]["speech"]
32+
if (
33+
"alternateResult" in res_json.keys()
34+
and res_json["alternateResult"]["fulfillment"]["speech"] != ""
35+
):
36+
return res_json["alternateResult"]["fulfillment"]["speech"]
3537
return "Error. No result."
3638
return res_json["result"]["fulfillment"]["speech"]
3739
except Exception as e:

zulip_bots/zulip_bots/bots/merels/merels.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ def contains_winning_move(self, board: Any) -> bool:
3434
merels = database.MerelsStorage(self.topic, self.storage)
3535
data = game_data.GameData(merels.get_game_data(self.topic))
3636

37-
if data.get_phase() > 1:
38-
if (mechanics.get_piece("X", data.grid()) <= 2) or (
39-
mechanics.get_piece("O", data.grid()) <= 2
40-
):
41-
return True
42-
return False
37+
return data.get_phase() > 1 and (
38+
(mechanics.get_piece("X", data.grid()) <= 2)
39+
or (mechanics.get_piece("O", data.grid()) <= 2)
40+
)
4341

4442
def make_move(self, move: str, player_number: int, computer_move: bool = False) -> Any:
4543
if self.storage.get(self.topic) == '["X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0]':

zulip_bots/zulip_bots/game_handler.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,8 @@ def get_game_info(self, game_id: str) -> Dict[str, Any]:
603603
def get_user_by_name(self, name: str) -> Dict[str, Any]:
604604
name = name.strip()
605605
for user in self.user_cache.values():
606-
if "full_name" in user.keys():
607-
if user["full_name"].lower() == name.lower():
608-
return user
606+
if "full_name" in user.keys() and user["full_name"].lower() == name.lower():
607+
return user
609608
return {}
610609

611610
def get_number_of_players(self, game_id: str) -> int:
@@ -756,9 +755,8 @@ def is_user_not_player(self, user_email: str, message: Dict[str, Any] = {}) -> b
756755
return False
757756
for invite in self.invites.values():
758757
for u in invite.keys():
759-
if u == "host":
760-
if user_email == invite["host"]:
761-
return False
758+
if u == "host" and user_email == invite["host"]:
759+
return False
762760
if u == user_email and "a" in invite[u]:
763761
return False
764762
return True
@@ -776,15 +774,14 @@ def broadcast(self, game_id: str, content: str, include_private: bool = True) ->
776774
if private_recipients is not None:
777775
for user in private_recipients:
778776
self.send_message(user, content, True)
779-
if game_id in self.invites.keys():
780-
if self.invites[game_id]["subject"] != "###private###":
781-
self.send_message(
782-
self.invites[game_id]["stream"],
783-
content,
784-
False,
785-
self.invites[game_id]["subject"],
786-
)
787-
return True
777+
if game_id in self.invites.keys() and self.invites[game_id]["subject"] != "###private###":
778+
self.send_message(
779+
self.invites[game_id]["stream"],
780+
content,
781+
False,
782+
self.invites[game_id]["subject"],
783+
)
784+
return True
788785
if game_id in self.instances.keys():
789786
self.send_message(
790787
self.instances[game_id].stream, content, False, self.instances[game_id].subject

0 commit comments

Comments
 (0)