Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Better error message on bogus constructor param to elements and other model items #51

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
------------------
16 changes: 15 additions & 1 deletion src/structurizr/model/model_item.py
Original file line number Diff line number Diff line change
@@ -83,7 +83,21 @@ 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)
20 changes: 20 additions & 0 deletions tests/unit/model/test_model_item.py
Original file line number Diff line number Diff line change
@@ -45,6 +45,26 @@ 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.