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 not self .mrid or not self .mrid .strip ():
5456 raise ValueError ("You must provide an mRID for this object." )
@@ -212,7 +214,7 @@ def _validate_reference(self, other: T, get_identifier: Callable[[Callable], str
212214
213215 # FIXME: in python 3.11, the IdentifiedObject type hint can be replaced with Self, and this can all be moved into the class def.
214216 # @singledispatchmethod
215- def _validate_reference (self , other : IdentifiedObject | T , getter : Callable [[str ], IdentifiedObject | T ], type_description : Callable [[], str ], get_identifier : Callable [[...], str ]= None ) -> bool :
217+ def _validate_reference (self , other : IdentifiedObject | T , getter : Callable [[str ], IdentifiedObject | T ], type_description : Callable [[], str ] | str , get_identifier : Callable [[...], str ]= None ) -> bool :
216218 """
217219 Validate whether a given reference exists to `other` using the provided getter function.
218220
@@ -224,18 +226,20 @@ def _validate_reference(self, other: IdentifiedObject | T, getter: Callable[[str
224226 """
225227 if isinstance (other , IdentifiedObject ):
226228 get_identifier = lambda _other : _other .mrid
227- describe_other = f"{ type_description } with mRID { other .mrid } "
229+ describe_other = lambda : f"{ type_description } with mRID { other .mrid } "
228230 else :
229231 require (get_identifier is not None , lambda : "foo" )
230232 describe_other = type_description
231-
232233 try :
233234 get_result = getter (get_identifier (other ))
234- require (get_result is other , lambda : f"{ describe_other } already exists in { str (self )} " )
235- return True
236235 except (KeyError , AttributeError ):
237236 return False
238237
238+ if get_result is other :
239+ return True
240+
241+ raise ValueError (f"{ describe_other ()} already exists in { str (self )} " )
242+
239243 def _validate_reference_by_field (self , other : IdentifiedObject , field : Any , getter : Callable [[Any ], IdentifiedObject ],
240244 field_name : str ) -> bool :
241245 """
0 commit comments