tx.__init__: call/return cleanups

This commit is contained in:
The MMGen Project 2024-03-04 10:30:36 +00:00
commit 8924016bac
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 27 additions and 15 deletions

View file

@ -23,9 +23,7 @@ def _base_proto_subclass(clsname,modname,proto):
import importlib
return getattr( importlib.import_module(modname), clsname )
def _get_cls_info(clsname,modname,args,kwargs):
assert args == (), f'{clsname}.chk1: only keyword args allowed in {clsname} initializer'
def _get_cls_info(clsname, modname, kwargs):
if 'proto' in kwargs:
proto = kwargs['proto']
@ -57,20 +55,21 @@ def _get_cls_info(clsname,modname,args,kwargs):
clsname = 'Automount' + clsname
del kwargs['automount']
return ( kwargs['cfg'], proto, clsname, modname, kwargs )
return (clsname, modname, kwargs)
def _get_obj( _clsname, _modname, *args, **kwargs ):
def _get_obj(_clsname, _modname, **kwargs):
"""
determine cls/mod/proto and pass them to _base_proto_subclass() to get a transaction instance
"""
cfg,proto,clsname,modname,kwargs = _get_cls_info(_clsname,_modname,args,kwargs)
clsname, modname, kwargs = _get_cls_info(_clsname, _modname, kwargs)
return _base_proto_subclass( clsname, modname, proto )(*args,**kwargs)
return _base_proto_subclass(clsname, modname, kwargs['proto'])(**kwargs)
async def _get_obj_async( _clsname, _modname, *args, **kwargs ):
async def _get_obj_async(_clsname, _modname, **kwargs):
cfg,proto,clsname,modname,kwargs = _get_cls_info(_clsname,_modname,args,kwargs)
clsname, modname, kwargs = _get_cls_info(_clsname, _modname, kwargs)
proto = kwargs['proto']
# NB: tracking wallet needed to retrieve the 'symbol' and 'decimals' parameters of token addr
# (see twctl:import_token()).
@ -83,15 +82,15 @@ async def _get_obj_async( _clsname, _modname, *args, **kwargs ):
'Sent',
'AutomountSent'):
from ..tw.ctl import TwCtl
kwargs['twctl'] = await TwCtl(cfg,proto)
kwargs['twctl'] = await TwCtl(kwargs['cfg'], proto)
return _base_proto_subclass( clsname, modname, proto )(*args,**kwargs)
return _base_proto_subclass(clsname, modname, proto)(**kwargs)
def _get(clsname,modname):
return lambda *args,**kwargs: _get_obj(clsname,modname,*args,**kwargs)
return lambda **kwargs: _get_obj(clsname, modname, **kwargs)
def _get_async(clsname,modname):
return lambda *args,**kwargs: _get_obj_async(clsname,modname,*args,**kwargs)
return lambda **kwargs: _get_obj_async(clsname, modname, **kwargs)
BaseTX = _get('Base', 'base')
UnsignedTX = _get('Unsigned', 'unsigned')

View file

@ -20,10 +20,10 @@ ignore = [
"pyethereum",
"rlp",
"main_split.py",
"ut_tx.py",
]
ignored-modules = [ # ignored for no-member, otherwise checked
"mmgen.proto.secp256k1.secp256k1",
"mmgen.autosign", # tx_dir, etc. created dynamically
"mmgen.term",
"msvcrt",
"gmpy2",
@ -65,6 +65,7 @@ ignored-classes = [ # ignored for no-member, otherwise checked
# mmgen:
"baseconv",
"mmgen.autosign.Signable.base",
"mmgen.autosign.Autosign", # tx_dir, etc. created dynamically
"Sha2",
"mmgen.xmrwallet.MoneroMMGenTX.Base",
"mmgen.xmrwallet.MoneroWalletOutputsFile.Base",

View file

@ -7,7 +7,7 @@ test.unit_tests_d.ut_tx: TX unit tests for the MMGen suite
import os,re
from mmgen.devtools import get_diff,get_ndiff
from mmgen.tx import NewTX,CompletedTX
from mmgen.tx import NewTX,CompletedTX,UnsignedTX
from mmgen.tx.file import MMGenTxFile
from mmgen.daemon import CoinDaemon
from mmgen.protocol import init_proto
@ -94,3 +94,15 @@ class unit_tests:
'litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
)
)
def errors(self,name,ut):
async def bad1():
await CompletedTX(cfg, filename='foo')
def bad2():
UnsignedTX(cfg, filename='foo')
bad_data = (
('forbidden positional args', 'TypeError', 'positional arguments', bad1),
('forbidden positional args', 'TypeError', 'positional arguments', bad2),
)
ut.process_bad_data(bad_data)
return True