feat: new alternate syntax and switch to black + isort (yeah, maybe not the best time, but that is done).

This commit is contained in:
Romain Dorgueil
2018-07-29 18:21:56 +01:00
parent 3094e43f9f
commit 89dda0dca6
123 changed files with 1672 additions and 1640 deletions

View File

@ -11,7 +11,7 @@ class NoOptConfigurable(Configurable):
class MyConfigurable(Configurable):
required_str = Option(str)
default_str = Option(str, default='foo')
default_str = Option(str, default="foo")
integer = Option(int, required=False)
@ -20,7 +20,7 @@ class MyHarderConfigurable(MyConfigurable):
class MyBetterConfigurable(MyConfigurable):
required_str = Option(str, required=False, default='kaboom')
required_str = Option(str, required=False, default="kaboom")
class MyConfigurableUsingPositionalOptions(MyConfigurable):
@ -35,7 +35,7 @@ def test_missing_required_option_error():
with pytest.raises(TypeError) as exc:
MyConfigurable(_final=True)
assert exc.match('missing 1 required option:')
assert exc.match("missing 1 required option:")
def test_missing_required_options_error():
@ -44,29 +44,29 @@ def test_missing_required_options_error():
with pytest.raises(TypeError) as exc:
MyHarderConfigurable(_final=True)
assert exc.match('missing 2 required options:')
assert exc.match("missing 2 required options:")
def test_extraneous_option_error():
with pytest.raises(TypeError) as exc:
MyConfigurable(required_str='foo', hello='world')
assert exc.match('got 1 unexpected option:')
MyConfigurable(required_str="foo", hello="world")
assert exc.match("got 1 unexpected option:")
def test_extraneous_options_error():
with pytest.raises(TypeError) as exc:
MyConfigurable(required_str='foo', hello='world', acme='corp')
assert exc.match('got 2 unexpected options:')
MyConfigurable(required_str="foo", hello="world", acme="corp")
assert exc.match("got 2 unexpected options:")
def test_defaults():
o = MyConfigurable(required_str='hello')
o = MyConfigurable(required_str="hello")
with inspect_node(o) as ni:
assert not ni.partial
assert o.required_str == 'hello'
assert o.default_str == 'foo'
assert o.required_str == "hello"
assert o.default_str == "foo"
assert o.integer is None
@ -76,30 +76,30 @@ def test_str_type_factory():
with inspect_node(o) as ni:
assert not ni.partial
assert o.required_str == '42'
assert o.default_str == 'foo'
assert o.required_str == "42"
assert o.default_str == "foo"
assert o.integer is None
def test_int_type_factory():
o = MyConfigurable(required_str='yo', default_str='bar', integer='42')
o = MyConfigurable(required_str="yo", default_str="bar", integer="42")
with inspect_node(o) as ni:
assert not ni.partial
assert o.required_str == 'yo'
assert o.default_str == 'bar'
assert o.required_str == "yo"
assert o.default_str == "bar"
assert o.integer == 42
def test_bool_type_factory():
o = MyHarderConfigurable(required_str='yes', also_required='True')
o = MyHarderConfigurable(required_str="yes", also_required="True")
with inspect_node(o) as ni:
assert not ni.partial
assert o.required_str == 'yes'
assert o.default_str == 'foo'
assert o.required_str == "yes"
assert o.default_str == "foo"
assert o.integer is None
assert o.also_required is True
@ -110,22 +110,22 @@ def test_option_resolution_order():
with inspect_node(o) as ni:
assert not ni.partial
assert o.required_str == 'kaboom'
assert o.default_str == 'foo'
assert o.required_str == "kaboom"
assert o.default_str == "foo"
assert o.integer is None
def test_option_positional():
o = MyConfigurableUsingPositionalOptions('1', '2', '3', required_str='hello')
o = MyConfigurableUsingPositionalOptions("1", "2", "3", required_str="hello")
with inspect_node(o) as ni:
assert not ni.partial
assert o.first == '1'
assert o.second == '2'
assert o.third == '3'
assert o.required_str == 'hello'
assert o.default_str == 'foo'
assert o.first == "1"
assert o.second == "2"
assert o.third == "3"
assert o.required_str == "hello"
assert o.default_str == "foo"
assert o.integer is None

View File

