Skip to content
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

feat: Add support for any datatype #147

Closed
wants to merge 2 commits into from
Closed
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: 2 additions & 0 deletions src/astx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
LambdaExpr,
)
from astx.datatypes import (
AnyType,
Boolean,
Complex,
Complex32,
Expand Down Expand Up @@ -237,6 +238,7 @@ def get_version() -> str:
"LiteralUTF8String",
"UTF8Char",
"UTF8String",
"AnyType",
]


Expand Down
1 change: 1 addition & 0 deletions src/astx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class ASTKind(Enum):
Decimal256DTKind = -621
UTF8CharDTKind = -622
UTF8StringDTKind = -623
AnyDTKind = -627

# imports(packages)
ImportStmtKind = -700
Expand Down
24 changes: 24 additions & 0 deletions src/astx/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,27 @@ def get_struct(self, simplified: bool = False) -> ReprStruct:
key = f"LiteralUTF8Char: {self.value}"
value = self.value
return self._prepare_struct(key, value, simplified)


@public
@typechecked
class AnyType(DataTypeOps):
"""Class representing the 'Any' data type, used generically."""

def __init__(self, loc: SourceLocation = NO_SOURCE_LOCATION) -> None:
"""Initialize the Any type with a source location."""
super().__init__()
self.loc = loc
self.kind = ASTKind.AnyDTKind
self.value = None

def __str__(self) -> str:
"""Return a simplified string representation for Any."""
return "Any"

def get_struct(self, simplified: bool = False) -> dict[str, str]:
"""Return the AST structure of the AnyType object."""
if simplified:
return {"key": "Any"}
else:
return {"type": "Any", "value": repr(self.value)}
5 changes: 5 additions & 0 deletions src/astx/transpilers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,8 @@ def visit(self, node: astx.WhileStmt) -> str:
condition = self.visit(node.condition)
body = self._generate_block(node.body)
return f"while {condition}:\n{body}"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.AnyType) -> str:
"""Handle AnyType nodes."""
return repr(node.value)
26 changes: 26 additions & 0 deletions tests/test_datatype_any.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests for Any data type."""

from astx.base import ASTKind
from astx.datatypes import AnyType


def test_any_type_creation() -> None:
"""Test basic creation of AnyType."""
any_type = AnyType()
assert isinstance(any_type, AnyType)
assert any_type.kind == ASTKind.AnyDTKind


def test_any_type_str_representation() -> None:
"""Test string representation of AnyType."""
any_type = AnyType()
assert str(any_type) == "Any"


def test_any_type_equality() -> None:
"""Test equality comparison between AnyType instances."""
any_type1 = AnyType()
any_type2 = AnyType()

# Same types should be equal
assert any_type1 == any_type2
21 changes: 21 additions & 0 deletions tests/transpilers/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,24 @@ def test_transpiler_ifstmt_without_else() -> None:
assert (
generated_code == expected_code
), f"Expected '{expected_code}', but got '{generated_code}'"


def test_transpiler_any_type() -> None:
"""Test the transpiler's handling of AnyType."""
# Create an AnyType instance
any_type_value = astx.AnyType()
any_type_value.value = None # The default value

# Initialize the transpiler
generator = astx2py.ASTxPythonTranspiler()

# Generate Python code
generated_code = generator.visit(any_type_value)

# Expected code
expected_code = repr(None) # Should be 'None'

# Assert that the generated code matches the expected output
assert (
generated_code == expected_code
), f"Generated code '{generated_code}' but expected '{expected_code}'"
Loading