Skip to content

feat: Add support for AsyncFor #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/astx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
ThrowStmt,
)
from astx.flows import (
AsyncForRangeLoopExpr,
AsyncForRangeLoopStmt,
CaseStmt,
ForCountLoopExpr,
ForCountLoopStmt,
Expand Down Expand Up @@ -199,6 +201,8 @@ def get_version() -> str:
"Argument",
"Arguments",
"AssignmentExpr",
"AsyncForRangeLoopExpr",
"AsyncForRangeLoopStmt",
"AwaitExpr",
"BinaryOp",
"Block",
Expand Down
2 changes: 2 additions & 0 deletions src/astx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class ASTKind(Enum):
SwitchStmtKind = -509
GotoStmtKind = -511
WithStmtKind = -512
AsyncRangeLoopStmtKind = -513
AsyncRangeLoopExprKind = -514

# data types
NullDTKind = -600
Expand Down
125 changes: 125 additions & 0 deletions src/astx/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,131 @@ def get_struct(self, simplified: bool = False) -> ReprStruct:
return self._prepare_struct(key, value, simplified)


@public
@typechecked
class AsyncForRangeLoopStmt(StatementType):
"""AST class for asynchronous `For` Range Statement."""

variable: InlineVariableDeclaration
start: Optional[Expr]
end: Expr
step: Optional[Expr]
body: Block

def __init__(
self,
variable: InlineVariableDeclaration,
start: Optional[Expr],
end: Expr,
step: Optional[Expr],
body: Block,
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
"""Initialize the AsyncForRangeLoopStmt instance."""
super().__init__(loc=loc, parent=parent)
self.variable = variable
self.start = start
self.end = end
self.step = step
self.body = body
self.kind = ASTKind.AsyncRangeLoopStmtKind

def __str__(self) -> str:
"""Return a string that represents the object."""
start = self.start
end = self.end
step = self.step
var_name = self.variable.name
return f"AsyncForRangeLoopStmt({var_name}=[{start}:{end}:{step}])"

def get_struct(self, simplified: bool = False) -> ReprStruct:
"""Return the AST structure of the object."""
for_start = {
"start": {}
if self.start is None
else self.start.get_struct(simplified)
}
for_end = {"end": self.end.get_struct(simplified)}
for_step = {
"step": {}
if self.step is None
else self.step.get_struct(simplified)
}
for_body = self.body.get_struct(simplified)

key = "ASYNC-FOR-RANGE-LOOP-STMT"
value: ReprStruct = {
**cast(DictDataTypesStruct, for_start),
**cast(DictDataTypesStruct, for_end),
**cast(DictDataTypesStruct, for_step),
**cast(DictDataTypesStruct, for_body),
}
return self._prepare_struct(key, value, simplified)


@public
@typechecked
class AsyncForRangeLoopExpr(Expr):
"""AST class for asynchronous `For` Range Expression."""

variable: InlineVariableDeclaration
start: Optional[Expr]
end: Expr
step: Optional[Expr]
body: Block

def __init__(
self,
variable: InlineVariableDeclaration,
start: Optional[Expr],
end: Expr,
step: Optional[Expr],
body: Block,
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
"""Initialize the AsyncForRangeLoopExpr instance."""
super().__init__(loc=loc, parent=parent)
self.variable = variable
self.start = start
self.end = end
self.step = step
self.body = body
self.kind = ASTKind.AsyncRangeLoopExprKind

def __str__(self) -> str:
"""Return a string that represents the object."""
var_name = self.variable.name
return f"AsyncForRangeLoopExpr[{var_name}]"

def get_struct(self, simplified: bool = False) -> ReprStruct:
"""Return the AST structure of the object."""
for_var = {"var": self.variable.get_struct(simplified)}
for_start = {
"start": {}
if self.start is None
else self.start.get_struct(simplified)
}
for_end = {"end": self.end.get_struct(simplified)}
for_step = {
"step": {}
if self.step is None
else self.step.get_struct(simplified)
}
for_body = self.body.get_struct(simplified)

key = "ASYNC-FOR-RANGE-LOOP-EXPR"
value: ReprStruct = {
**cast(DictDataTypesStruct, for_var),
**cast(DictDataTypesStruct, for_start),
**cast(DictDataTypesStruct, for_end),
**cast(DictDataTypesStruct, for_step),
**cast(DictDataTypesStruct, for_body),
}
return self._prepare_struct(key, value, simplified)


@public
@typechecked
class WhileStmt(StatementType):
Expand Down
25 changes: 25 additions & 0 deletions src/astx/tools/transpilers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ def visit(self, node: astx.AssignmentExpr) -> str:
target_str = " = ".join(self.visit(target) for target in node.targets)
return f"{target_str} = {self.visit(node.value)}"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.AsyncForRangeLoopExpr) -> str:
"""Handle AsyncForRangeLoopExpr nodes."""
if len(node.body) > 1:
raise ValueError(
"AsyncForRangeLoopExpr in Python just accept 1 node in the "
"body attribute."
)
start = (
self.visit(node.start)
if getattr(node, "start", None) is not None
else "0"
)
end = self.visit(node.end)
step = (
self.visit(node.step)
if getattr(node, "step", None) is not None
else "1"
)

return (
f"result = [{self.visit(node.body).strip()} async for "
f"{node.variable.name} in range({start}, {end}, {step})]"
)

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.AwaitExpr) -> str:
"""Handle AwaitExpr nodes."""
Expand Down
42 changes: 42 additions & 0 deletions tests/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from astx.base import SourceLocation
from astx.blocks import Block
from astx.flows import (
AsyncForRangeLoopExpr,
AsyncForRangeLoopStmt,
CaseStmt,
ForCountLoopExpr,
ForCountLoopStmt,
Expand Down Expand Up @@ -155,6 +157,46 @@ def test_for_count_loop_expr() -> None:
visualize(for_expr.get_struct())


def test_async_for_range_loop_expr() -> None:
"""Test `Async For Range Loop` expression`."""
decl_a = InlineVariableDeclaration(
"a", type_=Int32(), value=LiteralInt32(-1)
)
start = LiteralInt32(1)
end = LiteralInt32(10)
step = LiteralInt32(1)
body = Block()
body.append(LiteralInt32(2))
for_expr = AsyncForRangeLoopExpr(
variable=decl_a, start=start, end=end, step=step, body=body
)

assert str(for_expr)
assert for_expr.get_struct()
assert for_expr.get_struct(simplified=True)
visualize(for_expr.get_struct())


def test_async_for_range_loop_stmt() -> None:
"""Test `Async For Range Loop` statement."""
decl_a = InlineVariableDeclaration(
"a", type_=Int32(), value=LiteralInt32(-1)
)
start = LiteralInt32(1)
end = LiteralInt32(10)
step = LiteralInt32(1)
body = Block()
body.append(LiteralInt32(2))
for_stmt = AsyncForRangeLoopStmt(
variable=decl_a, start=start, end=end, step=step, body=body
)

assert str(for_stmt)
assert for_stmt.get_struct()
assert for_stmt.get_struct(simplified=True)
visualize(for_stmt.get_struct())


def test_while_expr() -> None:
"""Test `WhileExpr` class."""
# Define a condition: x < 5
Expand Down
23 changes: 23 additions & 0 deletions tests/tools/transpilers/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,29 @@ def test_transpiler_for_range_loop_expr() -> None:
)


def test_transpiler_async_for_range_loop_expr() -> None:
"""Test `Async For Range Loop` expression`."""
decl_a = astx.InlineVariableDeclaration(
"a", type_=astx.Int32(), value=astx.LiteralInt32(-1)
)
start = astx.LiteralInt32(0)
end = astx.LiteralInt32(10)
step = astx.LiteralInt32(1)
body = astx.Block()
body.append(astx.LiteralInt32(2))

for_expr = astx.AsyncForRangeLoopExpr(
variable=decl_a, start=start, end=end, step=step, body=body
)

generated_code = translate(for_expr)
expected_code = "result = [2 async for a in range(0, 10, 1)]"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_transpiler_binary_op() -> None:
"""Test astx.BinaryOp for addition operation."""
# Create a BinaryOp node for the expression "x + y"
Expand Down
Loading