From a75234bb9ec64fa2af8aa175f98cbba5b79a41e7 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 7 Aug 2026 09:37:46 +0000 Subject: [PATCH] ruff B023 (function definition in loop) --- mmgen/proto/btc/tx/info.py | 52 ++++++++++++++++++++------------------ pyproject.toml | 1 - test/modtest_d/swap.py | 36 +++++++++++++------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/mmgen/proto/btc/tx/info.py b/mmgen/proto/btc/tx/info.py index df97ae7a..f0c292db 100755 --- a/mmgen/proto/btc/tx/info.py +++ b/mmgen/proto/btc/tx/info.py @@ -99,33 +99,37 @@ class TxInfo(TxInfo): if have_bch and e.addr: yield '{:3} [{}]\n'.format('', e.addr.hl(vp2, color=False)) else: - col1_w = len(str(len(io))) + 1 - for n, e in enumerate(io_sorted()): + def gen(n, e): mmid_fmt = get_mmid_fmt(e, is_input) + if is_input: + yield (n+1, 'tx,vout:', f'{e.txid.hl()},{red(str(e.vout))}') + yield ('', 'address:', f'{e.addr.hl(vp1)} {mmid_fmt}') + if have_bch: + yield ('', '', f'[{e.addr.hl(vp2, color=False)}]') + else: + yield ( + n + 1, + 'address:', + (f'{e.addr.hl(vp1)} {mmid_fmt}' if e.addr else e.data.hl(add_label=True))) + if have_bch and e.addr: + yield ('', '', f'[{e.addr.hl(vp2, color=False)}]') + + if e.comment: + yield ('', 'comment:', e.comment.hl()) + + yield ('', 'amount:', f'{e.amt.hl()} {tx.dcoin}') + if is_input and blockcount: confs = e.confs + blockcount - tx.blockcount - days = int(confs // confs_per_day) - def gen(): - if is_input: - yield (n+1, 'tx,vout:', f'{e.txid.hl()},{red(str(e.vout))}') - yield ('', 'address:', f'{e.addr.hl(vp1)} {mmid_fmt}') - if have_bch: - yield ('', '', f'[{e.addr.hl(vp2, color=False)}]') - else: - yield ( - n + 1, - 'address:', - (f'{e.addr.hl(vp1)} {mmid_fmt}' if e.addr else e.data.hl(add_label=True))) - if have_bch and e.addr: - yield ('', '', f'[{e.addr.hl(vp2, color=False)}]') - if e.comment: - yield ('', 'comment:', e.comment.hl()) - yield ('', 'amount:', f'{e.amt.hl()} {tx.dcoin}') - if is_input and blockcount: - yield ('', 'confirmations:', f'{confs} (around {days} days)') - if not is_input and e.is_chg: - yield ('', 'change:', green('True')) - yield '\n'.join('{:>{w}} {:<8} {}'.format(*d, w=col1_w) for d in gen()) + '\n\n' + yield ('', 'confirmations:', f'{confs} (around {confs // confs_per_day} days)') + + if not is_input and e.is_chg: + yield ('', 'change:', green('True')) + + col1_w = len(str(len(io))) + 1 + + for n, e in enumerate(io_sorted()): + yield '\n'.join('{:>{w}} {:<8} {}'.format(*d, w=col1_w) for d in gen(n, e)) + '\n\n' tx = self.tx diff --git a/pyproject.toml b/pyproject.toml index 86a4a884..2c27f374 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,6 @@ ignore = [ "ASYNC221", # blocking method in async function (mmgen/rpc/backends/curl.py) "B006", # Do not use mutable data structures for argument defaults "B018", # Found useless expression. Either assign it to a variable or remove it. - "B023", # Function definition does not bind loop variable `confs` "B026", # Star-arg unpacking after a keyword argument is strongly discouraged "BLE001", # Do not catch blind exception: `Exception` "C401", # Unnecessary generator (rewrite as a set comprehension) diff --git a/test/modtest_d/swap.py b/test/modtest_d/swap.py index 3b059842..a29c70a6 100755 --- a/test/modtest_d/swap.py +++ b/test/modtest_d/swap.py @@ -81,6 +81,24 @@ class unit_tests: def memo(self, name, ut, desc='Swap transaction memo'): + def bad(s): + return lambda: Memo.parse(s) + + def bad10(swap_cfg): + def inner(): + coin = 'BTC' + proto = init_proto(cfg, coin, need_amt=True) + addr = make_burn_addr(proto, 'C') + asset = SwapAsset(coin, 'send') + Memo(swap_cfg, proto, asset, addr, trade_limit=None) + return inner + + def bad11(): + SwapAsset('XYZ', 'send') + + def bad12(): + SwapAsset('DOGE', 'send') + for coin, addrtype, asset_name, token in ( ('ltc', 'bech32', 'LTC', None), ('bch', 'compressed', 'BCH', None), @@ -158,22 +176,6 @@ class unit_tests: vmsg('\nTesting error handling:') - def bad(s): - return lambda: Memo.parse(s) - - def bad10(): - coin = 'BTC' - proto = init_proto(cfg, coin, need_amt=True) - addr = make_burn_addr(proto, 'C') - asset = SwapAsset(coin, 'send') - Memo(swap_cfg, proto, asset, addr, trade_limit=None) - - def bad11(): - SwapAsset('XYZ', 'send') - - def bad12(): - SwapAsset('DOGE', 'send') - ut.process_bad_data(( ('bad1', 'SwapMemoParseError', 'must contain', bad('x')), ('bad2', 'SwapMemoParseError', 'must contain', bad('y:z:x')), @@ -182,7 +184,7 @@ class unit_tests: ('bad5', 'SwapMemoParseError', 'failed to parse', bad('=:l:foobar:n')), ('bad6', 'SwapMemoParseError', 'invalid specifier', bad('=:l:foobar:x/3/0')), ('bad7', 'SwapMemoParseError', 'extra', bad('=:l:foobar:0/3/0:x')), - ('bad10', 'AssertionError', 'recv', bad10), + ('bad10', 'AssertionError', 'recv', bad10(swap_cfg)), ('bad11', 'SwapAssetError', 'unrecognized', bad11), ('bad12', 'SwapAssetError', 'unsupported', bad12), ), pfx='')