From 2076c0308b9235b88efe8cad10dc4a695c3add55 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 11 Oct 2023 12:58:53 +0000 Subject: [PATCH] pylint (test suite) - various cleanups --- examples/coin-daemon-info.py | 2 +- scripts/compute-file-chksum.py | 4 +++- scripts/tx-v2-to-v3.py | 4 ++-- test/gentest.py | 8 ++++---- test/include/pexpect.py | 4 ++-- test/objattrtest.py | 4 ++-- test/objtest.py | 2 +- test/test.py | 2 -- test/test_py_d/input.py | 4 ++-- test/test_py_d/ts_cfgfile.py | 2 +- test/test_py_d/ts_ethdev.py | 2 +- test/test_py_d/ts_input.py | 2 +- test/test_py_d/ts_main.py | 4 ++-- test/test_py_d/ts_ref_3seed.py | 2 +- test/test_py_d/ts_regtest.py | 14 +++++++------- test/test_py_d/ts_wallet.py | 2 +- test/test_py_d/ts_xmr_autosign.py | 4 ++-- test/test_py_d/ts_xmrwallet.py | 6 +++--- test/tooltest.py | 5 ++--- test/tooltest2.py | 7 ++++--- test/unit_tests_d/ut_rpc.py | 5 ++--- test/unit_tests_d/ut_util.py | 4 ++-- 22 files changed, 46 insertions(+), 47 deletions(-) diff --git a/examples/coin-daemon-info.py b/examples/coin-daemon-info.py index aa9b5153..cfb640d8 100755 --- a/examples/coin-daemon-info.py +++ b/examples/coin-daemon-info.py @@ -46,7 +46,7 @@ from mmgen.util import make_timestr async def get_rpc(cfg): try: return await rpc_init( cfg, ignore_wallet=True ) - except SocketError as e: + except SocketError: return False async def main(coins): diff --git a/scripts/compute-file-chksum.py b/scripts/compute-file-chksum.py index 0f97627a..ed2144ab 100755 --- a/scripts/compute-file-chksum.py +++ b/scripts/compute-file-chksum.py @@ -26,5 +26,7 @@ start = (1,0)[bool(cfg.include_first_line)] a = make_chksum_6(' '.join(lines[start:]).encode()) if start == 1: b = lines[0] - msg(("Checksum in file ({}) doesn't match computed value!".format(b),'Checksum in file OK')[a==b]) + msg( + 'Checksum in file OK' if a == b else + f"Checksum in file ({b}) doesn't match computed value!") Msg(a) diff --git a/scripts/tx-v2-to-v3.py b/scripts/tx-v2-to-v3.py index 3c849cbb..f900fc61 100755 --- a/scripts/tx-v2-to-v3.py +++ b/scripts/tx-v2-to-v3.py @@ -15,8 +15,8 @@ sys.path = [repo_root] + sys.path opts_data = { 'text': { - 'desc': "Convert MMGen transaction file from v2 format to v3 format", - 'usage': "", + 'desc': "Convert an MMGen transaction file from v2 format to v3 format", + 'usage': "", 'options': """ -h, --help Print this help message -d, --outdir=d Output files to directory 'd' instead of working dir diff --git a/test/gentest.py b/test/gentest.py index c6c56b2a..999193c6 100755 --- a/test/gentest.py +++ b/test/gentest.py @@ -198,9 +198,9 @@ class GenToolPycoin(GenTool): super().__init__(*args,**kwargs) try: from pycoin.networks.registry import network_for_netcode - except: + except Exception as e: raise ImportError( - 'Unable to import pycoin.networks.registry. Is pycoin installed on your system?') + 'Unable to import pycoin.networks.registry. Is pycoin installed on your system?') from e self.nfnc = network_for_netcode def run(self,sec,vcoin): @@ -232,9 +232,9 @@ class GenToolMonero_python(GenTool): load_cryptodomex() try: from monero.seed import Seed - except: + except Exception as e: raise ImportError( - 'Unable to import monero-python. Is monero-python installed on your system?') + 'Unable to import monero-python. Is monero-python installed on your system?') from e self.Seed = Seed def run(self,sec,vcoin): diff --git a/test/include/pexpect.py b/test/include/pexpect.py index a7c89595..86ce428d 100755 --- a/test/include/pexpect.py +++ b/test/include/pexpect.py @@ -206,13 +206,13 @@ class MMGenPexpect: try: ret = (self.p.expect_exact,self.p.expect)[bool(regex)](s) if s else 0 - except pexpect.TIMEOUT: + except pexpect.TIMEOUT as e: if cfg.debug_pexpect: raise m1 = f'\nERROR. Expect {s!r} timed out. Exiting\n' m2 = f'before: [{self.p.before}]\n' m3 = f'sent value: [{self.sent_value}]' if self.sent_value is not None else '' - raise pexpect.TIMEOUT(m1+m2+m3) + raise pexpect.TIMEOUT(m1+m2+m3) from e if cfg.debug_pexpect: debug_pexpect_msg(self.p) diff --git a/test/objattrtest.py b/test/objattrtest.py index dfc346d5..c5cedbc8 100755 --- a/test/objattrtest.py +++ b/test/objattrtest.py @@ -92,8 +92,8 @@ def test_attr_perm(obj,attrname,perm_name,perm_value,dobj,attrval_type): elif perm_name == 'reassign_ok': try: so = sample_objs[attrval_type.__name__] - except: - raise SampleObjError(f'unable to find sample object of type {attrval_type.__name__!r}') + except Exception as e: + raise SampleObjError(f'unable to find sample object of type {attrval_type.__name__!r}') from e # ListItemAttr allows setting an attribute if its value is None if type(dobj) is ListItemAttr and getattr(obj,attrname) is None: setattr(obj,attrname,so) diff --git a/test/objtest.py b/test/objtest.py index 5c10b86f..10e46a54 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -114,7 +114,7 @@ def run_test(mod,test,arg,input_data,arg1,exc_name): else: ret = cls(*args,**kwargs) - bad_ret = list() if issubclass(cls,list) else None + bad_ret = [] if issubclass(cls,list) else None if isinstance(ret_chk,str): ret_chk = ret_chk.encode() diff --git a/test/test.py b/test/test.py index 9e9386b1..7d6a79b5 100755 --- a/test/test.py +++ b/test/test.py @@ -1051,5 +1051,3 @@ except Exception: if os.getenv('MMGEN_EXEC_WRAPPER') or not os.getenv('MMGEN_IGNORE_TEST_PY_EXCEPTION'): raise die(1,red('Test script exited with error')) -except: - raise diff --git a/test/test_py_d/input.py b/test/test_py_d/input.py index 3095b853..49919526 100755 --- a/test/test_py_d/input.py +++ b/test/test_py_d/input.py @@ -68,8 +68,8 @@ def stealth_mnemonic_entry(t,mne,mn,entry_mode,pad_entry=False): ret = t.expect((p_ok.format(wnum),p_err.format(wnum-1))) if ret == 0: wnum += 1 - for j in range(len(w)): - t.send(w[j]) + for char in w: + t.send(char) time.sleep(0.005) def user_dieroll_entry(t,data): diff --git a/test/test_py_d/ts_cfgfile.py b/test/test_py_d/ts_cfgfile.py index 3a131755..472f871a 100755 --- a/test/test_py_d/ts_cfgfile.py +++ b/test/test_py_d/ts_cfgfile.py @@ -158,7 +158,7 @@ class TestSuiteCfgFile(TestSuiteBase): write_to_file(self.path('usr'),'\n'.join(d) + '\n') return self.old_sample_common( old_set = True, - pexpect_spawn = False if gc.platform == 'win' else True ) + pexpect_spawn = not gc.platform == 'win') def _autoset_opts(self,args=[],text='rpc_backend aiohttp\n'): write_to_file( self.path('usr'), text ) diff --git a/test/test_py_d/ts_ethdev.py b/test/test_py_d/ts_ethdev.py index 8a8443e5..7cec7924 100755 --- a/test/test_py_d/ts_ethdev.py +++ b/test/test_py_d/ts_ethdev.py @@ -1373,7 +1373,7 @@ class TestSuiteEthdev(TestSuiteBase,TestSuiteShared): def edit_comment1(self): return self.edit_comment(out_num=del_addrs[0],comment_text=tw_comment_zh[:3]) def edit_comment2(self): - spawn = False if gc.platform == 'win' else True + spawn = not gc.platform == 'win' return self.edit_comment( out_num = del_addrs[0], comment_text = tw_comment_zh[3:], diff --git a/test/test_py_d/ts_input.py b/test/test_py_d/ts_input.py index 4f5dbb46..da706915 100755 --- a/test/test_py_d/ts_input.py +++ b/test/test_py_d/ts_input.py @@ -240,7 +240,7 @@ class TestSuiteInput(TestSuiteBase): def _input_func(self,func_name,arg_dfls,func_args,text,expect,term): if term and gc.platform == 'win': return ('skip_warn','pexpect_spawn not supported on Windows platform') - func_args = {k:v for k,v in zip(arg_dfls.keys(),func_args)} + func_args = dict(zip(arg_dfls.keys(),func_args)) t = self.spawn( 'test/misc/input_func.py', [func_name,repr(func_args)], diff --git a/test/test_py_d/ts_main.py b/test/test_py_d/ts_main.py index fb14445f..ac497c42 100755 --- a/test/test_py_d/ts_main.py +++ b/test/test_py_d/ts_main.py @@ -991,5 +991,5 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared): add_args = ['--vsize-adj=1.08'], tweaks = ['confirm_non_mmgen'] ) - def txsign6(self,txf,wf): - return self.txsign5(txf,wf,bad_vsize=False,add_args=['--vsize-adj=1.08']) + def txsign6(self,wf,txf): + return self.txsign5(wf,txf,bad_vsize=False,add_args=['--vsize-adj=1.08']) diff --git a/test/test_py_d/ts_ref_3seed.py b/test/test_py_d/ts_ref_3seed.py index 910e88ad..c48d153e 100755 --- a/test/test_py_d/ts_ref_3seed.py +++ b/test/test_py_d/ts_ref_3seed.py @@ -366,7 +366,7 @@ class TestSuiteRef3Addr(TestSuiteRef3Seed): ('ref_bip39_12_passwdgen', ([],'new refwallet passwd file chksum (BIP39, 12 words)')), ('ref_bip39_18_passwdgen', ([],'new refwallet passwd file chksum (BIP39, up to 18 words)')), ('ref_bip39_24_passwdgen', ([],'new refwallet passwd file chksum (BIP39, up to 24 words)')), - ('ref_xmrseed_25_passwdgen', ([],'new refwallet passwd file chksum (Monero new-style mnemonic, 25 words)')), + ('ref_xmrseed_25_passwdgen', ([],'new refwallet passwd file chksum (Monero 25-word mnemonic)')), ('ref_hex2bip39_24_passwdgen',([],'new refwallet passwd file chksum (hex-to-BIP39, up to 24 words)')), ) diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index 9ddc208c..93bb991d 100755 --- a/test/test_py_d/ts_regtest.py +++ b/test/test_py_d/ts_regtest.py @@ -850,7 +850,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): t.expect('draw:\b','q',regex=True) else: txnum,idx = (8,1) if self.proto.coin == 'BCH' else (9,3) - t.expect(f'\s{txnum}\).*Inputs:.*:L:{idx}.*Outputs \(3\):.*:C:2.*\s10\)','q',regex=True) + t.expect(rf'\s{txnum}\).*Inputs:.*:L:{idx}.*Outputs \(3\):.*:C:2.*\s10\)','q',regex=True) return t def bob_getbalance(self,bals,confs=1): @@ -1100,7 +1100,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): new_txid = self.read_from_tmpfile('rbf_txid2').strip() return self.bob_rbf_status(rtFee[1], 'Replacement transaction has 1 confirmation', - f'Replacing transactions:\s+{new_txid}' ) + rf'Replacing transactions:\s+{new_txid}' ) def bob_rbf_status5(self): if not self.proto.cap('rbf'): @@ -1113,7 +1113,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): new_txid = self.read_from_tmpfile('rbf_txid2').strip() return self.bob_rbf_status(rtFee[1], 'Replacement transaction has 2 confirmations', - f'Replacing transactions:\s+{new_txid}' ) + rf'Replacing transactions:\s+{new_txid}' ) def _gen_pairs(self,n): from mmgen.tool.api import tool_api @@ -1606,10 +1606,10 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): def alice_txcreate_info(self,pexpect_spawn=False): t = self.spawn('mmgen-txcreate',['--alice','-Bi'],pexpect_spawn=pexpect_spawn) pats = ( - ( '\d+', 'w'), - ( '\d+', 'D'), - ( '\d+', 'D'), - ( '\d+', 'D'), + ( r'\d+', 'w'), + ( r'\d+', 'D'), + ( r'\d+', 'D'), + ( r'\d+', 'D'), ( pat_date, 'q'), ) for d,s in pats: diff --git a/test/test_py_d/ts_wallet.py b/test/test_py_d/ts_wallet.py index 68421021..432c5415 100755 --- a/test/test_py_d/ts_wallet.py +++ b/test/test_py_d/ts_wallet.py @@ -226,7 +226,7 @@ class TestSuiteWalletConv(TestSuiteBase,TestSuiteShared): t.passphrase_new('new '+wcls.desc,self.wpasswd) t.usr_rand(self.usr_rand_chars) if wcls.type.startswith('incog'): - for _ in (1,2,3): + for _ in range(3): t.expect('Encrypting random data from your operating system with ephemeral key') if wcls.type == 'incog_hidden': t.hincog_create(hincog_bytes) diff --git a/test/test_py_d/ts_xmr_autosign.py b/test/test_py_d/ts_xmr_autosign.py index a35c436f..19c0d515 100755 --- a/test/test_py_d/ts_xmr_autosign.py +++ b/test/test_py_d/ts_xmr_autosign.py @@ -168,13 +168,13 @@ class TestSuiteXMRAutosign(TestSuiteXMRWallet,TestSuiteAutosignBase): return self._new_addr_alice( '2:1', 'continue', - fr'Account index:\s+1\s+Creating new address' ) + r'Account index:\s+1\s+Creating new address' ) def new_address_alice_label(self): return self._new_addr_alice( '2:1,Alice’s new address', 'stop', - fr'Account index:\s+1\s+Creating new address.*Alice’s new address' ) + r'Account index:\s+1\s+Creating new address.*Alice’s new address' ) def dump_tmp_wallets(self): return self._dump_wallets(autosign=False) diff --git a/test/test_py_d/ts_xmrwallet.py b/test/test_py_d/ts_xmrwallet.py index 0d96c6ec..771db2a4 100755 --- a/test/test_py_d/ts_xmrwallet.py +++ b/test/test_py_d/ts_xmrwallet.py @@ -423,13 +423,13 @@ class TestSuiteXMRWallet(TestSuiteBase): return self.new_addr_alice( '4:2', 'continue', - fr'Account index:\s+2\s+Creating new address' ) + r'Account index:\s+2\s+Creating new address' ) def new_address_alice_label(self): return self.new_addr_alice( '4:2,Alice’s new address', 'stop', - fr'Account index:\s+2\s+Creating new address.*Alice’s new address' ) + r'Account index:\s+2\s+Creating new address.*Alice’s new address' ) async def mine_initial_coins(self): self.spawn('', msg_only=True, extra_desc='(opening wallet)') @@ -510,7 +510,7 @@ class TestSuiteXMRWallet(TestSuiteBase): t.expect('Wallet height: ') res = strip_ansi_escapes(t.expect_getend('Balance: ')) if bal_chk_func: - m = re.match( '(\S+) Unlocked balance: (\S+)', res, re.DOTALL ) + m = re.match( r'(\S+) Unlocked balance: (\S+)', res, re.DOTALL ) amts = [XMRAmt(amt) for amt in m.groups()] assert bal_chk_func(n,*amts), f'balance check for wallet {n} failed!' return t diff --git a/test/tooltest.py b/test/tooltest.py index e77f4a79..065d4ff3 100755 --- a/test/tooltest.py +++ b/test/tooltest.py @@ -305,9 +305,8 @@ class MMGenToolTestUtils: return if Return: return ret - else: - if not hush: - ok() + elif not hush: + ok() else: die(4,f'Error for command {name!r}') diff --git a/test/tooltest2.py b/test/tooltest2.py index 1ab24d1a..b2d51c74 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -779,9 +779,10 @@ def fork_cmd(cmd_name,args,opts,stdin_input): return cmd_out.strip() async def call_method(cls,method,cmd_name,args,mmtype,stdin_input): - vmsg('{}: {}{}'.format(purple('Running'), - ' '.join([cmd_name]+[repr(e) for e in args]), - ' '+mmtype if mmtype else '' )) + vmsg('{a}: {b}{c}'.format( + a = purple('Running'), + b = ' '.join([cmd_name]+[repr(e) for e in args]), + c = ' '+mmtype if mmtype else '' )) aargs,kwargs = main_tool.process_args(cmd_name,args,cls) oq_save = bool(cfg.quiet) if not cfg.verbose: diff --git a/test/unit_tests_d/ut_rpc.py b/test/unit_tests_d/ut_rpc.py index b6962db0..e9096e03 100755 --- a/test/unit_tests_d/ut_rpc.py +++ b/test/unit_tests_d/ut_rpc.py @@ -133,9 +133,8 @@ def run_test(network_ids,test_cf_auth=False,daemon_ids=None): for network_id in network_ids: proto = init_proto( cfg, network_id=network_id ) - ids = (lambda x: - set(daemon_ids) & set(x) if daemon_ids else x - )(CoinDaemon.get_daemon_ids(cfg,proto.coin)) + all_ids = CoinDaemon.get_daemon_ids(cfg,proto.coin) + ids = set(daemon_ids) & set(all_ids) if daemon_ids else all_ids for daemon_id in ids: do_test( CoinDaemon(cfg, proto=proto,test_suite=True,daemon_id=daemon_id) ) diff --git a/test/unit_tests_d/ut_util.py b/test/unit_tests_d/ut_util.py index 85ec0ca9..0b5e0d34 100755 --- a/test/unit_tests_d/ut_util.py +++ b/test/unit_tests_d/ut_util.py @@ -37,7 +37,7 @@ class unit_tests: for name,sample in samples.items(): vmsg(cyan(f'Input: {sample}')) - for fmt,chk in list(chks.values())[0].items(): + for fmt in list(chks.values())[0]: 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) @@ -86,7 +86,7 @@ class unit_tests: for name,sample in samples.items(): vmsg(cyan(f'Input: {sample}')) - for fmt,chk in list(chks.values())[0].items(): + for fmt in list(chks.values())[0]: res = fmt_dict(sample,fmt=fmt) if fmt else fmt_dict(sample) vmsg(f' {str(fmt)+":":{col1_w}} {res}') if name in chks: