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

refactor[lang]: remove VyperNode __hash__() and __eq__() implementations #4433

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion tests/unit/ast/nodes/test_binary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from tests.ast_utils import deepequals
from vyper import ast as vy_ast
from vyper.exceptions import SyntaxException

Expand All @@ -18,7 +19,7 @@ def x():
"""
)

assert expected == mutated
assert deepequals(expected, mutated)


def test_binary_length():
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/ast/nodes/test_compare_nodes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tests.ast_utils import deepequals
from vyper import ast as vy_ast


Expand All @@ -6,33 +7,33 @@ def test_compare_different_node_clases():
left = vyper_ast.body[0].target
right = vyper_ast.body[0].value

assert left != right
assert not deepequals(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 deepequals(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 not deepequals(left, right)


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 deepequals(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 deepequals(node, node)
3 changes: 2 additions & 1 deletion tests/unit/ast/test_ast_dict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import json

from tests.ast_utils import deepequals
from vyper import compiler
from vyper.ast.nodes import NODE_SRC_ATTRIBUTES
from vyper.ast.parse import parse_to_ast
Expand Down Expand Up @@ -138,7 +139,7 @@ def test() -> int128:
new_dict = json.loads(out_json)
new_ast = dict_to_ast(new_dict)

assert new_ast == original_ast
assert deepequals(new_ast, original_ast)


# strip source annotations like lineno, we don't care for inspecting
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/ast/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tests.ast_utils import deepequals
from vyper.ast.parse import parse_to_ast


Expand All @@ -12,7 +13,7 @@ def test() -> int128:
ast1 = parse_to_ast(code)
ast2 = parse_to_ast("\n \n" + code + "\n\n")

assert ast1 == ast2
assert deepequals(ast1, ast2)


def test_ast_unequal():
Expand All @@ -32,4 +33,4 @@ def test() -> int128:
ast1 = parse_to_ast(code1)
ast2 = parse_to_ast(code2)

assert ast1 != ast2
assert not deepequals(ast1, ast2)
14 changes: 3 additions & 11 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,9 @@
# default implementation of deepcopy is a hotspot
return pickle.loads(pickle.dumps(self))

def __eq__(self, other):
# CMC 2024-03-03 I'm not sure it makes much sense to compare AST
# nodes, especially if they come from other modules
if not isinstance(other, type(self)):
return False
if getattr(other, "node_id", None) != getattr(self, "node_id", None):
return False
for field_name in (i for i in self.get_fields() if i not in VyperNode.__slots__):
if getattr(self, field_name, None) != getattr(other, field_name, None):
return False
return True
def __ne__(self, other):
Fixed Show fixed Hide fixed
# temporary to suss out all instances of != in tests
raise Exception()

def __repr__(self):
cls = type(self)
Expand Down
2 changes: 1 addition & 1 deletion vyper/semantics/analysis/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def push_path(self, module_ast: vy_ast.Module) -> None:

def pop_path(self, expected: vy_ast.Module) -> None:
popped = self._path.pop()
if expected != popped:
if expected is not popped: # FIXME - use expected != popped
raise CompilerPanic("unreachable")
self._imports.pop()

Expand Down
Loading