Fixing flake8 warnings and errors

flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude benchmark

Excluding the benchmark directory (due to protobuf generated files)
Also removing some Python2 specific code
This commit is contained in:
Jacob Alexander
2019-09-26 22:18:28 -07:00
parent 1f0200af9c
commit b3021e4f6b
37 changed files with 536 additions and 452 deletions

View File

@@ -1,8 +1,7 @@
import pytest
import capnp
import os
import time
import capnp
import test_capability_capnp as capability
class Server(capability.TestInterface.Server):
@@ -160,6 +159,7 @@ class BadPipelineServer(capability.TestPipeline.Server):
_results = _context.results
_results.s = response.x + '_foo'
_results.outBox.cap = Server(100)
def _error(error):
raise Exception('test was a success')
@@ -186,7 +186,7 @@ def test_pipeline_exception():
pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception):
loop.wait(pipelinePromise)
pipelinePromise.wait()
with pytest.raises(Exception):
remote.wait()
@@ -194,7 +194,7 @@ def test_pipeline_exception():
def test_casting():
client = capability.TestExtends._new_client(Server())
client2 = client.upcast(capability.TestInterface)
client3 = client2.cast_as(capability.TestInterface)
_ = client2.cast_as(capability.TestInterface)
with pytest.raises(Exception):
client.upcast(capability.TestPipeline)

View File

@@ -1,12 +1,13 @@
import pytest
import capnp
import os
import pytest
import capnp
this_dir = os.path.dirname(__file__)
@pytest.fixture
def capability():
return capnp.load(os.path.join(this_dir, 'test_capability.capnp'))
return capnp.load(os.path.join(this_dir, 'test_capability.capnp'))
class Server:
def __init__(self, val=1):
@@ -150,6 +151,7 @@ class BadPipelineServer:
def _then(response):
context.results.s = response.x + '_foo'
context.results.outBox.cap = capability().TestInterface._new_server(Server(100))
def _error(error):
raise Exception('test was a success')
@@ -176,7 +178,7 @@ def test_pipeline_exception_context(capability):
pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception):
loop.wait(pipelinePromise)
pipelinePromise.wait()
with pytest.raises(Exception):
remote.wait()
@@ -184,7 +186,7 @@ def test_pipeline_exception_context(capability):
def test_casting_context(capability):
client = capability.TestExtends._new_client(Server())
client2 = client.upcast(capability.TestInterface)
client3 = client2.cast_as(capability.TestInterface)
_ = client2.cast_as(capability.TestInterface)
with pytest.raises(Exception):
client.upcast(capability.TestPipeline)

View File

@@ -1,12 +1,13 @@
import pytest
import capnp
import os
import pytest
import capnp
this_dir = os.path.dirname(__file__)
@pytest.fixture
def capability():
return capnp.load(os.path.join(this_dir, 'test_capability.capnp'))
return capnp.load(os.path.join(this_dir, 'test_capability.capnp'))
class Server:
def __init__(self, val=1):
@@ -154,6 +155,7 @@ class BadPipelineServer:
_results = _context.results
_results.s = response.x + '_foo'
_results.outBox.cap = capability().TestInterface._new_server(Server(100))
def _error(error):
raise Exception('test was a success')
@@ -180,7 +182,7 @@ def test_pipeline_exception(capability):
pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception):
loop.wait(pipelinePromise)
pipelinePromise.wait()
with pytest.raises(Exception):
remote.wait()
@@ -188,7 +190,7 @@ def test_pipeline_exception(capability):
def test_casting(capability):
client = capability.TestExtends._new_client(Server())
client2 = client.upcast(capability.TestInterface)
client3 = client2.cast_as(capability.TestInterface)
_ = client2.cast_as(capability.TestInterface)
with pytest.raises(Exception):
client.upcast(capability.TestPipeline)

View File

