pylint and mypy fixes

This commit is contained in:
Lex Berezhny 2018-11-03 19:45:28 -04:00
parent f60435c878
commit 48d7bfd225
5 changed files with 60 additions and 55 deletions

View file

@ -6,17 +6,18 @@ source =
torba
.tox/*/lib/python*/site-packages/torba
[mypy-twisted.*,cryptography.*,coincurve.*,pbkdf2]
[cryptography.*,coincurve.*,pbkdf2]
ignore_missing_imports = True
[pylint]
ignore=words,server
ignore=words,server,workbench
max-args=10
max-line-length=110
good-names=T,t,n,i,j,k,x,y,s,f,d,h,c,e,op,db,tx,io,cachedproperty,log,id
valid-metaclass-classmethod-first-arg=mcs
disable=
fixme,
broad-except,
no-else-return,
cyclic-import,
missing-docstring,

View file

@ -13,9 +13,9 @@ def get_argument_parser():
)
subparsers = parser.add_subparsers(dest='command', help='sub-command help')
gui = subparsers.add_parser("gui", help="Start Qt GUI.")
subparsers.add_parser("gui", help="Start Qt GUI.")
download = subparsers.add_parser("download", help="Download blockchain node binary.")
subparsers.add_parser("download", help="Download blockchain node binary.")
start = subparsers.add_parser("start", help="Start orchstr8 service.")
start.add_argument("--blockchain", help="Start blockchain node.", action="store_true")
@ -25,7 +25,7 @@ def get_argument_parser():
generate = subparsers.add_parser("generate", help="Call generate method on running orchstr8 instance.")
generate.add_argument("blocks", type=int, help="Number of blocks to generate")
transfer = subparsers.add_parser("transfer", help="Call transfer method on running orchstr8 instance.")
subparsers.add_parser("transfer", help="Call transfer method on running orchstr8 instance.")
return parser

View file

