Skip to content

Commit 5944e5f

Browse files
committed
.
1 parent 8490b30 commit 5944e5f

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/codegen/sdk/core/class_definition.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,29 @@ def parent_class_names(self) -> list[Name | ChainedAttribute]:
110110
return []
111111

112112
@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:
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.
121122
122123
Returns:
123124
Editable | None: The matching parent class node, or None if no match is found.
124125
"""
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]
126136

127137
@property
128138
@reader
@@ -233,30 +243,35 @@ def methods(self, *, max_depth: int | None = 0, private: bool = True, magic: boo
233243
return list(result.values())
234244

235245
@reader
236-
def get_nested_class(self, name: str) -> Self | None:
246+
def get_nested_class(self, name: str, optional: bool = False) -> Self | None:
237247
"""Returns a nested class by name from the current class.
238248
239249
Searches through the nested classes defined in the class and returns the first one that matches the given name.
240250
241251
Args:
242252
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.
243254
244255
Returns:
245256
Self | None: The nested class if found, None otherwise.
246257
"""
247258
for m in self.nested_classes:
248259
if m.name == name:
249260
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)
250264
return None
251265

252266
@reader
253-
def get_method(self, name: str) -> TFunction | None:
267+
def get_method(self, name: str, optional: bool = False) -> TFunction | None:
254268
"""Returns a specific method by name from the class or any of its superclasses.
255269
256270
Searches through the class's methods and its superclasses' methods to find a method with the specified name.
257271
258272
Args:
259273
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.
260275
261276
Returns:
262277
TFunction | None: The method if found, None otherwise.
@@ -267,6 +282,9 @@ def get_method(self, name: str) -> TFunction | None:
267282
for m in c.methods:
268283
if m.name == name:
269284
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)
270288
return None
271289

272290
@proxy_property
@@ -293,13 +311,14 @@ def attributes(self, *, max_depth: int | None = 0, private: bool = True) -> list
293311
return list(result.values())
294312

295313
@reader
296-
def get_attribute(self, name: str) -> Attribute | None:
314+
def get_attribute(self, name: str, optional: bool = False) -> Attribute | None:
297315
"""Returns a specific attribute by name.
298316
299317
Searches for an attribute with the given name in the current class and its superclasses.
300318
301319
Args:
302320
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.
303322
304323
Returns:
305324
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:
310329
for m in c.code_block.get_attributes(name):
311330
if m.name == name:
312331
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)
313335
return None
314336

315337
####################################################################################################################

src/codegen/sdk/typescript/interfaces/has_block.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,25 @@ def jsx_elements(self) -> list[JSXElement[Self]]:
8787
return jsx_elements
8888

8989
@reader
90-
def get_component(self, component_name: str) -> JSXElement[Self] | None:
90+
def get_component(self, component_name: str, optional: bool = False) -> JSXElement[Self] | None:
9191
"""Returns a specific JSX element from within this symbol's JSX elements.
9292
9393
Searches through all JSX elements in this symbol's code block and returns the first one that matches
9494
the given component name.
9595
9696
Args:
9797
component_name (str): The name of the JSX component to find.
98+
optional (bool, optional): If True, return None if the component is not found. Defaults to False.
9899
99100
Returns:
100101
JSXElement[Self] | None: The matching JSX element if found, None otherwise.
101102
"""
102103
for component in self.jsx_elements:
103104
if component.name == component_name:
104105
return component
106+
if not optional:
107+
msg = f"Component {component_name} not found in symbol {self.name} of file {self.file_path}. Use optional=True to return None instead."
108+
raise ValueError(msg)
105109
return None
106110

107111
@cached_property

0 commit comments

Comments
 (0)