@ -50,10 +50,7 @@ def test_define_with_decorator():
calls = []
def my_handler(*args, **kwargs):
calls.append((
args,
kwargs,
))
calls.append((args, kwargs))
Concrete = MethodBasedConfigurable(my_handler)
@ -64,7 +61,7 @@ def test_define_with_decorator():
assert ci.type == MethodBasedConfigurable
assert ci.partial
t = Concrete('foo', bar='baz')
t = Concrete("foo", bar="baz")
assert callable(t.handler)
assert len(calls) == 0
@ -75,15 +72,12 @@ def test_define_with_decorator():
def test_late_binding_method_decoration():
calls = []
@MethodBasedConfigurable(foo='foo')
@MethodBasedConfigurable(foo="foo")
def Concrete(*args, **kwargs):
calls.append((
args,
kwargs,
))
calls.append((args, kwargs))
assert callable(Concrete.handler)
t = Concrete(bar='baz')
t = Concrete(bar="baz")
assert callable(t.handler)
assert len(calls) == 0
@ -95,12 +89,9 @@ def test_define_with_argument():
calls = []
def concrete_handler(*args, **kwargs):
calls.append((
args,
kwargs,
))
calls.append((args, kwargs))
t = MethodBasedConfigurable(concrete_handler, 'foo', bar='baz')
t = MethodBasedConfigurable(concrete_handler, "foo", bar="baz")
assert callable(t.handler)
assert len(calls) == 0
t()
@ -112,12 +103,9 @@ def test_define_with_inheritance():
class Inheriting(MethodBasedConfigurable):
def handler(self, *args, **kwargs):
calls.append((
args,
kwargs,
))
calls.append((args, kwargs))
t = Inheriting('foo', bar='baz')
t = Inheriting("foo", bar="baz")
assert callable(t.handler)
assert len(calls) == 0
t()
@ -132,13 +120,10 @@ def test_inheritance_then_decorate():
@Inheriting
def Concrete(*args, **kwargs):
calls.append((
args,
kwargs,
))
calls.append((args, kwargs))
assert callable(Concrete.handler)
t = Concrete('foo', bar='baz')
t = Concrete("foo", bar="baz")
assert callable(t.handler)
assert len(calls) == 0
t()

View File

@ -12,11 +12,11 @@ class Bobby(Configurable):
@ContextProcessor
def think(self, context):
yield 'different'
yield "different"
def __call__(self, think, *args, **kwargs):
self.handler('1', *args, **kwargs)
self.handler2('2', *args, **kwargs)
self.handler("1", *args, **kwargs)
self.handler2("2", *args, **kwargs)
def test_partial():
@ -40,7 +40,7 @@ def test_partial():
assert len(ci.options) == 4
assert len(ci.processors) == 1
assert ci.partial
assert ci.partial[0] == (f1, )
assert ci.partial[0] == (f1,)
assert not len(ci.partial[1])
# instanciate a more complete partial instance ...
@ -53,13 +53,10 @@ def test_partial():
assert len(ci.options) == 4
assert len(ci.processors) == 1
assert ci.partial
assert ci.partial[0] == (
f1,
f2,
)
assert ci.partial[0] == (f1, f2)
assert not len(ci.partial[1])
c = C('foo')
c = C("foo")
with inspect_node(c) as ci:
assert ci.type == Bobby

View File

@ -1,7 +1,7 @@
from operator import attrgetter
from bonobo.config import Configurable
from bonobo.config.processors import ContextProcessor, resolve_processors, ContextCurrifier, use_context_processor
from bonobo.config.processors import ContextCurrifier, ContextProcessor, resolve_processors, use_context_processor
class CP1(Configurable):
@ -11,11 +11,11 @@ class CP1(Configurable):
@ContextProcessor
def a(self):
yield 'this is A'
yield "this is A"
@ContextProcessor
def b(self, a):
yield a.upper()[:-1] + 'b'
yield a.upper()[:-1] + "b"
def __call__(self, a, b):
return a, b
@ -46,20 +46,20 @@ class CP3(CP2):
def get_all_processors_names(cls):
return list(map(attrgetter('__name__'), resolve_processors(cls)))
return list(map(attrgetter("__name__"), resolve_processors(cls)))
def test_inheritance_and_ordering():
assert get_all_processors_names(CP1) == ['c', 'a', 'b']
assert get_all_processors_names(CP2) == ['c', 'a', 'b', 'f', 'e', 'd']
assert get_all_processors_names(CP3) == ['c', 'a', 'b', 'f', 'e', 'd', 'c', 'b']
assert get_all_processors_names(CP1) == ["c", "a", "b"]
assert get_all_processors_names(CP2) == ["c", "a", "b", "f", "e", "d"]
assert get_all_processors_names(CP3) == ["c", "a", "b", "f", "e", "d", "c", "b"]
def test_setup_teardown():
o = CP1()
stack = ContextCurrifier(o)
stack.setup()
assert o(*stack.args) == ('this is A', 'THIS IS b')
assert o(*stack.args) == ("this is A", "THIS IS b")
stack.teardown()
@ -71,4 +71,4 @@ def test_processors_on_func():
def node(context):
pass
assert get_all_processors_names(node) == ['cp']
assert get_all_processors_names(node) == ["cp"]

View File

@ -4,11 +4,11 @@ import time
import pytest
from bonobo.config import Configurable, Container, Exclusive, Service, use
from bonobo.config.services import validate_service_name, create_container
from bonobo.config.services import create_container, validate_service_name
from bonobo.util import get_name
class PrinterInterface():
class PrinterInterface:
def print(self, *args):
raise NotImplementedError()
@ -18,46 +18,43 @@ class ConcretePrinter(PrinterInterface):
self.prefix = prefix
def print(self, *args):
return ';'.join((self.prefix, *args))
return ";".join((self.prefix, *args))
SERVICES = Container(
printer0=ConcretePrinter(prefix='0'),
printer1=ConcretePrinter(prefix='1'),
)
SERVICES = Container(printer0=ConcretePrinter(prefix="0"), printer1=ConcretePrinter(prefix="1"))
class MyServiceDependantConfigurable(Configurable):
printer = Service(PrinterInterface, )
printer = Service(PrinterInterface)
def __call__(self, *args, printer: PrinterInterface):
return printer.print(*args)
def test_service_name_validator():
assert validate_service_name('foo') == 'foo'
assert validate_service_name('foo.bar') == 'foo.bar'
assert validate_service_name('Foo') == 'Foo'
assert validate_service_name('Foo.Bar') == 'Foo.Bar'
assert validate_service_name('Foo.a0') == 'Foo.a0'
assert validate_service_name("foo") == "foo"
assert validate_service_name("foo.bar") == "foo.bar"
assert validate_service_name("Foo") == "Foo"
assert validate_service_name("Foo.Bar") == "Foo.Bar"
assert validate_service_name("Foo.a0") == "Foo.a0"
with pytest.raises(ValueError):
validate_service_name('foo.0')
validate_service_name("foo.0")
with pytest.raises(ValueError):
validate_service_name('0.foo')
validate_service_name("0.foo")
def test_service_dependency():
o = MyServiceDependantConfigurable(printer='printer0')
o = MyServiceDependantConfigurable(printer="printer0")
assert o('foo', 'bar', printer=SERVICES.get('printer0')) == '0;foo;bar'
assert o('bar', 'baz', printer=SERVICES.get('printer1')) == '1;bar;baz'
assert o('foo', 'bar', **SERVICES.kwargs_for(o)) == '0;foo;bar'
assert o("foo", "bar", printer=SERVICES.get("printer0")) == "0;foo;bar"
assert o("bar", "baz", printer=SERVICES.get("printer1")) == "1;bar;baz"
assert o("foo", "bar", **SERVICES.kwargs_for(o)) == "0;foo;bar"
def test_service_dependency_unavailable():
o = MyServiceDependantConfigurable(printer='printer2')
o = MyServiceDependantConfigurable(printer="printer2")
with pytest.raises(KeyError):
SERVICES.kwargs_for(o)
@ -72,15 +69,15 @@ class VCR:
def test_exclusive():
vcr = VCR()
vcr.append('hello')
vcr.append("hello")
def record(prefix, vcr=vcr):
with Exclusive(vcr):
for i in range(5):
vcr.append(' '.join((prefix, str(i))))
vcr.append(" ".join((prefix, str(i))))
time.sleep(0.05)
threads = [threading.Thread(target=record, args=(str(i), )) for i in range(5)]
threads = [threading.Thread(target=record, args=(str(i),)) for i in range(5)]
for thread in threads:
thread.start()
@ -90,8 +87,32 @@ def test_exclusive():
thread.join()
assert vcr.tape == [
'hello', '0 0', '0 1', '0 2', '0 3', '0 4', '1 0', '1 1', '1 2', '1 3', '1 4', '2 0', '2 1', '2 2', '2 3',
'2 4', '3 0', '3 1', '3 2', '3 3', '3 4', '4 0', '4 1', '4 2', '4 3', '4 4'
"hello",
"0 0",
"0 1",
"0 2",
"0 3",
"0 4",
"1 0",
"1 1",
"1 2",
"1 3",
"1 4",
"2 0",
"2 1",
"2 2",
"2 3",
"2 4",
"3 0",
"3 1",
"3 2",
"3 3",
"3 4",
"4 0",
"4 1",
"4 2",
"4 3",
"4 4",
]
@ -100,28 +121,25 @@ def test_requires():
services = Container(output=vcr.append)
@use('output')
@use("output")
def append(out, x):
out(x)
svcargs = services.kwargs_for(append)
assert len(svcargs) == 1
assert svcargs['output'] == vcr.append
assert svcargs["output"] == vcr.append
@pytest.mark.parametrize('services', [None, {}])
@pytest.mark.parametrize("services", [None, {}])
def test_create_container_empty_values(services):
c = create_container(services)
assert len(c) == 2
assert 'fs' in c and get_name(c['fs']) == 'OSFS'
assert 'http' in c and get_name(c['http']) == 'requests'
assert "fs" in c and get_name(c["fs"]) == "OSFS"
assert "http" in c and get_name(c["http"]) == "requests"
def test_create_container_override():
c = create_container({
'http': 'http',
'fs': 'fs',
})
c = create_container({"http": "http", "fs": "fs"})
assert len(c) == 2
assert 'fs' in c and c['fs'] == 'fs'
assert 'http' in c and c['http'] == 'http'
assert "fs" in c and c["fs"] == "fs"
assert "http" in c and c["http"] == "http"