Skip to content

Commit 8562803

Browse files
committed
fix: update conditions paramenter type
1 parent 890148a commit 8562803

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

libs/astx-transpilers/src/astx_transpilers/python_string.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def visit(self, node: astx.GeneratorExpr) -> str:
721721
f"{self.visit(node.element)} for {self.visit(node.target)}"
722722
f" in {self.visit(node.iterable)}"
723723
)
724-
if node.conditions:
725-
for condition in node.conditions:
726-
ret_str += f" if {self.visit(condition)}"
724+
for cond in node.conditions:
725+
ret_str += f" if {self.visit(cond)}"
726+
727727
return f"({ret_str})"

libs/astx-transpilers/tests/test_python.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,6 @@ def test_transpiler_generator_expr() -> None:
15441544
),
15451545
],
15461546
)
1547-
print
15481547
generated_code = translate(gen_expr)
15491548
expected_code = "((x + x) for x in range(10) if (x > 3) if (x < 7))"
15501549
assert generated_code == expected_code, (

libs/astx/src/astx/flows.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import Optional, cast
5+
from typing import Iterable, Optional, cast
66

77
from public import public
88

@@ -747,14 +747,14 @@ class GeneratorExpr(Expr):
747747
element: Expr
748748
target: Expr
749749
iterable: Expr
750-
conditions: list[Expr]
750+
conditions: ASTNodes[Expr] | Iterable[Expr]
751751

752752
def __init__(
753753
self,
754754
element: Expr,
755755
target: Expr,
756756
iterable: Expr,
757-
conditions: Optional[list[Expr]] = None,
757+
conditions: Optional[ASTNodes[Expr]] | Optional[Iterable[Expr]] = [],
758758
loc: SourceLocation = NO_SOURCE_LOCATION,
759759
parent: Optional[ASTNodes] = None,
760760
) -> None:
@@ -763,20 +763,21 @@ def __init__(
763763
self.element = element
764764
self.target = target
765765
self.iterable = iterable
766-
self.conditions = conditions if conditions is not None else []
766+
if isinstance(conditions, ASTNodes):
767+
self.conditions = conditions
768+
elif isinstance(conditions, Iterable):
769+
self.conditions = ASTNodes()
770+
for condition in conditions:
771+
self.conditions.append(condition)
767772
self.kind = ASTKind.GeneratorExprKind
768773

769774
def __str__(self) -> str:
770775
"""Return a string representation of the object."""
771776
ret_str = (
772777
f"GeneratorExpr[element={self.element}, target={self.target},"
773-
f" iterable={self.iterable},"
778+
f" iterable={self.iterable}, conditions="
779+
f"{[str(cond) for cond in self.conditions]}"
774780
)
775-
if self.conditions is not None:
776-
cons_list = []
777-
for cond in self.conditions:
778-
cons_list.append(str(cond))
779-
ret_str += f" conditions={(cons_list)}]"
780781
return ret_str
781782

782783
def get_struct(self, simplified: bool = False) -> ReprStruct:
@@ -786,12 +787,8 @@ def get_struct(self, simplified: bool = False) -> ReprStruct:
786787
"element": self.element.get_struct(simplified),
787788
"target": self.target.get_struct(simplified),
788789
"iterable": self.iterable.get_struct(simplified),
789-
"conditions": cast(
790-
ReprStruct,
791-
{
792-
str(cond): cond.get_struct(simplified)
793-
for cond in self.conditions
794-
},
795-
),
790+
"conditions": self.conditions.get_struct(simplified)
791+
if isinstance(self.conditions, ASTNodes)
792+
else ASTNodes().get_struct(simplified),
796793
}
797794
return self._prepare_struct(key, value, simplified)

libs/astx/tests/test_flows.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ def test_do_while_stmt() -> None:
388388
visualize(do_while_stmt.get_struct())
389389

390390

391-
def test_generator_expr() -> None:
392-
"""Test `GeneratorExpr` class."""
391+
def test_generator_expr_1() -> None:
392+
"""Test `GeneratorExpr` class with conditions of Iterable type."""
393393
gen_expr = astx.GeneratorExpr(
394394
element=astx.BinaryOp(
395395
op_code="+", lhs=astx.Variable("x"), rhs=astx.Variable("x")
@@ -398,14 +398,42 @@ def test_generator_expr() -> None:
398398
iterable=astx.Identifier("range(10)"),
399399
conditions=[
400400
astx.BinaryOp(
401-
op_code=">", lhs=astx.Variable("x"), rhs=astx.LiteralInt32(3)
401+
op_code="<", lhs=astx.Variable("x"), rhs=astx.LiteralInt32(8)
402402
),
403403
astx.BinaryOp(
404-
op_code="<", lhs=astx.Variable("x"), rhs=astx.LiteralInt32(7)
404+
op_code="%", lhs=astx.Variable("x"), rhs=astx.LiteralInt32(2)
405405
),
406406
],
407407
)
408408
assert str(gen_expr)
409409
assert gen_expr.get_struct()
410410
assert gen_expr.get_struct(simplified=True)
411411
visualize(gen_expr.get_struct())
412+
413+
414+
def test_generator_expr_2() -> None:
415+
"""Test `GeneratorExpr` class with conditions of ASTNodes type."""
416+
conditions = astx.ASTNodes[astx.Expr]()
417+
conditions.append(
418+
astx.BinaryOp(
419+
op_code="<", lhs=astx.Variable("x"), rhs=astx.LiteralInt32(8)
420+
)
421+
)
422+
conditions.append(
423+
astx.BinaryOp(
424+
op_code="%", lhs=astx.Variable("x"), rhs=astx.LiteralInt32(2)
425+
)
426+
)
427+
gen_expr = astx.GeneratorExpr(
428+
element=astx.BinaryOp(
429+
op_code="+", lhs=astx.Variable("x"), rhs=astx.Variable("x")
430+
),
431+
target=astx.Variable("x"),
432+
iterable=astx.Identifier("range(10)"),
433+
conditions=conditions,
434+
)
435+
print(gen_expr)
436+
assert str(gen_expr)
437+
assert gen_expr.get_struct()
438+
assert gen_expr.get_struct(simplified=True)
439+
visualize(gen_expr.get_struct())

0 commit comments

Comments
 (0)