Skip to content

Commit 9dd016f

Browse files
committed
updated ast parsing
1 parent b5b2dbf commit 9dd016f

File tree

2 files changed

+340
-25
lines changed

2 files changed

+340
-25
lines changed

llms.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,25 @@ def generic_visit(self, node: ast.AST) -> ast.AST:
6969
transformer = RemoveCommentsAndDocstrings()
7070
cleaned_ast = transformer.visit(parsed_ast)
7171
ast.fix_missing_locations(cleaned_ast)
72-
# Use astroid to handle AST to source code conversion
73-
astroid_module = astroid.parse(ast.dump(cleaned_ast))
74-
compressed_code = cast(str, astroid_module.as_string())
75-
# Remove multiple blank lines
72+
73+
# Convert AST to source code using astroid
74+
try:
75+
# Try using ast.unparse first (Python 3.9+)
76+
source = ast.unparse(cleaned_ast) # type: ignore
77+
except (AttributeError, TypeError):
78+
# Fallback to astroid for older Python versions
79+
source = cast(str, astroid.parse(ast.dump(cleaned_ast)).as_string())
80+
81+
# Clean up the source code
82+
lines = source.split("\n")
83+
cleaned_lines = []
84+
for line in lines:
85+
# Remove empty pass statements
86+
if line.strip() != "pass":
87+
cleaned_lines.append(line)
88+
89+
# Join lines and remove multiple blank lines
90+
compressed_code = "\n".join(cleaned_lines)
7691
compressed_code = re.sub(r"\n\s*\n\s*\n", "\n\n", compressed_code)
7792
return compressed_code
7893
except Exception as e:

0 commit comments

Comments
 (0)