@@ -1,9 +1,10 @@
import pytest
import platform
import capnp
import os
import platform
import tempfile
import sys
import pytest
import capnp
this_dir = os.path.dirname(__file__)
@@ -49,7 +50,7 @@ def get_two_adjacent_messages(test_capnp):
msg2 = test_capnp.Msg.new_message()
m2 = msg2.to_bytes()
return m1 + m2
return m1 + m2
def test_large_read_multiple_bytes(test_capnp):
data = get_two_adjacent_messages(test_capnp)
@@ -81,19 +82,3 @@ def test_large_read_mutltiple_bytes_memoryview(test_capnp):
data = get_two_adjacent_messages(test_capnp) + b' '
for m in test_capnp.Msg.read_multiple_bytes(memoryview(data)):
pass
@pytest.mark.skipif(sys.version_info[0] == 3, reason="Legacy buffer support only for python 2.7")
def test_large_read_mutltiple_bytes_buffer(test_capnp):
data = get_two_adjacent_messages(test_capnp)
for m in test_capnp.Msg.read_multiple_bytes(buffer(data)):
pass
with pytest.raises(capnp.KjException):
data = get_two_adjacent_messages(test_capnp)[:-1]
for m in test_capnp.Msg.read_multiple_bytes(buffer(data)):
pass
with pytest.raises(capnp.KjException):
data = get_two_adjacent_messages(test_capnp) + b' '
for m in test_capnp.Msg.read_multiple_bytes(buffer(data)):
pass

View File

@@ -7,15 +7,15 @@ this_dir = os.path.dirname(__file__)
@pytest.fixture
def addressbook():
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
@pytest.fixture
def foo():
return capnp.load(os.path.join(this_dir, 'foo.capnp'))
return capnp.load(os.path.join(this_dir, 'foo.capnp'))
@pytest.fixture
def bar():
return capnp.load(os.path.join(this_dir, 'bar.capnp'))
return capnp.load(os.path.join(this_dir, 'bar.capnp'))
def test_basic_load():
capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
@@ -56,13 +56,13 @@ def test_failed_import():
bar.foo = foo
def test_defualt_import_hook():
import addressbook_capnp
import addressbook_capnp # noqa: F401
def test_dash_import():
import addressbook_with_dashes_capnp
import addressbook_with_dashes_capnp # noqa: F401
def test_spaces_import():
import addressbook_with_spaces_capnp
import addressbook_with_spaces_capnp # noqa: F401
def test_add_import_hook():
capnp.add_import_hook([this_dir])
@@ -86,4 +86,4 @@ def test_remove_import_hook():
del sys.modules['addressbook_capnp'] # hack to deal with it being imported already
with pytest.raises(ImportError):
import addressbook_capnp
import addressbook_capnp # noqa: F401

View File

@@ -7,7 +7,7 @@ this_dir = os.path.dirname(__file__)
@pytest.fixture
def addressbook():
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
def test_object_basic(addressbook):

View File

@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import pytest
import capnp
@@ -16,7 +16,7 @@ else:
@pytest.fixture
def addressbook():
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
def test_addressbook_message_classes(addressbook):
def writeAddressBook(fd):
@@ -71,7 +71,7 @@ def test_addressbook_message_classes(addressbook):
assert bobPhones[0].type == 'home'
assert bobPhones[1].number == "555-7654"
assert bobPhones[1].type == 'work'
assert bob.employment.unemployed == None
assert bob.employment.unemployed is None
f = open('example', 'w')
writeAddressBook(f.fileno())
@@ -130,7 +130,7 @@ def test_addressbook(addressbook):
assert bobPhones[0].type == 'home'
assert bobPhones[1].number == "555-7654"
assert bobPhones[1].type == 'work'
assert bob.employment.unemployed == None
assert bob.employment.unemployed is None
f = open('example', 'w')
@@ -192,7 +192,7 @@ def test_addressbook_resizable(addressbook):
assert bobPhones[0].type == 'home'
assert bobPhones[1].number == "555-7654"
assert bobPhones[1].type == 'work'
assert bob.employment.unemployed == None
assert bob.employment.unemployed is None
f = open('example', 'w')
@@ -262,7 +262,7 @@ def test_addressbook_explicit_fields(addressbook):
assert bobPhones[1]._get_by_field(phone_fields['number']) == "555-7654"
assert bobPhones[1]._get_by_field(phone_fields['type']) == 'work'
employment = bob._get_by_field(person_fields['employment'])
employment._get_by_field(addressbook.Person.Employment.schema.fields['unemployed']) == None
employment._get_by_field(addressbook.Person.Employment.schema.fields['unemployed']) is None
f = open('example', 'w')
@@ -371,8 +371,8 @@ def check_list(reader, expected):
assert reader[i] == v
def check_all_types(reader):
assert reader.voidField == None
assert reader.boolField == True
assert reader.voidField is None
assert reader.boolField
assert reader.int8Field == -123
assert reader.int16Field == -12345
assert reader.int32Field == -12345678
@@ -387,8 +387,8 @@ def check_all_types(reader):
assert reader.dataField == b"bar"
subReader = reader.structField
assert subReader.voidField == None
assert subReader.boolField == True
assert subReader.voidField is None
assert subReader.boolField
assert subReader.int8Field == -12
assert subReader.int16Field == 3456
assert subReader.int32Field == -78901234
@@ -495,7 +495,7 @@ def test_build_first_segment_size(all_types):
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
assert str(root) + '\n' == expectedText
root = all_types.TestAllTypes.new_message(1024*1024)
root = all_types.TestAllTypes.new_message(1024 * 1024)
init_all_types(root)
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
assert str(root) + '\n' == expectedText

View File

@@ -1,8 +1,3 @@
import pytest
import capnp
import os
import time
import test_response_capnp
class FooServer(test_response_capnp.Foo.Server):

View File

@@ -1,6 +1,5 @@
import pytest
import capnp
import os
import socket
import test_capability_capnp
@@ -30,7 +29,7 @@ def test_simple_rpc():
read, write = socket.socketpair(socket.AF_UNIX)
restorer = SimpleRestorer()
server = capnp.TwoPartyServer(write, restorer)
_ = capnp.TwoPartyServer(write, restorer)
client = capnp.TwoPartyClient(read)
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
@@ -47,7 +46,7 @@ def test_simple_rpc_with_options():
read, write = socket.socketpair(socket.AF_UNIX)
restorer = SimpleRestorer()
server = capnp.TwoPartyServer(write, restorer)
_ = capnp.TwoPartyServer(write, restorer)
# This traversal limit is too low to receive the response in, so we expect
# an exception during the call.
client = capnp.TwoPartyClient(read, traversal_limit_in_words=1)
@@ -58,13 +57,13 @@ def test_simple_rpc_with_options():
remote = cap.foo(i=5)
with pytest.raises(capnp.KjException):
response = remote.wait()
_ = remote.wait()
def test_simple_rpc_restore_func():
read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, restore_func)
_ = capnp.TwoPartyServer(write, restore_func)
client = capnp.TwoPartyClient(read)
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
@@ -86,7 +85,7 @@ def text_restore_func(objectId):
def test_ez_rpc():
read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, text_restore_func)
_ = capnp.TwoPartyServer(write, text_restore_func)
client = capnp.TwoPartyClient(read)
cap = client.ez_restore('testInterface')
@@ -108,7 +107,7 @@ def test_ez_rpc():
def test_simple_rpc_bootstrap():
read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, bootstrap=Server(100))
_ = capnp.TwoPartyServer(write, bootstrap=Server(100))
client = capnp.TwoPartyClient(read)
cap = client.bootstrap()

View File

@@ -1,21 +1,23 @@
import capnp
import gc
import os
import socket
import gc
import subprocess
import sys # add examples dir to sys.path
import time
import sys # add examples dir to sys.path
import capnp
examples_dir = os.path.join(os.path.dirname(__file__), '..', 'examples')
sys.path.append(examples_dir)
import calculator_client
import calculator_server
import calculator_client # noqa: E402
import calculator_server # noqa: E402
def test_calculator():
read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl())
_ = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl())
calculator_client.main(read)
@@ -57,7 +59,7 @@ def test_calculator_gc():
evaluate_impl_orig = calculator_server.evaluate_impl
calculator_server.evaluate_impl = new_evaluate_impl(evaluate_impl_orig)
server = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl())
_ = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl())
calculator_client.main(read)
calculator_server.evaluate_impl = evaluate_impl_orig

View File

@@ -42,7 +42,10 @@ def test_roundtrip_bytes(all_types):
msg = all_types.TestAllTypes.from_bytes(message_bytes)
test_regression.check_all_types(msg)
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="TODO: Investigate why this works on CPython but fails on PyPy.")
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="TODO: Investigate why this works on CPython but fails on PyPy."
)
def test_roundtrip_segments(all_types):
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
@@ -79,7 +82,10 @@ def test_roundtrip_bytes_fail(all_types):
with pytest.raises(TypeError):
all_types.TestAllTypes.from_bytes(42)
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="This works in PyPy 4.0.1 but travisci's version of PyPy has some bug that fails this test.")
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="This works in PyPy 4.0.1 but travisci's version of PyPy has some bug that fails this test."
)
def test_roundtrip_bytes_packed(all_types):
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
@@ -146,7 +152,10 @@ def test_roundtrip_bytes_multiple_packed(all_types):
i += 1
assert i == 3
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="This works on my local PyPy v2.5.0, but is for some reason broken on TravisCI. Skip for now.")
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="This works on my local PyPy v2.5.0, but is for some reason broken on TravisCI. Skip for now."
)
def test_roundtrip_dict(all_types):
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)

View File

@@ -77,7 +77,10 @@ def test_which_reader(addressbook):
addresses.which
@pytest.mark.skipif(capnp.version.LIBCAPNP_VERSION < 5000, reason="Using ints as enums requires v0.5.0+ of the C++ capnp library")
@pytest.mark.skipif(
capnp.version.LIBCAPNP_VERSION < 5000,
reason="Using ints as enums requires v0.5.0+ of the C++ capnp library"
)
def test_enum(addressbook):
addresses = addressbook.AddressBook.new_message()
people = addresses.init('people', 2)
@@ -188,13 +191,8 @@ def test_set_dict_union(addressbook):
assert person.employment.employer.name == 'foo'
try:
basestring # attempt to evaluate basestring
def isstr(s):
return isinstance(s, basestring)
except NameError:
def isstr(s):
return isinstance(s, str)
def isstr(s):
return isinstance(s, str)
def test_to_dict_enum(addressbook):
@@ -227,7 +225,12 @@ def test_to_dict_verbose(addressbook):
def test_to_dict_ordered(addressbook):
person = addressbook.Person.new_message(**{'name': 'Alice', 'phones': [{'type': 'mobile', 'number': '555-1212'}], 'id': 123, 'employment': {'school': 'MIT'}, 'email': 'alice@example.com'})
person = addressbook.Person.new_message(**{
'name': 'Alice',
'phones': [{'type': 'mobile', 'number': '555-1212'}],
'id': 123,
'employment': {'school': 'MIT'}, 'email': 'alice@example.com'
})
if sys.version_info >= (2, 7):
assert list(person.to_dict(ordered=True).keys()) == ['id', 'name', 'email', 'phones', 'employment']
@@ -246,4 +249,4 @@ def test_nested_list(addressbook):
struct.list[1][0] = 2
struct.list[1][1] = 3
assert struct.to_dict()["list"] == [[1], [2,3]]
assert struct.to_dict()["list"] == [[1], [2, 3]]

View File

@@ -1,20 +1,38 @@
import capnp
import pytest
import test_capability_capnp
'''
thread test
'''
import platform
import socket
import threading
import platform
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="pycapnp's GIL handling isn't working properly at the moment for PyPy")
import pytest
import capnp
import test_capability_capnp
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="pycapnp's GIL handling isn't working properly at the moment for PyPy"
)
def test_making_event_loop():
'''
Event loop test
'''
capnp.remove_event_loop(True)
capnp.create_event_loop()
capnp.remove_event_loop()
capnp.create_event_loop()
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="pycapnp's GIL handling isn't working properly at the moment for PyPy")
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="pycapnp's GIL handling isn't working properly at the moment for PyPy"
)
def test_making_threaded_event_loop():
'''
Threaded event loop test
'''
capnp.remove_event_loop(True)
capnp.create_event_loop(True)
@@ -23,23 +41,40 @@ def test_making_threaded_event_loop():
class Server(test_capability_capnp.TestInterface.Server):
'''
Server
'''
def __init__(self, val=1):
self.val = val
def foo(self, i, j, **kwargs):
'''
foo
'''
return str(i * 5 + self.val)
class SimpleRestorer(test_capability_capnp.TestSturdyRefObjectId.Restorer):
'''
SimpleRestorer
'''
def restore(self, ref_id):
'''
Restore
'''
assert ref_id.tag == 'testInterface'
return Server(100)
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="pycapnp's GIL handling isn't working properly at the moment for PyPy")
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="pycapnp's GIL handling isn't working properly at the moment for PyPy"
)
def test_using_threads():
'''
Thread test
'''
capnp.remove_event_loop(True)
capnp.create_event_loop(True)
@@ -47,7 +82,7 @@ def test_using_threads():
def run_server():
restorer = SimpleRestorer()
server = capnp.TwoPartyServer(write, restorer)
_ = capnp.TwoPartyServer(write, restorer)
capnp.wait_forever()
server_thread = threading.Thread(target=run_server)