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