From e2ffa0a34c4aebf17925884c5a3939d8d01ae836 Mon Sep 17 00:00:00 2001 From: yt-ms <71541861+yt-ms@users.noreply.github.com> Date: Mon, 2 Nov 2020 09:41:58 +0000 Subject: [PATCH 1/2] fix(ModelItem): better error message on bogus constructor param --- CHANGELOG.rst | 1 + src/structurizr/model/model_item.py | 12 +++++++++++- tests/unit/model/test_model_item.py | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a438020b..13e1a666 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,7 @@ History Next Release ------------ * Feat: Add implied relationships to entities (#42) +* Fix: Better error on bad ModelItem constructor argument (#50) 0.1.1 (2020-10-19) ------------------ diff --git a/src/structurizr/model/model_item.py b/src/structurizr/model/model_item.py index 4e79ca25..7a61a460 100644 --- a/src/structurizr/model/model_item.py +++ b/src/structurizr/model/model_item.py @@ -83,7 +83,17 @@ def __init__( **kwargs, ): """""" - super().__init__(**kwargs) + if len(kwargs) > 0: + type_name = type(self).__name__ + args = [f"'{key}'" for key in kwargs.keys()] + if len(args) == 1: + raise TypeError(f"{type_name}.__init__() got an unexpected " + f"keyword argument {args[0]}") + else: + raise TypeError(f"{type_name}.__init__() got unexpected " + f"keyword arguments {', '.join(args)}") + + super().__init__() self.id = id self.tags = OrderedSet(tags) self.properties = dict(properties) diff --git a/tests/unit/model/test_model_item.py b/tests/unit/model/test_model_item.py index 496f9d86..6015bd1c 100644 --- a/tests/unit/model/test_model_item.py +++ b/tests/unit/model/test_model_item.py @@ -45,6 +45,25 @@ def test_model_item_init(attributes): assert getattr(model_item, attr) == expected +def test_handling_of_bogus_init_params(): + """ + Test for sensible error message if wrong param passed. + + See https://github.com/Midnighter/structurizr-python/issues/50. + """ + with pytest.raises( + TypeError, match=r"ConcreteModelItem.__init__\(\) got an unexpected " + r"keyword argument 'foo'" + ): + ConcreteModelItem(foo=7) + with pytest.raises( + TypeError, + match=r"ConcreteModelItem.__init__\(\) got unexpected keyword " + r"arguments 'foo', 'bar'", + ): + ConcreteModelItem(foo=7, bar=17) + + def test_default_element_tags_order(empty_model: Model): """ Test that the default tags get added in the right order. From ef4b95171dc4140d842ba3adfe195b6c816bd86f Mon Sep 17 00:00:00 2001 From: yt-ms <71541861+yt-ms@users.noreply.github.com> Date: Mon, 2 Nov 2020 09:59:58 +0000 Subject: [PATCH 2/2] style: black reformatting --- src/structurizr/model/model_item.py | 12 ++++++++---- tests/unit/model/test_model_item.py | 7 ++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/structurizr/model/model_item.py b/src/structurizr/model/model_item.py index 7a61a460..8f97b895 100644 --- a/src/structurizr/model/model_item.py +++ b/src/structurizr/model/model_item.py @@ -87,11 +87,15 @@ def __init__( type_name = type(self).__name__ args = [f"'{key}'" for key in kwargs.keys()] if len(args) == 1: - raise TypeError(f"{type_name}.__init__() got an unexpected " - f"keyword argument {args[0]}") + raise TypeError( + f"{type_name}.__init__() got an unexpected " + f"keyword argument {args[0]}" + ) else: - raise TypeError(f"{type_name}.__init__() got unexpected " - f"keyword arguments {', '.join(args)}") + raise TypeError( + f"{type_name}.__init__() got unexpected " + f"keyword arguments {', '.join(args)}" + ) super().__init__() self.id = id diff --git a/tests/unit/model/test_model_item.py b/tests/unit/model/test_model_item.py index 6015bd1c..12a1d5b5 100644 --- a/tests/unit/model/test_model_item.py +++ b/tests/unit/model/test_model_item.py @@ -52,14 +52,15 @@ def test_handling_of_bogus_init_params(): See https://github.com/Midnighter/structurizr-python/issues/50. """ with pytest.raises( - TypeError, match=r"ConcreteModelItem.__init__\(\) got an unexpected " - r"keyword argument 'foo'" + TypeError, + match=r"ConcreteModelItem.__init__\(\) got an unexpected " + r"keyword argument 'foo'", ): ConcreteModelItem(foo=7) with pytest.raises( TypeError, match=r"ConcreteModelItem.__init__\(\) got unexpected keyword " - r"arguments 'foo', 'bar'", + r"arguments 'foo', 'bar'", ): ConcreteModelItem(foo=7, bar=17)