Skip to content

Commit

Permalink
Add support for any datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
vikasgrewal16 committed Nov 24, 2024
1 parent 3893490 commit c3eddb9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
10 changes: 7 additions & 3 deletions src/astx/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,11 +795,15 @@ def __init__(self, loc: SourceLocation = NO_SOURCE_LOCATION) -> None:
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) -> ReprStruct:
"""Return the AST structure for the Any type."""
return self._prepare_struct("Any", None, simplified)
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)}
26 changes: 6 additions & 20 deletions tests/test_datatype_any.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
from typing import Dict, Any
"""Tests for Any data type."""

import pytest
from astx.base import ASTKind
from astx.datatypes import AnyType
from astx.base import ASTKind, NO_SOURCE_LOCATION


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_struct_representation() -> None:
"""Test struct representation of AnyType."""
any_type = AnyType()

# Test with simplified=False
struct = any_type.get_struct(simplified=False)
assert isinstance(struct, dict)
assert struct["type"] == "Any"
assert struct["value"] is None

# Test with simplified=True
simplified_struct = any_type.get_struct(simplified=True)
assert isinstance(simplified_struct, dict)
assert simplified_struct["type"] == "Any"
assert simplified_struct["value"] is None

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
assert any_type1 == any_type2
7 changes: 4 additions & 3 deletions tests/transpilers/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,17 +757,18 @@ def test_transpiler_ifstmt_without_else() -> None:

def test_transpiler_any_type() -> None:
"""Test the transpiler's handling of AnyType."""
# Create an AnyType instance with default source location
# 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 - "Any" representation
expected_code = "Any"
# Expected code
expected_code = repr(None) # Should be 'None'

# Assert that the generated code matches the expected output
assert (
Expand Down

0 comments on commit c3eddb9

Please sign in to comment.