Skip to content
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
2 changes: 1 addition & 1 deletion .makim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ groups:
params:
help: Specify parameters to be used for tests
type: string
default: "-vv"
default: "-vv -n auto"
backend: bash
run: |
pytest \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def visit(self, node: astx.GeneratorExpr) -> str:
@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Identifier) -> str:
"""Handle Identifier nodes."""
return f"{node.value}"
return f"{node.name}"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.IfExpr) -> str:
Expand Down
8 changes: 4 additions & 4 deletions libs/astx-transpilers/src/astx_transpilers/python_to_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ def visit(self, node: astx.CatchHandlerStmt) -> ast.ExceptHandler:
type_ = self.visit(node.types[0])
name = None
if hasattr(node, "name") and node.name:
if hasattr(node.name, "value"):
name = node.name.value
if hasattr(node.name, "name"):
name = node.name.name
elif isinstance(node.name, str):
name = node.name
else:
Expand Down Expand Up @@ -867,9 +867,9 @@ def visit(self, node: astx.GeneratorExpr) -> ast.GeneratorExp:
@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Identifier) -> ast.Name:
"""Handle Identifier nodes."""
if not hasattr(node, "value"):
if not hasattr(node, "name"):
return self._convert_using_unparse(node)
return ast.Name(id=str(node.value), ctx=ast.Load())
return ast.Name(id=node.name, ctx=ast.Load())

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.IfExpr) -> ast.IfExp:
Expand Down
4 changes: 2 additions & 2 deletions libs/astx-transpilers/tests/test_python_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,8 @@ def test_transpiler_assignmentexpr() -> None:
def test_transpiler_delete_stmt() -> None:
"""Test astx.DeleteStmt transpilation."""
# Create identifiers to be deleted
var1 = astx.Identifier(value="x")
var2 = astx.Identifier(value="y")
var1 = astx.Identifier(name="x")
var2 = astx.Identifier(name="y")

# Create a DeleteStmt with multiple targets
delete_stmt = astx.DeleteStmt(value=[var1, var2])
Expand Down
18 changes: 9 additions & 9 deletions libs/astx-transpilers/tests/test_python_to_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def test_for_count_loop_stmt_simple(self) -> None:
comparators=[astx.LiteralInt32(value=10)],
)
update = astx.AugAssign(
target=astx.Identifier(value="i"),
target=astx.Identifier(name="i"),
value=astx.LiteralInt32(value=1),
op_code="+=",
)
Expand Down Expand Up @@ -684,12 +684,12 @@ def setup_method(self) -> None:

def test_catch_handler_stmt_simple(self) -> None:
"""Test astx.CatchHandlerStmt with exception type."""
exception_type = astx.Identifier(value="Exception")
exception_type = astx.Identifier(name="Exception")
body_stmt = astx.BreakStmt()
body = astx.Block()
body.append(body_stmt)
node = astx.CatchHandlerStmt(
types=[exception_type], name=astx.Identifier(value="e"), body=body
types=[exception_type], name=astx.Identifier(name="e"), body=body
)
result = self.transpiler.visit(node)
assert isinstance(result, ast.ExceptHandler)
Expand All @@ -701,8 +701,8 @@ def test_exception_handler_stmt_simple(self) -> None:
body = astx.Block()
body.append(body_stmt)
handler = astx.CatchHandlerStmt(
types=[astx.Identifier(value="Exception")],
name=astx.Identifier(value="e"),
types=[astx.Identifier(name="Exception")],
name=astx.Identifier(name="e"),
body=body,
)
node = astx.ExceptionHandlerStmt(body=body, handlers=[handler])
Expand Down Expand Up @@ -1051,7 +1051,7 @@ def test_and_op_simple(self) -> None:

def test_aug_assign_add(self) -> None:
"""Test astx.AugAssign with addition."""
target = astx.Identifier(value="x")
target = astx.Identifier(name="x")
value = astx.LiteralInt32(value=5)
node = astx.AugAssign(target=target, value=value, op_code="+=")
result = self.transpiler.visit(node)
Expand Down Expand Up @@ -1226,7 +1226,7 @@ def test_block_simple(self) -> None:

