From ba089310208ccd664fc5b50ac66f77e96ba36b33 Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Sat, 11 Aug 2018 07:33:11 +0200 Subject: [PATCH] smell: fixes assert in container constructor. --- bonobo/config/services.py | 7 +++++-- tests/config/test_services.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bonobo/config/services.py b/bonobo/config/services.py index fa6879d..852aae6 100644 --- a/bonobo/config/services.py +++ b/bonobo/config/services.py @@ -66,8 +66,11 @@ class Service(Option): class Container(dict): def __new__(cls, *args, **kwargs): if len(args) == 1: - assert not len(kwargs), 'only one usage at a time, my dear.' - if not (args[0]): + if len(kwargs): + raise ValueError( + 'You can either use {} with one positional argument or with keyword arguments, not both.' + ) + if not args[0]: return super().__new__(cls) if isinstance(args[0], cls): return cls diff --git a/tests/config/test_services.py b/tests/config/test_services.py index 5671e90..a4b8c02 100644 --- a/tests/config/test_services.py +++ b/tests/config/test_services.py @@ -130,6 +130,20 @@ def test_requires(): assert svcargs['output'] == vcr.append +def test_constructor(): + c1 = Container(foo='foo', bar='bar') + assert 2 == len(c1) + + c2 = Container({'foo': 'foo', 'bar': 'bar'}) + assert 2 == len(c2) + + assert c1['foo'] == c2['foo'] + assert c1['bar'] == c2['bar'] + + with pytest.raises(ValueError): + Container({'bar': 'bar'}, foo='foo') + + @pytest.mark.parametrize('services', [None, {}]) def test_create_container_empty_values(services): c = create_container(services)