@@ -110,19 +110,29 @@ def parent_class_names(self) -> list[Name | ChainedAttribute]:
110
110
return []
111
111
112
112
@reader
113
- def get_parent_class (self , parent_class_name : str ) -> Editable | None :
113
+ def get_parent_class (self , parent_class_name : str , optional : bool = False ) -> Editable | None :
114
114
"""Returns the parent class node with the specified name.
115
115
116
116
Retrieves a parent class Name or ChainedAttribute node from this class's list of parent class names that matches
117
117
the specified name.
118
118
119
119
Args:
120
120
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.
121
122
122
123
Returns:
123
124
Editable | None: The matching parent class node, or None if no match is found.
124
125
"""
125
- return next ((p for p in self .parent_class_names if p .source == parent_class_name ), None )
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 ]
126
136
127
137
@property
128
138
@reader
@@ -233,30 +243,35 @@ def methods(self, *, max_depth: int | None = 0, private: bool = True, magic: boo
233
243
return list (result .values ())
234
244
235
245
@reader
236
- def get_nested_class (self , name : str ) -> Self | None :
246
+ def get_nested_class (self , name : str , optional : bool = False ) -> Self | None :
237
247
"""Returns a nested class by name from the current class.
238
248
239
249
Searches through the nested classes defined in the class and returns the first one that matches the given name.
240
250
241
251
Args:
242
252
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.
243
254
244
255
Returns:
245
256
Self | None: The nested class if found, None otherwise.
246
257
"""
247
258
for m in self .nested_classes :
248
259
if m .name == name :
249
260
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 )
250
264
return None
251
265
252
266
@reader
253
- def get_method (self , name : str ) -> TFunction | None :
267
+ def get_method (self , name : str , optional : bool = False ) -> TFunction | None :
254
268
"""Returns a specific method by name from the class or any of its superclasses.
255
269
256
270
Searches through the class's methods and its superclasses' methods to find a method with the specified name.
257
271
258
272
Args:
259
273
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.
260
275
261
276
Returns:
262
277
TFunction | None: The method if found, None otherwise.
@@ -267,6 +282,9 @@ def get_method(self, name: str) -> TFunction | None:
267
282
for m in c .methods :
268
283
if m .name == name :
269
284
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 )
270
288
return None
271
289
272
290
@proxy_property
@@ -293,13 +311,14 @@ def attributes(self, *, max_depth: int | None = 0, private: bool = True) -> list
293
311
return list (result .values ())
294
312
295
313
@reader
296
- def get_attribute (self , name : str ) -> Attribute | None :
314
+ def get_attribute (self , name : str , optional : bool = False ) -> Attribute | None :
297
315
"""Returns a specific attribute by name.
298
316
299
317
Searches for an attribute with the given name in the current class and its superclasses.
300
318
301
319
Args:
302
320
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.
303
322
304
323
Returns:
305
324
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.
@@ -310,6 +329,9 @@ def get_attribute(self, name: str) -> Attribute | None:
310
329
for m in c .code_block .get_attributes (name ):
311
330
if m .name == name :
312
331
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 )
313
335
return None
314
336
315
337
####################################################################################################################
0 commit comments