From 8924016bace93869427837c958da6b81be563383 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 4 Mar 2024 10:30:36 +0000 Subject: [PATCH] tx.__init__: call/return cleanups --- mmgen/tx/__init__.py | 25 ++++++++++++------------- pyproject.toml | 3 ++- test/unit_tests_d/ut_tx.py | 14 +++++++++++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mmgen/tx/__init__.py b/mmgen/tx/__init__.py index 62e351aa..e33dd148 100755 --- a/mmgen/tx/__init__.py +++ b/mmgen/tx/__init__.py @@ -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') diff --git a/pyproject.toml b/pyproject.toml index 6f894fab..6249af1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/test/unit_tests_d/ut_tx.py b/test/unit_tests_d/ut_tx.py index 75c0806d..5d7ced09 100755 --- a/test/unit_tests_d/ut_tx.py +++ b/test/unit_tests_d/ut_tx.py @@ -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