@ -1,5 +1,4 @@
import os
import sys
import shutil
import asyncio
import zipfile
@ -8,9 +7,10 @@ import logging
import tempfile
import subprocess
import importlib
import requests
from binascii import hexlify
from typing import Type
from typing import Type, Optional
import requests
from torba.server.server import Server
from torba.server.env import Env
@ -19,12 +19,6 @@ from torba.baseledger import BaseLedger, BlockHeightEvent
from torba.basemanager import BaseWalletManager
from torba.baseaccount import BaseAccount
root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
def get_manager_from_environment(default_manager=BaseWalletManager):
if 'TORBA_MANAGER' not in os.environ:
@ -122,17 +116,17 @@ class WalletNode:
self.manager_class = manager_class
self.ledger_class = ledger_class
self.verbose = verbose
self.manager: BaseWalletManager = None
self.ledger: BaseLedger = None
self.wallet: Wallet = None
self.account: BaseAccount = None
self.data_path: str = None
self.manager: Optional[BaseWalletManager] = None
self.ledger: Optional[BaseLedger] = None
self.wallet: Optional[Wallet] = None
self.account: Optional[BaseAccount] = None
self.data_path: Optional[str] = None
async def start(self):
self.data_path = tempfile.mkdtemp()
wallet_file_name = os.path.join(self.data_path, 'my_wallet.json')
with open(wallet_file_name, 'w') as wf:
wf.write('{"version": 1, "accounts": []}\n')
with open(wallet_file_name, 'w') as wallet_file:
wallet_file.write('{"version": 1, "accounts": []}\n')
self.manager = self.manager_class.from_config({
'ledgers': {
self.ledger_class.get_id(): {
@ -164,6 +158,7 @@ class SPVNode:
self.coin_class = coin_class
self.controller = None
self.data_path = None
self.server = None
async def start(self):
self.data_path = tempfile.mkdtemp()
@ -253,15 +248,15 @@ class BlockchainNode:
if not os.path.exists(downloaded_file):
self.log.info('Downloading: %s', self.latest_release_url)
r = requests.get(self.latest_release_url, stream=True)
response = requests.get(self.latest_release_url, stream=True)
with open(downloaded_file, 'wb') as f:
shutil.copyfileobj(r.raw, f)
shutil.copyfileobj(response.raw, f)
self.log.info('Extracting: %s', downloaded_file)
if downloaded_file.endswith('.zip'):
with zipfile.ZipFile(downloaded_file) as zf:
zf.extractall(self.bin_dir)
with zipfile.ZipFile(downloaded_file) as dotzip:
dotzip.extractall(self.bin_dir)
# zipfile bug https://bugs.python.org/issue15795
os.chmod(self.cli_bin, 0o755)
os.chmod(self.daemon_bin, 0o755)
@ -314,7 +309,7 @@ class BlockchainNode:
process = await asyncio.create_subprocess_exec(
*cmnd_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
out, err = await process.communicate()
out, _ = await process.communicate()
self.log.info(out.decode().strip())
return out.decode().strip()
@ -322,8 +317,8 @@ class BlockchainNode:
self._block_expected += blocks
return self._cli_cmnd('generate', str(blocks))
def invalidateblock(self, hash):
return self._cli_cmnd('invalidateblock', hash)
def invalidateblock(self, blockhash):
return self._cli_cmnd('invalidateblock', blockhash)
def get_raw_change_address(self):
return self._cli_cmnd('getrawchangeaddress')
@ -331,8 +326,8 @@ class BlockchainNode:
async def get_balance(self):
return float(await self._cli_cmnd('getbalance'))
def send_to_address(self, address, credits):
return self._cli_cmnd('sendtoaddress', address, str(credits))
def send_to_address(self, address, amount):
return self._cli_cmnd('sendtoaddress', address, str(amount))
def send_raw_transaction(self, tx):
return self._cli_cmnd('sendrawtransaction', tx.decode())

View file

@ -27,7 +27,7 @@ class WebSocketLogHandler(logging.Handler):
class TestingServiceAPI:
def __init__(self, stack: Conductor, loop: asyncio.AbstractEventLoop):
def __init__(self, stack: Conductor, loop: asyncio.AbstractEventLoop) -> None:
self.stack = stack
self.loop = loop
self.app = Application()
@ -112,25 +112,25 @@ class TestingServiceAPI:
})
async def log(self, request):
ws = WebSocketResponse()
await ws.prepare(request)
self.app['websockets'].add(ws)
web_socket = WebSocketResponse()
await web_socket.prepare(request)
self.app['websockets'].add(web_socket)
try:
async for msg in ws:
async for msg in web_socket:
if msg.type == WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
await web_socket.close()
elif msg.type == WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
print('web socket connection closed with exception %s' %
web_socket.exception())
finally:
self.app['websockets'].remove(ws)
return ws
self.app['websockets'].remove(web_socket)
return web_socket
@staticmethod
async def on_shutdown(app):
for ws in app['websockets']:
await ws.close(code=WSCloseCode.GOING_AWAY, message='Server shutdown')
for web_socket in app['websockets']:
await web_socket.close(code=WSCloseCode.GOING_AWAY, message='Server shutdown')
async def on_status(self, _):
if not self.app['websockets']:
@ -143,5 +143,5 @@ class TestingServiceAPI:
})
def send_message(self, msg):
for ws in self.app['websockets']:
asyncio.ensure_future(ws.send_json(msg))
for web_socket in self.app['websockets']:
asyncio.ensure_future(web_socket.send_json(msg))

View file

@ -1,12 +1,12 @@
import asyncio
import unittest
import logging
import unittest
from unittest.case import _Outcome
from .node import Conductor
try:
from asyncio.runners import _cancel_all_tasks
from asyncio.runners import _cancel_all_tasks # pylint: disable=C0412
except ImportError:
# this is only available in py3.7
def _cancel_all_tasks(loop):
@ -17,26 +17,26 @@ class AsyncioTestCase(unittest.TestCase):
# Implementation inspired by discussion:
# https://bugs.python.org/issue32972
async def asyncSetUp(self):
async def asyncSetUp(self): # pylint: disable=C0103
pass
async def asyncTearDown(self):
async def asyncTearDown(self): # pylint: disable=C0103
pass
async def doAsyncCleanups(self):
async def doAsyncCleanups(self): # pylint: disable=C0103
pass
def run(self, result=None):
def run(self, result=None): # pylint: disable=R0915
orig_result = result
if result is None:
result = self.defaultTestResult()
startTestRun = getattr(result, 'startTestRun', None)
startTestRun = getattr(result, 'startTestRun', None) # pylint: disable=C0103
if startTestRun is not None:
startTestRun()
result.startTest(self)
testMethod = getattr(self, self._testMethodName)
testMethod = getattr(self, self._testMethodName) # pylint: disable=C0103
if (getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False)):
# If the class or method was skipped.
@ -99,9 +99,9 @@ class AsyncioTestCase(unittest.TestCase):
finally:
result.stopTest(self)
if orig_result is None:
stopTestRun = getattr(result, 'stopTestRun', None)
stopTestRun = getattr(result, 'stopTestRun', None) # pylint: disable=C0103
if stopTestRun is not None:
stopTestRun()
stopTestRun() # pylint: disable=E1102
# explicitly break reference cycles:
# outcome.errors -> frame -> outcome -> outcome.errors
@ -119,6 +119,15 @@ class IntegrationTestCase(AsyncioTestCase):
MANAGER = None
VERBOSITY = logging.WARNING
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conductor = None
self.blockchain = None
self.manager = None
self.ledger = None
self.wallet = None
self.account = None
async def asyncSetUp(self):
self.conductor = Conductor(
ledger_module=self.LEDGER, manager_module=self.MANAGER, verbosity=self.VERBOSITY
@ -162,7 +171,7 @@ class IntegrationTestCase(AsyncioTestCase):
async def on_transaction(self, tx):
addresses = await self.get_tx_addresses(tx, self.ledger)
await asyncio.wait([
self.ledger.on_transaction.where(lambda e: e.address == address)
self.ledger.on_transaction.where(lambda e: e.address == address) # pylint: disable=W0640
for address in addresses
])