@@ -110,29 +110,19 @@ def parent_class_names(self) -> list[Name | ChainedAttribute]:
110110 return []
111111
112112 @reader
113- def get_parent_class (self , parent_class_name : str , optional : bool = False ) -> Editable | None :
113+ def get_parent_class (self , parent_class_name : str ) -> Editable | None :
114114 """Returns the parent class node with the specified name.
115115
116116 Retrieves a parent class Name or ChainedAttribute node from this class's list of parent class names that matches
117117 the specified name.
118118
119119 Args:
120120 parent_class_name (str): The name of the parent class to find.
121- optional (bool, optional): Whether to return None if the parent class is not found. Defaults to False.
122121
123122 Returns:
124123 Editable | None: The matching parent class node, or None if no match is found.
125124 """
126- parent_class = [p for p in self .parent_class_names if p .source == parent_class_name ]
127- if not parent_class :
128- if not optional :
129- msg = f"Parent class { parent_class_name } not found in class { self .name } . Use optional=True to return None instead."
130- raise ValueError (msg )
131- return None
132- if len (parent_class ) > 1 :
133- msg = f"Multiple parent classes found with name { parent_class_name } in class { self .name } ."
134- raise ValueError (msg )
135- return parent_class [0 ]
125+ return next ((p for p in self .parent_class_names if p .source == parent_class_name ), None )
136126
137127 @property
138128 @reader
@@ -243,35 +233,30 @@ def methods(self, *, max_depth: int | None = 0, private: bool = True, magic: boo
243233 return list (result .values ())
244234
245235 @reader
246- def get_nested_class (self , name : str , optional : bool = False ) -> Self | None :
236+ def get_nested_class (self , name : str ) -> Self | None :
247237 """Returns a nested class by name from the current class.
248238
249239 Searches through the nested classes defined in the class and returns the first one that matches the given name.
250240
251241 Args:
252242 name (str): The name of the nested class to find.
253- optional (bool, optional): Whether to return None if the nested class is not found. Defaults to False.
254243
255244 Returns:
256245 Self | None: The nested class if found, None otherwise.
257246 """
258247 for m in self .nested_classes :
259248 if m .name == name :
260249 return m
261- if not optional :
262- msg = f"Nested class { name } not found in class { self .name } . Use optional=True to return None instead."
263- raise ValueError (msg )
264250 return None
265251
266252 @reader
267- def get_method (self , name : str , optional : bool = False ) -> TFunction | None :
253+ def get_method (self , name : str ) -> TFunction | None :
268254 """Returns a specific method by name from the class or any of its superclasses.
269255
270256 Searches through the class's methods and its superclasses' methods to find a method with the specified name.
271257
272258 Args:
273259 name (str): The name of the method to find.
274- optional (bool, optional): Whether to return None if the method is not found. Defaults to False.
275260
276261 Returns:
277262 TFunction | None: The method if found, None otherwise.
@@ -282,9 +267,6 @@ def get_method(self, name: str, optional: bool = False) -> TFunction | None:
282267 for m in c .methods :
283268 if m .name == name :
284269 return m
285- if not optional :
286- msg = f"Method { name } not found in class { self .name } . Use optional=True to return None instead."
287- raise ValueError (msg )
288270 return None
289271
290272 @proxy_property
@@ -311,14 +293,13 @@ def attributes(self, *, max_depth: int | None = 0, private: bool = True) -> list
311293 return list (result .values ())
312294
313295 @reader
314- def get_attribute (self , name : str , optional : bool = False ) -> Attribute | None :
296+ def get_attribute (self , name : str ) -> Attribute | None :
315297 """Returns a specific attribute by name.
316298
317299 Searches for an attribute with the given name in the current class and its superclasses.
318300
319301 Args:
320302 name (str): The name of the attribute to search for.
321- optional (bool, optional): Whether to return None if the attribute is not found. Defaults to False.
322303
323304 Returns:
324305 Attribute | None: The matching attribute if found, None otherwise. If multiple attributes with the same name exist in the inheritance hierarchy, returns the first one found.
@@ -329,9 +310,6 @@ def get_attribute(self, name: str, optional: bool = False) -> Attribute | None:
329310 for m in c .code_block .get_attributes (name ):
330311 if m .name == name :
331312 return m
332- if not optional :
333- msg = f"Attribute { name } not found in class { self .name } . Use optional=True to return None instead."
334- raise ValueError (msg )
335313 return None
336314
337315 ####################################################################################################################
0 commit comments