From b009dcd7d3251bd4110dc9b85ece5296930c1cd7 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 29 Oct 2023 17:24:29 -0700 Subject: [PATCH] ruff: Fix SIM108 Use ternary operator instead of `if`-`else`-block. Signed-off-by: Anders Kaseorg --- tools/deploy | 10 +---- tools/provision | 11 ++--- .../jabber/jabber_mirror_backend.py | 5 +-- .../perforce/zulip_perforce_config.py | 7 +-- zulip/integrations/twitter/twitter-bot | 10 +---- zulip/zulip/__init__.py | 44 +++++++------------ .../bots/dropbox_share/dropbox_share.py | 10 +---- .../bots/merels/libraries/mechanics.py | 5 +-- .../zulip_bots/bots/virtual_fs/virtual_fs.py | 5 +-- 9 files changed, 30 insertions(+), 77 deletions(-) diff --git a/tools/deploy b/tools/deploy index dd70a19d..6da11daa 100755 --- a/tools/deploy +++ b/tools/deploy @@ -181,10 +181,7 @@ def prepare(options: argparse.Namespace) -> None: def log(options: argparse.Namespace) -> None: check_common_options(options) headers = {"key": options.token} - if options.lines: - lines = options.lines - else: - lines = None + lines = options.lines payload = {"name": options.botname, "lines": lines} url = urllib.parse.urljoin(options.server, "bots/logs/" + options.botname) response = requests.get(url, json=payload, headers=headers) @@ -209,10 +206,7 @@ def delete(options: argparse.Namespace) -> None: def list_bots(options: argparse.Namespace) -> None: check_common_options(options) headers = {"key": options.token} - if options.format: - pretty_print = True - else: - pretty_print = False + pretty_print = options.format url = urllib.parse.urljoin(options.server, "bots/list") response = requests.get(url, headers=headers) result = handle_common_response( diff --git a/tools/provision b/tools/provision index 8b7cf45c..e261d612 100755 --- a/tools/provision +++ b/tools/provision @@ -77,13 +77,10 @@ the Python version this command is executed with.""" else: print("Virtualenv already exists.") - if os.path.isdir(os.path.join(venv_dir, "Scripts")): - # POSIX compatibility layer and Linux environment emulation for Windows - # venv uses /Scripts instead of /bin on Windows cmd and Power Shell. - # Read https://docs.python.org/3/library/venv.html - venv_exec_dir = "Scripts" - else: - venv_exec_dir = "bin" + # POSIX compatibility layer and Linux environment emulation for Windows + # venv uses /Scripts instead of /bin on Windows cmd and Power Shell. + # Read https://docs.python.org/3/library/venv.html + venv_exec_dir = "Scripts" if os.path.isdir(os.path.join(venv_dir, "Scripts")) else "bin" # On OS X, ensure we use the virtualenv version of the python binary for # future subprocesses instead of the version that this script was launched with. See diff --git a/zulip/integrations/jabber/jabber_mirror_backend.py b/zulip/integrations/jabber/jabber_mirror_backend.py index 7162f62c..96190905 100755 --- a/zulip/integrations/jabber/jabber_mirror_backend.py +++ b/zulip/integrations/jabber/jabber_mirror_backend.py @@ -452,10 +452,7 @@ option does not affect login credentials.""".replace("\n", " "), zulipToJabber.set_jabber_client(xmpp) xmpp.process(block=False) - if options.mode == "public": - event_types = ["stream"] - else: - event_types = ["message", "subscription"] + event_types = ["stream"] if options.mode == "public" else ["message", "subscription"] try: logging.info("Connecting to Zulip.") diff --git a/zulip/integrations/perforce/zulip_perforce_config.py b/zulip/integrations/perforce/zulip_perforce_config.py index 15f5d8ff..2a6ca4b5 100644 --- a/zulip/integrations/perforce/zulip_perforce_config.py +++ b/zulip/integrations/perforce/zulip_perforce_config.py @@ -31,11 +31,8 @@ P4_WEB: Optional[str] = None # * subject "change_root" def commit_notice_destination(path: str, changelist: int) -> Optional[Dict[str, str]]: dirs = path.split("/") - if len(dirs) >= 4 and dirs[3] not in ("*", "..."): - directory = dirs[3] - else: - # No subdirectory, so just use "depot" - directory = dirs[2] + # If no subdirectory, just use "depot" + directory = dirs[3] if len(dirs) >= 4 and dirs[3] not in ("*", "...") else dirs[2] if directory not in ["evil-master-plan", "my-super-secret-repository"]: return dict(stream=f"{directory}-commits", subject=path) diff --git a/zulip/integrations/twitter/twitter-bot b/zulip/integrations/twitter/twitter-bot index 15823f48..94fbd0ea 100755 --- a/zulip/integrations/twitter/twitter-bot +++ b/zulip/integrations/twitter/twitter-bot @@ -181,15 +181,9 @@ elif opts.twitter_name: else: statuses = api.GetUserTimeline(screen_name=opts.twitter_name, since_id=since_id) -if opts.excluded_terms: - excluded_terms = opts.excluded_terms.split(",") -else: - excluded_terms = [] +excluded_terms = opts.excluded_terms.split(",") if opts.excluded_terms else [] -if opts.excluded_users: - excluded_users = opts.excluded_users.split(",") -else: - excluded_users = [] +excluded_users = opts.excluded_users.split(",") if opts.excluded_users else [] for status in statuses[::-1][: opts.limit_tweets]: # Check if the tweet is from an excluded user diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 41ecc951..a2307e7c 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -36,9 +36,6 @@ assert sys.version_info >= (3, 6) logger = logging.getLogger(__name__) -# In newer versions, the 'json' attribute is a function, not a property -requests_json_is_function = callable(requests.Response.json) - API_VERSTRING = "v1/" # An optional parameter to `move_topic` and `update_message` actions @@ -571,14 +568,11 @@ class Client: if files is None: files = [] - if longpolling: - # When long-polling, set timeout to 90 sec as a balance - # between a low traffic rate and a still reasonable latency - # time in case of a connection failure. - request_timeout = 90.0 - else: - # Otherwise, 15s should be plenty of time. - request_timeout = 15.0 if not timeout else timeout + # When long-polling, set timeout to 90 sec as a balance + # between a low traffic rate and a still reasonable latency + # time in case of a connection failure. + # Otherwise, 15s should be plenty of time. + request_timeout = 90.0 if longpolling else timeout or 15.0 request = {} req_files = [] @@ -630,10 +624,7 @@ class Client: while True: try: - if method == "GET": - kwarg = "params" - else: - kwarg = "data" + kwarg = "params" if method == "GET" else "data" kwargs = {kwarg: query_state["request"]} @@ -689,22 +680,17 @@ class Client: raise try: - if requests_json_is_function: - json_result = res.json() - else: - json_result = res.json + json_result = res.json() except Exception: - json_result = None + end_error_retry(False) + return { + "msg": "Unexpected error from the server", + "result": "http-error", + "status_code": res.status_code, + } - if json_result is not None: - end_error_retry(True) - return json_result - end_error_retry(False) - return { - "msg": "Unexpected error from the server", - "result": "http-error", - "status_code": res.status_code, - } + end_error_retry(True) + return json_result def call_endpoint( self, diff --git a/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py b/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py index 29c9ba11..ab30911c 100644 --- a/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py +++ b/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py @@ -113,10 +113,7 @@ def syntax_help(cmd_name: str) -> str: commands = get_commands() f, arg_names = commands[cmd_name] arg_syntax = " ".join("<" + a + ">" for a in arg_names) - if arg_syntax: - cmd = cmd_name + " " + arg_syntax - else: - cmd = cmd_name + cmd = cmd_name + " " + arg_syntax if arg_syntax else cmd_name return f"syntax: {cmd}" @@ -207,10 +204,7 @@ def dbx_read(client: Any, fn: str) -> str: def dbx_search(client: Any, query: str, folder: str, max_results: str) -> str: - if folder is None: - folder = "" - else: - folder = "/" + folder + folder = "" if folder is None else "/" + folder if max_results is None: max_results = "20" try: diff --git a/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py b/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py index 9501211d..4ef9c3b6 100644 --- a/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py +++ b/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py @@ -303,10 +303,7 @@ def display_game(topic_name, merels_storage): response = "" - if data.take_mode == 1: - take = "Yes" - else: - take = "No" + take = "Yes" if data.take_mode == 1 else "No" response += interface.graph_grid(data.grid()) + "\n" response += f"""Phase {data.get_phase()}. Take mode: {take}. diff --git a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py b/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py index a514e310..5bf443fb 100644 --- a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py +++ b/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py @@ -166,10 +166,7 @@ def syntax_help(cmd_name: str) -> str: commands = get_commands() f, arg_names = commands[cmd_name] arg_syntax = " ".join("<" + a + ">" for a in arg_names) - if arg_syntax: - cmd = cmd_name + " " + arg_syntax - else: - cmd = cmd_name + cmd = cmd_name + " " + arg_syntax if arg_syntax else cmd_name return f"syntax: {cmd}"