From 5ccf9e061e3eade28c4eb6658bc807c4f50b9bbb Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 7 Aug 2026 09:37:47 +0000 Subject: [PATCH] ruff RUF015 (prefer `next(...)` over single element slice) --- mmgen/addrdata.py | 2 +- mmgen/color.py | 2 +- mmgen/daemon.py | 2 +- mmgen/proto/btc/rpc/local.py | 2 +- mmgen/protocol.py | 2 +- mmgen/tw/view.py | 2 +- mmgen/tx/keys.py | 2 +- pyproject.toml | 1 - test/cmdtest_d/httpd/etherscan.py | 2 +- test/cmdtest_d/regtest.py | 4 ++-- test/cmdtest_d/shared.py | 2 +- test/cmdtest_d/swap.py | 2 +- test/modtest_d/subseed.py | 6 +++--- test/modtest_d/util.py | 8 ++++---- test/objtest.py | 2 +- 15 files changed, 20 insertions(+), 21 deletions(-) diff --git a/mmgen/addrdata.py b/mmgen/addrdata.py index b79862d0..7a927449 100755 --- a/mmgen/addrdata.py +++ b/mmgen/addrdata.py @@ -51,7 +51,7 @@ class AddrData(MMGenObject): def coinaddr2mmaddr(self, coinaddr): d = self.make_reverse_dict([coinaddr]) - return (list(d.values())[0][0]) if d else None + return next(iter(d.values()))[0] if d else None def add(self, addrlist): if isinstance(addrlist, AddrList): diff --git a/mmgen/color.py b/mmgen/color.py index 6f9c9f0a..c9d8d25f 100755 --- a/mmgen/color.py +++ b/mmgen/color.py @@ -68,7 +68,7 @@ def get_terminfo_colors(term=None): return None else: set_vt100() - s = [e.split('#')[1] for e in cmdout.split(',') if e.startswith('colors')][0] + s = next(iter(e.split('#', 1)[1] for e in cmdout.split(',') if e.startswith('colors'))) from .util import is_hex_str if s.isdecimal(): return int(s) diff --git a/mmgen/daemon.py b/mmgen/daemon.py index ddda8982..d5566939 100755 --- a/mmgen/daemon.py +++ b/mmgen/daemon.py @@ -255,7 +255,7 @@ class Daemon(Lockable): die(2, f'Unable to execute {cls.exec_fn}') else: res = cp.stdout.splitlines() - return (res[0] if len(res) == 1 else [s for s in res if 'ersion' in s][0]).strip() + return (res[0] if len(res) == 1 else next(s for s in res if 'ersion' in s)).strip() class RPCDaemon(Daemon): diff --git a/mmgen/proto/btc/rpc/local.py b/mmgen/proto/btc/rpc/local.py index b11ec9fd..69738108 100755 --- a/mmgen/proto/btc/rpc/local.py +++ b/mmgen/proto/btc/rpc/local.py @@ -354,7 +354,7 @@ class BitcoinRPCClient(RPCClient, metaclass=AsyncInit): self.daemon.cfg_file) def get_daemon_cfg_option(self, req_key): - return list(self.get_daemon_cfg_options([req_key]).values())[0] + return next(iter(self.get_daemon_cfg_options([req_key]).values())) def get_daemon_cfg_options(self, req_keys): diff --git a/mmgen/protocol.py b/mmgen/protocol.py index b5e57a48..e23ea427 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -92,7 +92,7 @@ class CoinProtocol(MMGenObject): if hasattr(self, 'addr_ver_info'): self.addr_ver_bytes = {bytes.fromhex(k): v for k, v in self.addr_ver_info.items()} self.addr_fmt_to_ver_bytes = {v: k for k, v in self.addr_ver_bytes.items()} - self.addr_ver_bytes_len = len(list(self.addr_ver_bytes)[0]) + self.addr_ver_bytes_len = len(next(iter(self.addr_ver_bytes))) if gc.cmd_caps: for cap in gc.cmd_caps.caps: diff --git a/mmgen/tw/view.py b/mmgen/tw/view.py index 50f1fad3..ce12a530 100755 --- a/mmgen/tw/view.py +++ b/mmgen/tw/view.py @@ -673,7 +673,7 @@ class TwView(MMGenObject, metaclass=AsyncInit): async def do_error_msg(): msg_r( 'Choice must be a single number between {n} and {m} inclusive{s}'.format( - n = list(data.keys())[0] if is_addr_idx else 1, + n = next(iter(data.keys())) if is_addr_idx else 1, m = list(data.keys())[-1] if is_addr_idx else len(data), s = ' ' if self.scroll else '')) if self.scroll: diff --git a/mmgen/tx/keys.py b/mmgen/tx/keys.py index 5d6112fe..b6a2a7a7 100755 --- a/mmgen/tx/keys.py +++ b/mmgen/tx/keys.py @@ -128,7 +128,7 @@ class TxKeys: ignore_in_fmt = True, passwd_file = self.passwdfile).seed elif self.saved_seeds and subseeds_checked is False: - seed = self.saved_seeds[list(self.saved_seeds)[0]].subseed_by_seed_id(sid, print_msg=True) + seed = self.saved_seeds[next(iter(self.saved_seeds))].subseed_by_seed_id(sid, print_msg=True) subseeds_checked = True if not seed: continue diff --git a/pyproject.toml b/pyproject.toml index 7658a9e6..23264d64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,6 @@ ignore = [ "PLW1510", # `subprocess.run` without explicit `check` argument "RET501", # Do not explicitly `return None` in function if it is the only possible return value "RUF012", # Mutable default value for class attribute - "RUF015", # Prefer `next(s for s in res if 'ersion' in s)` over single element slice "RUF022", # `__all__` is not sorted "RUF100", # Unused `noqa` directive (non-enabled: `F401`) "S102", # Use of `exec` detected diff --git a/test/cmdtest_d/httpd/etherscan.py b/test/cmdtest_d/httpd/etherscan.py index 8c239e13..9b9c75e3 100755 --- a/test/cmdtest_d/httpd/etherscan.py +++ b/test/cmdtest_d/httpd/etherscan.py @@ -29,7 +29,7 @@ class EtherscanServer(HTTPD): target = 'result' length = int(environ.get('CONTENT_LENGTH', '0')) qs = environ['wsgi.input'].read(length).decode() - tx = [s for s in qs.split('&') if 'RawTx=' in s][0].split('=')[1] + tx = next(s for s in qs.split('&') if 'RawTx=' in s).split('=')[1] keccak_256 = get_keccak() txid = '0x' + keccak_256(bytes.fromhex(tx[2:])).hexdigest() diff --git a/test/cmdtest_d/regtest.py b/test/cmdtest_d/regtest.py index 2fe8e36d..4f096db4 100755 --- a/test/cmdtest_d/regtest.py +++ b/test/cmdtest_d/regtest.py @@ -1510,7 +1510,7 @@ class CmdTestRegtest(CmdTestBase, CmdTestShared): assert self.proto.cap('segwit') if not hasattr(self, '_b_start_'): t = self.spawn('mmgen-tool', ['--color=0', '--bob', 'listaddresses'], no_msg=True) - self._b_start_ = int([e for e in t.read().split('\n') if ':B:1' in e][0].split()[0].rstrip(')')) + self._b_start_ = int(next(e for e in t.read().split('\n') if ':B:1' in e).split(maxsplit=1)[0].rstrip(')')) t.close() return self._b_start_ @@ -1677,7 +1677,7 @@ class CmdTestRegtest(CmdTestBase, CmdTestShared): def alice_add_comment_coinaddr(self): mmid = self._user_sid('alice') + (':S:1', ':L:1')[self.proto.coin=='BCH'] t = self.spawn('mmgen-tool', ['--alice', 'listaddress', mmid, 'wide=true'], no_msg=True) - addr = [i for i in strip_ansi_escapes(t.read()).splitlines() if re.search(rf'\b{mmid}\b', i)][0].split()[3] + addr = next(i for i in strip_ansi_escapes(t.read()).splitlines() if re.search(rf'\b{mmid}\b', i)).split()[3] return self.user_add_comment('alice', addr, 'Label added using coin address of MMGen address') def alice_chk_comment_coinaddr(self): diff --git a/test/cmdtest_d/shared.py b/test/cmdtest_d/shared.py index bfe144ab..93fd067d 100755 --- a/test/cmdtest_d/shared.py +++ b/test/cmdtest_d/shared.py @@ -73,7 +73,7 @@ class CmdTestShared: e2 = fr'Using .*{auto_chg_addr}.* as.*address' res = t.expect([e1, e2], regex=True) if res == 0: - choice = [s.split(')')[0].lstrip() for s in t.p.match[0].split('\n') if auto_chg_addr in s][0] + choice = next(s.split(')', 1)[0].lstrip() for s in t.p.match[0].split('\n') if auto_chg_addr in s) t.send(f'{choice}\n') t.expect(e2, regex=True) t.send('y') diff --git a/test/cmdtest_d/swap.py b/test/cmdtest_d/swap.py index d80e70fb..7e79efd4 100755 --- a/test/cmdtest_d/swap.py +++ b/test/cmdtest_d/swap.py @@ -581,7 +581,7 @@ class CmdTestSwap(CmdTestSwapMethods, CmdTestRegtest, CmdTestAutosignThreaded): def fund_bob_recv_subwallet(self, proto_idx=1, amt='5'): coin_arg = f'--coin={self.protos[proto_idx].coin}' t = self.spawn('mmgen-tool', ['--bob', coin_arg, 'listaddresses']) - addr = [s for s in strip_ansi_escapes(t.read()).splitlines() if 'C:1 No' in s][0].split()[3] + addr = next(s for s in strip_ansi_escapes(t.read()).splitlines() if 'C:1 No' in s).split()[3] t = self.spawn( 'mmgen-regtest', [coin_arg, 'send', addr, str(amt)], diff --git a/test/modtest_d/subseed.py b/test/modtest_d/subseed.py index d9948091..9f8a5134 100755 --- a/test/modtest_d/subseed.py +++ b/test/modtest_d/subseed.py @@ -63,14 +63,14 @@ class unit_tests: assert len(s_lines) == nSubseeds + 4, s a = seed.subseed('2L').sid - b = [e for e in s_lines if ' 2L:' in e][0].strip().split()[1] + b = next(e for e in s_lines if ' 2L:' in e).strip().split()[1] assert a == b, b c = seed.subseed('2').sid assert c == a, c a = seed.subseed('5S').sid - b = [e for e in s_lines if ' 5S:' in e][0].strip().split()[3] + b = next(e for e in s_lines if ' 5S:' in e).strip().split()[3] assert a == b, b s = seed.subseeds.format(nSubseeds+1, nSubseeds+2) @@ -79,7 +79,7 @@ class unit_tests: ss_idx = str(nSubseeds+2) + 'S' a = seed.subseed(ss_idx).sid - b = [e for e in s_lines if f' {ss_idx}:' in e][0].strip().split()[3] + b = next(e for e in s_lines if f' {ss_idx}:' in e).strip().split()[3] assert a == b, b s = seed.subseeds.format(1, 10) diff --git a/test/modtest_d/util.py b/test/modtest_d/util.py index 8eabb26f..93fb7d68 100755 --- a/test/modtest_d/util.py +++ b/test/modtest_d/util.py @@ -33,11 +33,11 @@ class unit_tests: } - col1_w = max(len(str(e)) for e in list(chks.values())[0]) + 1 + col1_w = max(len(str(e)) for e in next(iter(chks.values()))) + 1 for _name, sample in samples.items(): vmsg(cyan(f'Input: {sample}')) - for fmt in list(chks.values())[0]: + for fmt in next(iter(chks.values())): spc = '\n' if fmt in ('col', 'list') else ' ' indent = ' + ' if fmt == 'col' else '' res = fmt_list(sample, fmt=fmt, indent=indent) if fmt else fmt_list(sample, indent=indent) @@ -82,11 +82,11 @@ class unit_tests: } } - col1_w = max(len(str(e)) for e in list(chks.values())[0]) + 1 + col1_w = max(len(str(e)) for e in next(iter(chks.values()))) + 1 for _name, sample in samples.items(): vmsg(cyan(f'Input: {sample}')) - for fmt in list(chks.values())[0]: + for fmt in next(iter(chks.values())): res = fmt_dict(sample, fmt=fmt) if fmt else fmt_dict(sample) vmsg(f' {str(fmt)+":":{col1_w}} {res}') if _name in chks: diff --git a/test/objtest.py b/test/objtest.py index 18ed1913..744bdffb 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -80,7 +80,7 @@ def run_test(mod, test, arg, input_data, arg1, exc_name): del arg['arg'] else: args = [] - ret_chk = list(arg.values())[0] # assume only one key present + ret_chk = next(iter(arg.values())) # assume only one key present if 'ret' in arg: ret_chk = arg['ret'] del arg['ret']