File tree Expand file tree Collapse file tree 3 files changed +31
-1
lines changed
Expand file tree Collapse file tree 3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 44
55Next Release
66------------
7+ * Fix: Better error on bad ModelItem constructor argument (#50)
78
890.2.1 (2020-11-27)
910------------------
Original file line number Diff line number Diff line change @@ -84,7 +84,17 @@ def __init__(
8484 ** kwargs ,
8585 ):
8686 """Initialise a ModelItem instance."""
87- super ().__init__ (** kwargs )
87+ if len (kwargs ) > 0 :
88+ type_name = type (self ).__name__
89+ args = [f"'{ key } '" for key in kwargs .keys ()]
90+ if len (args ) == 1 :
91+ raise TypeError (f"{ type_name } .__init__() got an unexpected "
92+ f"keyword argument { args [0 ]} " )
93+ else :
94+ raise TypeError (f"{ type_name } .__init__() got unexpected "
95+ f"keyword arguments { ', ' .join (args )} " )
96+
97+ super ().__init__ ()
8898 self .id = id
8999 self .tags = OrderedSet (tags )
90100 self .properties = dict (properties )
Original file line number Diff line number Diff line change @@ -45,6 +45,25 @@ def test_model_item_init(attributes):
4545 assert getattr (model_item , attr ) == expected
4646
4747
48+ def test_handling_of_bogus_init_params ():
49+ """
50+ Test for sensible error message if wrong param passed.
51+
52+ See https://github.com/Midnighter/structurizr-python/issues/50.
53+ """
54+ with pytest .raises (
55+ TypeError , match = r"ConcreteModelItem.__init__\(\) got an unexpected "
56+ r"keyword argument 'foo'"
57+ ):
58+ ConcreteModelItem (foo = 7 )
59+ with pytest .raises (
60+ TypeError ,
61+ match = r"ConcreteModelItem.__init__\(\) got unexpected keyword "
62+ r"arguments 'foo', 'bar'" ,
63+ ):
64+ ConcreteModelItem (foo = 7 , bar = 17 )
65+
66+
4867def test_default_element_tags_order (empty_model : Model ):
4968 """
5069 Test that the default tags get added in the right order.
You can’t perform that action at this time.
0 commit comments