Skip to content

Commit

Permalink
Keep pycodestyle happy and enforce it
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Booth committed Apr 12, 2018
1 parent a60ec89 commit d4950fe
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ install:
- pip install pytest-asyncio
- pip install pytest-cov
- pip install python-coveralls
- pip install pycodestyle
# command to run tests
script: pytest --cov=aiorpcx
script:
- pytest --cov=aiorpcx
- pycodestyle --exclude=conf.py .
after_success: coveralls
2 changes: 1 addition & 1 deletion aiorpcx/framing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def messages(self, data):
class NewlineFramer(FramerBase):
'''A framer for a protocol where messages are separated by newlines.'''

def __init__(self, max_size=250*4000):
def __init__(self, max_size=250 * 4000):
'''max_size - an anti-DoS measure. If, after processing an incoming
message, buffered data would exceed max_size bytes, that
buffered data is dropped entirely and the framer waits for a
Expand Down
4 changes: 2 additions & 2 deletions aiorpcx/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ def __init__(self, items):
self.items = items
assert isinstance(items, list)
assert items
assert (all(isinstance(item, RPCRequest) for item in items)
or all(isinstance(item, RPCResponse) for item in items))
assert (all(isinstance(item, RPCRequest) for item in items) or
all(isinstance(item, RPCResponse) for item in items))

def requests(self):
'''An iterable of the batch items that are not notifications.
Expand Down
3 changes: 2 additions & 1 deletion aiorpcx/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def __init__(self, host=None, port=None, *, rpc_protocol=None, framer=None,

async def create_connection(self):
'''Initiate a connection.'''
self_func = lambda: self
def self_func():
return self
if self.proxy:
return await self.proxy.create_connection(
self_func, self.host, self.port, loop=self.loop, **self.kwargs)
Expand Down
4 changes: 2 additions & 2 deletions aiorpcx/socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ async def handshake(cls, socket, dst_host, dst_port, auth, loop):

# Get response
data = await cls.sock_recv(loop, socket, 5)
if (len(data) != 5 or data[0] != 5 or data[2] != 0
or data[3] not in (1, 3, 4)):
if (len(data) != 5 or data[0] != 5 or data[2] != 0 or
data[3] not in (1, 3, 4)):
raise SOCKSProtocolError(f'invalid SOCKS5 proxy response: {data}')
if data[1] != 0:
raise SOCKSFailure(cls.ERROR_CODES.get(
Expand Down
1 change: 1 addition & 0 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import aiorpcx
import logging


async def main():
async with aiorpcx.ClientSession('localhost', 8888) as session:
session.send_request('echo', ["Howdy"])
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
author_email='kyuupichan@gmail.com',
license='MIT Licence',
url='https://github.com/kyuupichan/aiorpcX',
download_url = ('https://github.com/kyuupichan/aiorpcX/archive/'
f'{version}.tar.gz'),
download_url=('https://github.com/kyuupichan/aiorpcX/archive/'
f'{version}.tar.gz'),
long_description=(
'Transport, protocol and framing-independent async RPC '
'client and server implementation. '
Expand Down
9 changes: 5 additions & 4 deletions tests/test_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ def test_batch_message_from_parts():
assert rpc.batch_message_from_parts([b'1']) == b'[1]'
assert rpc.batch_message_from_parts([b'1', b'2']) == b'[1, 2]'
# An empty part is not valid, but anyway.
assert (rpc.batch_message_from_parts([b'1', b'', b'[3]'])
== b'[1, , [3]]')
assert (rpc.batch_message_from_parts([b'1', b'', b'[3]']) ==
b'[1, , [3]]')


def test_built_in_errors():
Expand Down Expand Up @@ -561,9 +561,10 @@ def test_JSONRPCv2_and_JSONRPCLoosemessages():
(RPCBatch([
RPCRequest('foo', [], 2),
RPCRequest('bar', [2], None)
]), [
{"jsonrpc": "2.0", "method": "foo", "id": 2},
{"jsonrpc": "2.0", "method": "bar", "params": [2]}
]),
[{"jsonrpc": "2.0", "method": "foo", "id": 2},
{"jsonrpc": "2.0", "method": "bar", "params": [2]}]),
]

for rpc in [JSONRPCv2, JSONRPCLoose]:
Expand Down
8 changes: 6 additions & 2 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async def test_handlers(self, server):
@pytest.mark.asyncio
async def test_send_request(self, server):
called = set()

def on_done(task):
assert task.result() == 23
called.add(True)
Expand Down Expand Up @@ -163,14 +164,15 @@ async def test_send_batch(self, server):
assert isinstance(req1, RPCRequestOut)
assert isinstance(req2, RPCRequest)
assert isinstance(req3, RPCRequestOut)
assert await batch == False # No meaningful result of a batch
assert await batch is False # No meaningful result of a batch
assert await req1 == 'a'
assert await req3 == 'b'
assert MyServerSession.current_server.notifications == ['item']

@pytest.mark.asyncio
async def test_send_batch_timeout(self, server):
called = set()

def on_done(task):
assert isinstance(task.exception(), asyncio.TimeoutError)
called.add(True)
Expand All @@ -189,6 +191,7 @@ async def double(value):
return value * 2

called = set()

def on_done(task):
assert task.result() == 12
called.add(True)
Expand Down Expand Up @@ -276,7 +279,7 @@ async def test_resource_release(self, server):
try:
client = ClientSession('localhost', 0)
await client.connect()
except:
except Exception:
pass
assert asyncio.Task.all_tasks(loop) == tasks

Expand All @@ -290,6 +293,7 @@ async def test_resource_release(self, server):
async def test_pausing(self, server):
called = []
limit = None

def my_write(data):
called.append(data)
if len(called) == limit:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def SOCKS4_bad_first(self, a=False):
result = self.consume_SOCKS4(a)
if result:
first = random.randrange(1, 256)
self.transport.write(bytes([first])
+ self.SOCKS4_good_bytes()[1:])
self.transport.write(bytes([first]) +
self.SOCKS4_good_bytes()[1:])

def SOCKS4_fail(self, code, a=False):
result = self.consume_SOCKS4(a)
Expand Down Expand Up @@ -425,10 +425,10 @@ def test_good(self, lss5, auth, chosen_auth, addr5, split):
else:
received.append(bytes([5, 1, 0]))
if chosen_auth == 2:
received.append(bytes([1, len(auth.username)])
+ auth.username.encode()
+ bytes([len(auth.password)])
+ auth.password.encode())
received.append(bytes([1, len(auth.username)]) +
auth.username.encode() +
bytes([len(auth.password)]) +
auth.password.encode())

req = bytearray([5, 1, 0])
if isinstance(host, ipaddress.IPv4Address):
Expand Down
5 changes: 4 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ def f7(x, *args, **kwargs):
assert signature_info(f7) == SignatureInfo(1, None, ['x'], any)
assert signature_info(f8) == SignatureInfo(2, 3, [], None)


def run_briefly(loop):
async def once():
pass
gen = once()
t = loop.create_task(gen)
loop.run_until_complete(t)


def test_concurrency_constructor():
Concurrency(3)
Concurrency(max_concurrent=0)
Expand All @@ -81,6 +83,7 @@ async def concurrency_max(c):

loop = asyncio.get_event_loop()
fut = loop.create_future()

async def work():
async with c.semaphore:
q.append(None)
Expand Down Expand Up @@ -135,10 +138,10 @@ def test_task_set():
assert not tasks
loop.run_until_complete(tasks.wait())

fut = loop.create_future()
async def work():
await fut

fut = loop.create_future()
count = 3
my_tasks = []
for _ in range(count):
Expand Down

0 comments on commit d4950fe

Please sign in to comment.