def test_delete_stmt_simple(self) -> None:
"""Test astx.DeleteStmt with variable."""
target = astx.Identifier(value="x")
target = astx.Identifier(name="x")
node = astx.DeleteStmt(value=[target])
result = self.transpiler.visit(node)
assert isinstance(result, ast.Delete)
Expand All @@ -1247,7 +1247,7 @@ def test_ellipsis_simple(self) -> None:

def test_identifier_simple(self) -> None:
"""Test astx.Identifier with simple value."""
node = astx.Identifier(value="my_id")
node = astx.Identifier(name="my_id")
result = self.transpiler.visit(node)
assert isinstance(result, ast.Name)
assert result.id == "my_id"
Expand Down Expand Up @@ -1412,7 +1412,7 @@ def setup_method(self) -> None:

def test_assignment_expr_single_target(self) -> None:
"""Test astx.AssignmentExpr with single target."""
target = astx.Identifier(value="x")
target = astx.Identifier(name="x")
value = astx.LiteralInt32(value=42)
node = astx.AssignmentExpr(targets=[target], value=value)
result = self.transpiler.visit(node)
Expand Down
18 changes: 9 additions & 9 deletions libs/astx/src/astx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
base,
blocks,
callables,
data,
flows,
literals,
mixes,
packages,
symbol_table,
types,
variables,
)
from astx.base import (
AST,
Expand All @@ -22,7 +22,6 @@
DataType,
Expr,
ExprType,
Identifier,
OperatorType,
ParenthesizedExpr,
SourceLocation,
Expand Down Expand Up @@ -60,6 +59,13 @@
ListComprehension,
SetComprehension,
)
from astx.data import (
DeleteStmt,
Identifier,
InlineVariableDeclaration,
Variable,
VariableDeclaration,
)
from astx.exceptions import (
CatchHandlerStmt,
ExceptionHandlerStmt,
Expand Down Expand Up @@ -197,12 +203,6 @@
XnorOp,
XorOp,
)
from astx.variables import (
DeleteStmt,
InlineVariableDeclaration,
Variable,
VariableDeclaration,
)


def get_version() -> str:
Expand Down Expand Up @@ -375,6 +375,7 @@ def get_version() -> str:
"base",
"blocks",
"callables",
"data",
"datatypes",
"flows",
"get_version",
Expand All @@ -383,7 +384,6 @@ def get_version() -> str:
"packages",
"symbol_table",
"types",
"variables",
]


Expand Down
26 changes: 1 addition & 25 deletions libs/astx/src/astx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,29 +386,6 @@ class Expr(AST):
nbytes: int = 0


@public
@typechecked
class Identifier(Expr):
"""AST class for identifiers."""

value: str

def __init__(
self,
value: str,
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
"""Initialize the Identifier instance."""
super().__init__(loc=loc, parent=parent)
self.value = value

def get_struct(self, simplified: bool = False) -> ReprStruct:
"""Return a structure that represents the Identifier object."""
key = f"IDENTIFIER[{self.value}]"
return self._prepare_struct(key, self.value, simplified)


@public
@typechecked
class ExprType(Expr):
Expand Down Expand Up @@ -465,12 +442,11 @@ def __init__(
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
super().__init__(loc)
super().__init__(loc=loc, parent=parent)
self.name = f"temp_{DataType._tmp_id}"
DataType._tmp_id += 1
# set it as a generic data type
self.type_: ExprType = ExprType()
self.parent = parent

def __str__(self) -> str:
"""Return an string that represents the object."""
Expand Down
2 changes: 1 addition & 1 deletion libs/astx/src/astx/callables.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
Undefined,
)
from astx.blocks import Block
from astx.data import Variable
from astx.modifiers import MutabilityKind, ScopeKind, VisibilityKind
from astx.tools.typing import typechecked
from astx.types import AnyType
from astx.variables import Variable

UNDEFINED = Undefined()

Expand Down
2 changes: 1 addition & 1 deletion libs/astx/src/astx/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
)
from astx.blocks import Block
from astx.callables import FunctionDef
from astx.data import VariableDeclaration
from astx.modifiers import VisibilityKind
from astx.tools.typing import typechecked
from astx.variables import VariableDeclaration


