Skip to content

feat: Implement transpiler ASTx to Python String via ASTx to Python AST#284

Closed
nagajaideep wants to merge 3 commits intoarxlang:mainfrom
nagajaideep:main
Closed

feat: Implement transpiler ASTx to Python String via ASTx to Python AST#284
nagajaideep wants to merge 3 commits intoarxlang:mainfrom
nagajaideep:main

Conversation

@nagajaideep
Copy link
Contributor

This PR refactors the python_to_ast.py module to align with the mentor's architecture for AST-based transpilation.
It removes circular dependencies, introduces a new module for string generation, and ensures a clean separation of concerns between AST conversion and string generation.

Changes Made:

  1. Fixed Circular Dependency in python_to_ast.py:

    • Removed the circular import from astx_transpilers.python_string in the _convert_using_unparse method.
    • Replaced it with a fallback logic returning ast.Constant or ast.Name nodes.
    • Ensured the file is self-contained and only handles ASTx → Python AST conversion.
  2. Created New python_string_new.py:

    • Implements the mentor’s design for string generation.
    • Uses ASTxPythonASTTranspiler from python_to_ast.py internally.
    • Flow: ASTx Node → ASTxPythonASTTranspiler → Python AST → ast.unparse() → String.
    • Maintains backward compatibility with indent handling.
  3. Added test_new_implementation.py:

    • Tests the correctness of the new implementation.

How to test these changes

For Direct AST Output (ASTx → Python AST):

from astx_transpilers.python_to_ast import ASTxPythonASTTranspiler
transpiler = ASTxPythonASTTranspiler()
ast_node = transpiler.visit(astx_node)  # Returns Python AST

For String Output (ASTx → Python Source Code):

from astx_transpilers.python_string_new import ASTxPythonTranspiler
transpiler = ASTxPythonTranspiler()
code_string = transpiler.visit(astx_node)  # Returns Python source code

Run Tests:

pytest -v libs/astx-transpilers/tests/test_new_implementation.py

Pull Request checklists

Note:

This PR is a:

  • bug-fix
  • new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more
    complexity.
  • New and old tests passed locally.

Additional information

Benefits of the Refactor:

  • Single Source of Truth: Only python_to_ast.py handles ASTx → Python AST conversion.
  • No Circular Dependencies: Clean import hierarchy.
  • Reusability: AST output can be used for multiple purposes.
  • Maintainability: Changes only need to be made in one place.
  • Leverages Python's Tools: Uses ast.unparse() for consistent string generation.
  • Modular Design: Clear separation between AST conversion and string generation.

## Reviewer's Checklist

- [ ] I managed to reproduce the problem locally from the `main` branch
- [ ] I managed to test the new changes locally
- [ ] I confirm that the issues mentioned were fixed/resolved .

@xmnlab xmnlab changed the title added all the updates need for the new implementation feat: Implement transpiler ASTx to Python String via ASTx to Python AST Aug 17, 2025
Copy link
Contributor

@xmnlab xmnlab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for working on that @nagajaideep
in general looks pretty good .. I will take a bit more time to review it again soon

@@ -1,1845 +0,0 @@
"""Test Python Transpiler."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nagajaideep you shouldn't remove this file .. because this is the way we test the transpiler .. probably you should update the tests .. but it should be something similart:

transpile an astx node, and check the string if that makes sense

@xmnlab
Copy link
Contributor

xmnlab commented Aug 20, 2025

@nagajaideep could you please rebase your branch on top of the upstream main?

@nagajaideep
Copy link
Contributor Author

nagajaideep commented Aug 20, 2025

Yes @xmnlab will rebase it and include the test_python_string.py file in the upcoming commit and fix those errors and give you an update

@nagajaideep nagajaideep closed this by deleting the head repository Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants