fix: Enhance extract_class_name function to identify Component subclasses (#4492)

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-11-11 10:43:59 -03:00 committed by GitHub
commit f43fd895c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -326,10 +326,32 @@ def extract_function_name(code):
raise ValueError(msg)
def extract_class_name(code):
module = ast.parse(code)
for node in module.body:
if isinstance(node, ast.ClassDef):
return node.name
msg = f"No class definition found in the code string. Code snippet: {code[:100]}"
raise ValueError(msg)
def extract_class_name(code: str) -> str:
"""Extract the name of the first Component subclass found in the code.
Args:
code (str): The source code to parse
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