@public
Expand Down
2 changes: 1 addition & 1 deletion libs/astx/src/astx/context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
ASTNodes,
DataTypesStruct,
Expr,
Identifier,
ReprStruct,
SourceLocation,
StatementType,
)
from astx.blocks import Block
from astx.data import Identifier


@public
Expand Down
37 changes: 26 additions & 11 deletions libs/astx/src/astx/variables.py → libs/astx/src/astx/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
ASTNodes,
DataType,
Expr,
Identifier,
ReprStruct,
SourceLocation,
StatementType,
Expand Down Expand Up @@ -120,33 +119,49 @@ def get_struct(self, simplified: bool = False) -> ReprStruct:

@public
@typechecked
class Variable(DataTypeOps):
"""AST class for the variable usage."""
class Identifier(DataTypeOps):
"""AST class for identifiers."""

name: str
type_: DataType = AnyType()

def __init__(
self,
name: str,
type_: DataType = AnyType(),
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
"""Initialize the Variable instance."""
"""Initialize the Identifier instance."""
super().__init__(loc=loc, parent=parent)
self.name = name
self.type_ = type_
# note: necessary for data operations
self.type_ = AnyType()

def __str__(self) -> str:
"""Return a string that represents the object."""
return f"Variable[{self.name}]"
return f"{self.__class__.__name__}[{self.name}]"

def get_struct(self, simplified: bool = False) -> ReprStruct:
"""Return the AST structure of the object."""
key = f"Variable[{self.name}]"
value = self.name
return self._prepare_struct(key, value, simplified)
"""Return a structure that represents the Identifier object."""
key = f"{self.__class__.__name__.upper()}[{self.name}]"
return self._prepare_struct(key, self.name, simplified)


@public
@typechecked
class Variable(Identifier):
"""AST class for the variable usage."""

def __init__(
self,
name: str,
type_: DataType = AnyType(),
loc: SourceLocation = NO_SOURCE_LOCATION,
parent: Optional[ASTNodes] = None,
) -> None:
"""Initialize the Variable instance."""
super().__init__(name=name, loc=loc, parent=parent)
self.type_ = type_


class DeleteStmt(StatementType):
Expand Down
2 changes: 1 addition & 1 deletion libs/astx/src/astx/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
ASTNodes,
DictDataTypesStruct,
Expr,
Identifier,
ReprStruct,
SourceLocation,
StatementType,
)
from astx.blocks import Block
from astx.data import Identifier
from astx.tools.typing import typechecked


Expand Down
7 changes: 3 additions & 4 deletions libs/astx/src/astx/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
ASTNodes,
DictDataTypesStruct,
Expr,
Identifier,
ReprStruct,
SourceLocation,
StatementType,
)
from astx.blocks import Block
from astx.data import Identifier, InlineVariableDeclaration
from astx.tools.typing import typechecked
from astx.variables import InlineVariableDeclaration


@public
Expand Down Expand Up @@ -734,11 +733,11 @@ def __init__(

def __str__(self) -> str:
"""Return a string representation of the object."""
return f"Goto[{self.label.value}]"
return f"Goto[{self.label.name}]"

def get_struct(self, simplified: bool = False) -> ReprStruct:
"""Return the AST structure of the object."""
key = f"GOTO-STMT[{self.label.value}]"
key = f"GOTO-STMT[{self.label.name}]"
value: DictDataTypesStruct = {}
return self._prepare_struct(key, value, simplified)

Expand Down
2 changes: 1 addition & 1 deletion libs/astx/src/astx/mixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from astx.base import DataType
from astx.callables import FunctionDef
from astx.variables import Variable
from astx.data import Variable

__all__ = ["NamedExpr"]

Expand Down
Loading
Loading