Skip to content

Commit

Permalink
feat[tool]: improvements to AST annotation (#3829)
Browse files Browse the repository at this point in the history
this commit enriches the annotated AST output so that links between
nodes are explicit and consumers of the AST do not need to "guess" about
relationships.

- link to the type declaration node for user types
- add `sha256sum` field for `Module` nodes
- add fields on `Import*` nodes so that they can be linked directly to
  `Module` nodes (including `sha256sum`, so that changes in dependencies
  can be detected)
- add improved type information (including parsed out metadata, like
  array length) consistently across all types.

misc/refactors:
- add `to_dict()` for type objects
- removed some dead code - `compare_nodes` was only used in a couple of
  tests, and node equality could be used there instead. (node equality
  is not super well-defined, but we could revisit that later).
  • Loading branch information
charles-cooper authored Mar 9, 2024
1 parent cf37ec2 commit 9428196
Show file tree
Hide file tree
Showing 20 changed files with 1,448 additions and 242 deletions.
2 changes: 1 addition & 1 deletion tests/unit/ast/nodes/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def x():
"""
)

assert vy_ast.compare_nodes(expected, mutated)
assert expected == mutated


def test_binary_length():
Expand Down
14 changes: 5 additions & 9 deletions tests/unit/ast/nodes/test_compare_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,32 @@ def test_compare_different_node_clases():
right = vyper_ast.body[0].value

assert left != right
assert not vy_ast.compare_nodes(left, right)


def test_compare_different_nodes_same_class():
vyper_ast = vy_ast.parse_to_ast("[1, 2]")
left, right = vyper_ast.body[0].value.elements

assert left != right
assert not vy_ast.compare_nodes(left, right)


def test_compare_different_nodes_same_value():
vyper_ast = vy_ast.parse_to_ast("[1, 1]")
left, right = vyper_ast.body[0].value.elements

assert left != right
assert vy_ast.compare_nodes(left, right)


def test_compare_complex_nodes_same_value():
vyper_ast = vy_ast.parse_to_ast("[{'foo':'bar', 43:[1,2,3]}, {'foo':'bar', 43:[1,2,3]}]")
left, right = vyper_ast.body[0].value.elements
def test_compare_similar_node():
# test equality without node_ids
left = vy_ast.Int(value=1)
right = vy_ast.Int(value=1)

assert left != right
assert vy_ast.compare_nodes(left, right)
assert left == right


def test_compare_same_node():
vyper_ast = vy_ast.parse_to_ast("42")
node = vyper_ast.body[0].value

assert node == node
assert vy_ast.compare_nodes(node, node)
7 changes: 0 additions & 7 deletions tests/unit/ast/nodes/test_from_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ def test_kwargs():
assert new_node.value == 666


def test_compare_nodes():
old_node = vy_ast.parse_to_ast("foo = 42")
new_node = vy_ast.Int.from_node(old_node, value=666)

assert not vy_ast.compare_nodes(old_node, new_node)


def test_new_node_has_no_parent():
old_node = vy_ast.parse_to_ast("foo = 42")
new_node = vy_ast.Int.from_node(old_node, value=666)
Expand Down
Loading

0 comments on commit 9428196

Please sign in to comment.