2424"""
2525
2626@dataclass (slots = True )
27- class IdentifiedObject (object , metaclass = ABCMeta ):
27+ class IdentifiedObject (metaclass = ABCMeta ):
2828 """
2929 Root class to provide common identification for all classes needing identification and naming attributes.
3030 Everything should extend this class, however it's not mandated that every subclass must use all the fields
@@ -49,6 +49,8 @@ class IdentifiedObject(object, metaclass=ABCMeta):
4949 # TODO: Missing num_diagram_objects: int = None def has_diagram_objects(self): return (self.num_diagram_objects or 0) > 0
5050
5151 def __init__ (self , names : Optional [List [Name ]] = None , ** kwargs ):
52+ if kwargs :
53+ raise TypeError ("unexpected keyword arguments in IdentifiedObject constructor: {}" .format (kwargs ))
5254 super (IdentifiedObject , self ).__init__ (** kwargs )
5355 if names :
5456 for name in names :
@@ -209,7 +211,7 @@ def _validate_reference(self, other: T, get_identifier: Callable[[Callable], str
209211
210212 # FIXME: in python 3.11, the IdentifiedObject type hint can be replaced with Self, and this can all be moved into the class def.
211213 # @singledispatchmethod
212- def _validate_reference (self , other : IdentifiedObject | T , getter : Callable [[str ], IdentifiedObject | T ], type_description : Callable [[], str ], get_identifier : Callable [[...], str ]= None ) -> bool :
214+ def _validate_reference (self , other : IdentifiedObject | T , getter : Callable [[str ], IdentifiedObject | T ], type_description : Callable [[], str ] | str , get_identifier : Callable [[...], str ]= None ) -> bool :
213215 """
214216 Validate whether a given reference exists to `other` using the provided getter function.
215217
@@ -221,18 +223,20 @@ def _validate_reference(self, other: IdentifiedObject | T, getter: Callable[[str
221223 """
222224 if isinstance (other , IdentifiedObject ):
223225 get_identifier = lambda _other : _other .mrid
224- describe_other = f"{ type_description } with mRID { other .mrid } "
226+ describe_other = lambda : f"{ type_description } with mRID { other .mrid } "
225227 else :
226228 require (get_identifier is not None , lambda : "foo" )
227229 describe_other = type_description
228-
229230 try :
230231 get_result = getter (get_identifier (other ))
231- require (get_result is other , lambda : f"{ describe_other } already exists in { str (self )} " )
232- return True
233232 except (KeyError , AttributeError ):
234233 return False
235234
235+ if get_result is other :
236+ return True
237+
238+ raise ValueError (f"{ describe_other ()} already exists in { str (self )} " )
239+
236240 def _validate_reference_by_field (self , other : IdentifiedObject , field : Any , getter : Callable [[Any ], IdentifiedObject ],
237241 field_name : str ) -> bool :
238242 """
0 commit comments