This repository was archived by the owner on Jan 21, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +44
-29
lines changed Expand file tree Collapse file tree 3 files changed +44
-29
lines changed Original file line number Diff line number Diff line change 25
25
class AbstractBase (ABC ):
26
26
"""Define common business logic through an abstract base class."""
27
27
28
+ def __init__ (self , ** kwargs ):
29
+ """
30
+ Initialize an abstract base class.
31
+
32
+ The AbstractBase class is designed to be the singular root of the entire class
33
+ hierarchy, similar to `object`, and acts as a guard against unknown keyword
34
+ arguments. Any keyword arguments not consumed in the hierarchy above cause a
35
+ `TypeError`.
36
+
37
+ """
38
+ if kwargs :
39
+ is_plural = len (kwargs ) > 1
40
+ message = "\n " .join (f"{ key } ={ value } " for key , value in kwargs .items ())
41
+ raise TypeError (
42
+ f"{ type (self ).__name__ } .__init__() got { '' if is_plural else 'an ' } "
43
+ f"unexpected keyword argument{ 's' if is_plural else '' } :\n { message } "
44
+ )
45
+ super ().__init__ ()
46
+
28
47
def __hash__ (self ) -> int :
29
48
"""Return an integer that represents a unique hash value for this instance."""
30
49
return id (self )
Original file line number Diff line number Diff line change @@ -84,21 +84,7 @@ def __init__(
84
84
** kwargs ,
85
85
):
86
86
"""Initialise a ModelItem instance."""
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 (
92
- f"{ type_name } .__init__() got an unexpected "
93
- f"keyword argument { args [0 ]} "
94
- )
95
- else :
96
- raise TypeError (
97
- f"{ type_name } .__init__() got unexpected "
98
- f"keyword arguments { ', ' .join (args )} "
99
- )
100
-
101
- super ().__init__ ()
87
+ super ().__init__ (** kwargs )
102
88
self .id = id
103
89
self .tags = OrderedSet (tags )
104
90
self .properties = dict (properties )
Original file line number Diff line number Diff line change @@ -45,24 +45,34 @@ def test_model_item_init(attributes):
45
45
assert getattr (model_item , attr ) == expected
46
46
47
47
48
- def test_handling_of_bogus_init_params ():
48
+ @pytest .mark .parametrize (
49
+ "kwargs" ,
50
+ [
51
+ pytest .param (
52
+ {"foo" : 7 },
53
+ marks = pytest .mark .raises (
54
+ exception = TypeError ,
55
+ message = "ConcreteModelItem.__init__() got an unexpected keyword "
56
+ "argument:\n foo=7" ,
57
+ ),
58
+ ),
59
+ pytest .param (
60
+ {"foo" : 7 , "bar" : 17 },
61
+ marks = pytest .mark .raises (
62
+ exception = TypeError ,
63
+ message = "ConcreteModelItem.__init__() got unexpected keyword "
64
+ "arguments:\n foo=7\n bar=17" ,
65
+ ),
66
+ ),
67
+ ],
68
+ )
69
+ def test_handle_unknown_argument (kwargs : dict ):
49
70
"""
50
- Test for sensible error message if wrong param passed.
71
+ Test for a sensible error message when unknown keyword arguments are passed.
51
72
52
73
See https://github.com/Midnighter/structurizr-python/issues/50.
53
74
"""
54
- with pytest .raises (
55
- TypeError ,
56
- match = r"ConcreteModelItem.__init__\(\) got an unexpected "
57
- r"keyword argument 'foo'" ,
58
- ):
59
- ConcreteModelItem (foo = 7 )
60
- with pytest .raises (
61
- TypeError ,
62
- match = r"ConcreteModelItem.__init__\(\) got unexpected keyword "
63
- r"arguments 'foo', 'bar'" ,
64
- ):
65
- ConcreteModelItem (foo = 7 , bar = 17 )
75
+ ConcreteModelItem (** kwargs )
66
76
67
77
68
78
def test_default_element_tags_order (empty_model : Model ):
You can’t perform that action at this time.
0 commit comments