From 1f9be956a6608f926a5ee402605c630e38871568 Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Wed, 27 Dec 2023 12:31:18 +0800 Subject: [PATCH 1/3] fix minmax folded node --- vyper/builtins/functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vyper/builtins/functions.py b/vyper/builtins/functions.py index 718ad806c9..a924a56010 100644 --- a/vyper/builtins/functions.py +++ b/vyper/builtins/functions.py @@ -2020,7 +2020,7 @@ def fold(self, node): raise TypeMismatch("Cannot perform action between dislike numeric types", node) value = self._eval_fn(left.value, right.value) - return type(node.args[0]).from_node(node, value=value) + return type(left).from_node(node, value=value) def fetch_call_return(self, node): self._validate_arg_types(node) From 81d1bfb07cc3a68028275cbbb3bb1ca5ced5f66f Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Wed, 27 Dec 2023 12:31:23 +0800 Subject: [PATCH 2/3] improve tests --- tests/functional/syntax/test_abs.py | 4 ++++ tests/functional/syntax/test_as_wei_value.py | 4 ++++ tests/functional/syntax/test_ceil.py | 1 + tests/functional/syntax/test_floor.py | 1 + tests/functional/syntax/test_len.py | 1 + tests/functional/syntax/test_minmax.py | 8 ++++++++ tests/functional/syntax/test_minmax_value.py | 4 ++++ tests/functional/syntax/test_powmod.py | 4 ++++ tests/functional/syntax/test_uint2str.py | 4 ++++ 9 files changed, 31 insertions(+) diff --git a/tests/functional/syntax/test_abs.py b/tests/functional/syntax/test_abs.py index cb231a33b1..28ac8bc769 100644 --- a/tests/functional/syntax/test_abs.py +++ b/tests/functional/syntax/test_abs.py @@ -26,6 +26,10 @@ def test_abs_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_c """ FOO: constant(int256) = -3 BAR: constant(int256) = abs(FOO) + +@external +def foo(): + a: int256 = BAR """ ] diff --git a/tests/functional/syntax/test_as_wei_value.py b/tests/functional/syntax/test_as_wei_value.py index dc72c55242..6ec01a3214 100644 --- a/tests/functional/syntax/test_as_wei_value.py +++ b/tests/functional/syntax/test_as_wei_value.py @@ -114,6 +114,10 @@ def foo() -> uint256: """ y: constant(String[5]) = "szabo" x: constant(uint256) = as_wei_value(5, y) + +@external +def foo(): + a: uint256 = x """, ] diff --git a/tests/functional/syntax/test_ceil.py b/tests/functional/syntax/test_ceil.py index 53608ebea4..f9a4f3bbe2 100644 --- a/tests/functional/syntax/test_ceil.py +++ b/tests/functional/syntax/test_ceil.py @@ -6,6 +6,7 @@ """ BAR: constant(decimal) = 2.5 FOO: constant(int256) = ceil(BAR) + @external def foo(): a: int256 = FOO diff --git a/tests/functional/syntax/test_floor.py b/tests/functional/syntax/test_floor.py index 7b7f960a86..6f3abf1dd0 100644 --- a/tests/functional/syntax/test_floor.py +++ b/tests/functional/syntax/test_floor.py @@ -6,6 +6,7 @@ """ BAR: constant(decimal) = 2.5 FOO: constant(int256) = floor(BAR) + @external def foo(): a: int256 = FOO diff --git a/tests/functional/syntax/test_len.py b/tests/functional/syntax/test_len.py index 6867ceda63..449e89fe56 100644 --- a/tests/functional/syntax/test_len.py +++ b/tests/functional/syntax/test_len.py @@ -42,6 +42,7 @@ def foo(inp: String[10]) -> uint256: """ BAR: constant(String[5]) = "vyper" FOO: constant(uint256) = len(BAR) + @external def foo() -> uint256: a: uint256 = FOO diff --git a/tests/functional/syntax/test_minmax.py b/tests/functional/syntax/test_minmax.py index 6565974f19..c1129f7251 100644 --- a/tests/functional/syntax/test_minmax.py +++ b/tests/functional/syntax/test_minmax.py @@ -41,11 +41,19 @@ def test_block_fail(assert_compile_failed, get_contract_with_gas_estimation, bad FOO: constant(uint256) = 123 BAR: constant(uint256) = 456 BAZ: constant(uint256) = min(FOO, BAR) + +@external +def foo(): + a: uint256 = BAZ """, """ FOO: constant(uint256) = 123 BAR: constant(uint256) = 456 BAZ: constant(uint256) = max(FOO, BAR) + +@external +def foo(): + a: uint256 = BAZ """, ] diff --git a/tests/functional/syntax/test_minmax_value.py b/tests/functional/syntax/test_minmax_value.py index 14b61e2f0a..4249d8419b 100644 --- a/tests/functional/syntax/test_minmax_value.py +++ b/tests/functional/syntax/test_minmax_value.py @@ -15,6 +15,10 @@ def foo(): """, """ FOO: constant(address) = min_value(address) + +@external +def foo(): + a: address = FOO """, ] diff --git a/tests/functional/syntax/test_powmod.py b/tests/functional/syntax/test_powmod.py index fce692a74a..a86039ca4a 100644 --- a/tests/functional/syntax/test_powmod.py +++ b/tests/functional/syntax/test_powmod.py @@ -25,6 +25,10 @@ def test_powmod_fail(assert_compile_failed, get_contract_with_gas_estimation, ba FOO: constant(uint256) = 3 BAR: constant(uint256) = 5 BAZ: constant(uint256) = pow_mod256(FOO, BAR) + +@external +def foo(): + a: uint256 = BAZ """ ] diff --git a/tests/functional/syntax/test_uint2str.py b/tests/functional/syntax/test_uint2str.py index 392b7b952b..90e929421c 100644 --- a/tests/functional/syntax/test_uint2str.py +++ b/tests/functional/syntax/test_uint2str.py @@ -6,6 +6,10 @@ """ FOO: constant(uint256) = 3 BAR: constant(String[78]) = uint2str(FOO) + +@external +def foo(): + a: String[78] = BAR """ ] From 3736bf525b46b8d8d9bcc22abff5e778026afb04 Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Wed, 27 Dec 2023 12:43:27 +0800 Subject: [PATCH 3/3] use pytest.raises instead of assert_compile_failed --- .../exceptions/test_argument_exception.py | 4 ++-- tests/functional/syntax/test_abs.py | 9 ++++---- tests/functional/syntax/test_addmulmod.py | 4 ++-- tests/functional/syntax/test_as_wei_value.py | 6 +++-- tests/functional/syntax/test_ceil.py | 2 +- tests/functional/syntax/test_dynamic_array.py | 9 ++++---- tests/functional/syntax/test_epsilon.py | 15 ++++++++---- tests/functional/syntax/test_floor.py | 2 +- tests/functional/syntax/test_len.py | 13 +++++------ tests/functional/syntax/test_method_id.py | 9 ++++---- tests/functional/syntax/test_minmax.py | 9 ++++---- tests/functional/syntax/test_minmax_value.py | 23 ++++++++++++++----- tests/functional/syntax/test_powmod.py | 9 ++++---- tests/functional/syntax/test_raw_call.py | 6 ++--- tests/functional/syntax/test_ternary.py | 2 +- tests/functional/syntax/test_uint2str.py | 4 ++-- tests/functional/syntax/test_unary.py | 2 +- 17 files changed, 75 insertions(+), 53 deletions(-) diff --git a/tests/functional/syntax/exceptions/test_argument_exception.py b/tests/functional/syntax/exceptions/test_argument_exception.py index 939cc7104b..0b7ec21bdb 100644 --- a/tests/functional/syntax/exceptions/test_argument_exception.py +++ b/tests/functional/syntax/exceptions/test_argument_exception.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import ArgumentException fail_list = [ @@ -95,4 +95,4 @@ def foo(): @pytest.mark.parametrize("bad_code", fail_list) def test_function_declaration_exception(bad_code): with pytest.raises(ArgumentException): - compiler.compile_code(bad_code) + compile_code(bad_code) diff --git a/tests/functional/syntax/test_abs.py b/tests/functional/syntax/test_abs.py index 28ac8bc769..0841ff05d6 100644 --- a/tests/functional/syntax/test_abs.py +++ b/tests/functional/syntax/test_abs.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import InvalidType fail_list = [ @@ -18,8 +18,9 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) -def test_abs_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code, exc): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc) +def test_abs_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) valid_list = [ @@ -36,4 +37,4 @@ def foo(): @pytest.mark.parametrize("code", valid_list) def test_abs_pass(code): - assert compiler.compile_code(code) is not None + assert compile_code(code) is not None diff --git a/tests/functional/syntax/test_addmulmod.py b/tests/functional/syntax/test_addmulmod.py index 7be87ddc97..17c7b3ab8c 100644 --- a/tests/functional/syntax/test_addmulmod.py +++ b/tests/functional/syntax/test_addmulmod.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import InvalidType fail_list = [ @@ -46,4 +46,4 @@ def test_add_mod_fail(assert_compile_failed, get_contract, code, exc): @pytest.mark.parametrize("code", valid_list) def test_addmulmod_pass(code): - assert compiler.compile_code(code) is not None + assert compile_code(code) is not None diff --git a/tests/functional/syntax/test_as_wei_value.py b/tests/functional/syntax/test_as_wei_value.py index 6ec01a3214..a68f30424e 100644 --- a/tests/functional/syntax/test_as_wei_value.py +++ b/tests/functional/syntax/test_as_wei_value.py @@ -1,5 +1,6 @@ import pytest +from vyper import compile_code from vyper.exceptions import ( ArgumentException, InvalidLiteral, @@ -84,8 +85,9 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) -def test_as_wei_fail(get_contract_with_gas_estimation, bad_code, exc, assert_compile_failed): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc) +def test_as_wei_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) valid_list = [ diff --git a/tests/functional/syntax/test_ceil.py b/tests/functional/syntax/test_ceil.py index f9a4f3bbe2..41f4175d01 100644 --- a/tests/functional/syntax/test_ceil.py +++ b/tests/functional/syntax/test_ceil.py @@ -1,6 +1,6 @@ import pytest -from vyper.compiler import compile_code +from vyper import compile_code valid_list = [ """ diff --git a/tests/functional/syntax/test_dynamic_array.py b/tests/functional/syntax/test_dynamic_array.py index ae4a55c124..f566a80625 100644 --- a/tests/functional/syntax/test_dynamic_array.py +++ b/tests/functional/syntax/test_dynamic_array.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import StructureException fail_list = [ @@ -36,8 +36,9 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) -def test_block_fail(assert_compile_failed, get_contract, bad_code, exc): - assert_compile_failed(lambda: get_contract(bad_code), exc) +def test_block_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) valid_list = [ @@ -56,4 +57,4 @@ def test_block_fail(assert_compile_failed, get_contract, bad_code, exc): @pytest.mark.parametrize("good_code", valid_list) def test_dynarray_pass(good_code): - assert compiler.compile_code(good_code) is not None + assert compile_code(good_code) is not None diff --git a/tests/functional/syntax/test_epsilon.py b/tests/functional/syntax/test_epsilon.py index 73369d4b8a..8fa5bc70ca 100644 --- a/tests/functional/syntax/test_epsilon.py +++ b/tests/functional/syntax/test_epsilon.py @@ -1,14 +1,19 @@ import pytest +from vyper import compile_code from vyper.exceptions import InvalidType fail_list = [ - """ + ( + """ FOO: constant(address) = epsilon(address) - """ + """, + InvalidType, + ) ] -@pytest.mark.parametrize("bad_code", fail_list) -def test_block_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), InvalidType) +@pytest.mark.parametrize("bad_code,exc", fail_list) +def test_block_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) diff --git a/tests/functional/syntax/test_floor.py b/tests/functional/syntax/test_floor.py index 6f3abf1dd0..5c30aecbe1 100644 --- a/tests/functional/syntax/test_floor.py +++ b/tests/functional/syntax/test_floor.py @@ -1,6 +1,6 @@ import pytest -from vyper.compiler import compile_code +from vyper import compile_code valid_list = [ """ diff --git a/tests/functional/syntax/test_len.py b/tests/functional/syntax/test_len.py index 449e89fe56..b8cc61df1d 100644 --- a/tests/functional/syntax/test_len.py +++ b/tests/functional/syntax/test_len.py @@ -1,7 +1,6 @@ import pytest -from pytest import raises -from vyper import compiler +from vyper import compile_code from vyper.exceptions import TypeMismatch fail_list = [ @@ -21,11 +20,11 @@ def foo(inp: int128) -> uint256: @pytest.mark.parametrize("bad_code", fail_list) def test_block_fail(bad_code): if isinstance(bad_code, tuple): - with raises(bad_code[1]): - compiler.compile_code(bad_code[0]) + with pytest.raises(bad_code[1]): + compile_code(bad_code[0]) else: - with raises(TypeMismatch): - compiler.compile_code(bad_code) + with pytest.raises(TypeMismatch): + compile_code(bad_code) valid_list = [ @@ -53,4 +52,4 @@ def foo() -> uint256: @pytest.mark.parametrize("good_code", valid_list) def test_list_success(good_code): - assert compiler.compile_code(good_code) is not None + assert compile_code(good_code) is not None diff --git a/tests/functional/syntax/test_method_id.py b/tests/functional/syntax/test_method_id.py index da67e67fe3..849c1b0d55 100644 --- a/tests/functional/syntax/test_method_id.py +++ b/tests/functional/syntax/test_method_id.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import InvalidLiteral, InvalidType fail_list = [ @@ -28,8 +28,9 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) -def test_method_id_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code, exc): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc) +def test_method_id_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) valid_list = [ @@ -46,4 +47,4 @@ def foo(a: Bytes[4] = BAR): @pytest.mark.parametrize("code", valid_list) def test_method_id_pass(code): - assert compiler.compile_code(code) is not None + assert compile_code(code) is not None diff --git a/tests/functional/syntax/test_minmax.py b/tests/functional/syntax/test_minmax.py index c1129f7251..78ee74635c 100644 --- a/tests/functional/syntax/test_minmax.py +++ b/tests/functional/syntax/test_minmax.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import InvalidType, OverflowException, TypeMismatch fail_list = [ @@ -32,8 +32,9 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) -def test_block_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code, exc): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc) +def test_block_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) valid_list = [ @@ -60,4 +61,4 @@ def foo(): @pytest.mark.parametrize("good_code", valid_list) def test_block_success(good_code): - assert compiler.compile_code(good_code) is not None + assert compile_code(good_code) is not None diff --git a/tests/functional/syntax/test_minmax_value.py b/tests/functional/syntax/test_minmax_value.py index 4249d8419b..8cc3370b42 100644 --- a/tests/functional/syntax/test_minmax_value.py +++ b/tests/functional/syntax/test_minmax_value.py @@ -1,28 +1,39 @@ import pytest +from vyper import compile_code from vyper.exceptions import InvalidType fail_list = [ - """ + ( + """ @external def foo(): a: address = min_value(address) """, - """ + InvalidType, + ), + ( + """ @external def foo(): a: address = max_value(address) """, - """ + InvalidType, + ), + ( + """ FOO: constant(address) = min_value(address) @external def foo(): a: address = FOO """, + InvalidType, + ), ] -@pytest.mark.parametrize("bad_code", fail_list) -def test_block_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), InvalidType) +@pytest.mark.parametrize("bad_code,exc", fail_list) +def test_block_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) diff --git a/tests/functional/syntax/test_powmod.py b/tests/functional/syntax/test_powmod.py index a86039ca4a..12ea23152c 100644 --- a/tests/functional/syntax/test_powmod.py +++ b/tests/functional/syntax/test_powmod.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import InvalidType fail_list = [ @@ -16,8 +16,9 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) -def test_powmod_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code, exc): - assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc) +def test_powmod_fail(bad_code, exc): + with pytest.raises(exc): + compile_code(bad_code) valid_list = [ @@ -35,4 +36,4 @@ def foo(): @pytest.mark.parametrize("code", valid_list) def test_powmod_pass(code): - assert compiler.compile_code(code) is not None + assert compile_code(code) is not None diff --git a/tests/functional/syntax/test_raw_call.py b/tests/functional/syntax/test_raw_call.py index 4ff058ba40..c0b38d1d1e 100644 --- a/tests/functional/syntax/test_raw_call.py +++ b/tests/functional/syntax/test_raw_call.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code from vyper.exceptions import ArgumentException, InvalidType, SyntaxException, TypeMismatch fail_list = [ @@ -39,7 +39,7 @@ def foo(): @pytest.mark.parametrize("bad_code,exc", fail_list) def test_raw_call_fail(bad_code, exc): with pytest.raises(exc): - compiler.compile_code(bad_code) + compile_code(bad_code) valid_list = [ @@ -109,4 +109,4 @@ def foo(): @pytest.mark.parametrize("good_code", valid_list) def test_raw_call_success(good_code): - assert compiler.compile_code(good_code) is not None + assert compile_code(good_code) is not None diff --git a/tests/functional/syntax/test_ternary.py b/tests/functional/syntax/test_ternary.py index 3573648368..6a2bb9c072 100644 --- a/tests/functional/syntax/test_ternary.py +++ b/tests/functional/syntax/test_ternary.py @@ -1,6 +1,6 @@ import pytest -from vyper.compiler import compile_code +from vyper import compile_code from vyper.exceptions import InvalidType, TypeMismatch good_list = [ diff --git a/tests/functional/syntax/test_uint2str.py b/tests/functional/syntax/test_uint2str.py index 90e929421c..9e6dde30cc 100644 --- a/tests/functional/syntax/test_uint2str.py +++ b/tests/functional/syntax/test_uint2str.py @@ -1,6 +1,6 @@ import pytest -from vyper import compiler +from vyper import compile_code valid_list = [ """ @@ -16,4 +16,4 @@ def foo(): @pytest.mark.parametrize("code", valid_list) def test_addmulmod_pass(code): - assert compiler.compile_code(code) is not None + assert compile_code(code) is not None diff --git a/tests/functional/syntax/test_unary.py b/tests/functional/syntax/test_unary.py index c19e5ba5f0..5942ee15db 100644 --- a/tests/functional/syntax/test_unary.py +++ b/tests/functional/syntax/test_unary.py @@ -1,6 +1,6 @@ import pytest -from vyper.compiler import compile_code +from vyper import compile_code from vyper.exceptions import InvalidType fail_list = [