fix: Enhance extract_class_name function to identify Component subclasses (#4492)
This commit is contained in:
parent
357c5878a5
commit
f43fd895c6
1 changed files with 29 additions and 7 deletions
|
|
@ -326,10 +326,32 @@ def extract_function_name(code):
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
def extract_class_name(code):
|
def extract_class_name(code: str) -> str:
|
||||||
module = ast.parse(code)
|
"""Extract the name of the first Component subclass found in the code.
|
||||||
for node in module.body:
|
|
||||||
if isinstance(node, ast.ClassDef):
|
Args:
|
||||||
return node.name
|
code (str): The source code to parse
|
||||||
msg = f"No class definition found in the code string. Code snippet: {code[:100]}"
|
|
||||||
raise ValueError(msg)
|
Returns:
|
||||||
|
str: Name of the first Component subclass found
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If no Component subclass is found in the code
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
module = ast.parse(code)
|
||||||
|
for node in module.body:
|
||||||
|
if not isinstance(node, ast.ClassDef):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Check bases for Component inheritance
|
||||||
|
# TODO: Build a more robust check for Component inheritance
|
||||||
|
for base in node.bases:
|
||||||
|
if isinstance(base, ast.Name) and any(pattern in base.id for pattern in ["Component", "LC"]):
|
||||||
|
return node.name
|
||||||
|
|
||||||
|
msg = f"No Component subclass found in the code string. Code snippet: {code[:100]}"
|
||||||
|
raise TypeError(msg)
|
||||||
|
except SyntaxError as e:
|
||||||
|
msg = f"Invalid Python code: {e!s}"
|
||||||
|
raise ValueError(msg) from e
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue