Skip to content

Commit

Permalink
chore: improve exception for type validation (#3759)
Browse files Browse the repository at this point in the history
change `InvalidType` error to more accurate `TypeMismatch`
  • Loading branch information
tserg authored Feb 8, 2024
1 parent 4ecd26b commit c6b29c7
Show file tree
Hide file tree
Showing 42 changed files with 163 additions and 180 deletions.
6 changes: 3 additions & 3 deletions tests/functional/builtins/codegen/test_minmax_value.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from vyper.exceptions import InvalidType, OverflowException
from vyper.exceptions import OverflowException, TypeMismatch
from vyper.semantics.types import DecimalT, IntegerT
from vyper.semantics.types.shortcuts import INT256_T, UINT256_T

Expand Down Expand Up @@ -39,12 +39,12 @@ def foo():
if typ == UINT256_T:
assert_compile_failed(lambda: get_contract(upper), OverflowException)
else:
assert_compile_failed(lambda: get_contract(upper), InvalidType)
assert_compile_failed(lambda: get_contract(upper), TypeMismatch)

if typ == INT256_T:
assert_compile_failed(lambda: get_contract(lower), OverflowException)
else:
assert_compile_failed(lambda: get_contract(lower), InvalidType)
assert_compile_failed(lambda: get_contract(lower), TypeMismatch)


@pytest.mark.parametrize("typ", [DecimalT()])
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/builtins/codegen/test_raw_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from vyper import compile_code
from vyper.builtins.functions import eip1167_bytecode
from vyper.exceptions import ArgumentException, InvalidType, StateAccessViolation
from vyper.exceptions import ArgumentException, StateAccessViolation, TypeMismatch

pytestmark = pytest.mark.usefixtures("memory_mocker")

Expand Down Expand Up @@ -628,7 +628,7 @@ def foo(_addr: address):
def foo(_addr: address):
raw_call(_addr, 256)
""",
InvalidType,
TypeMismatch,
),
]

Expand Down
6 changes: 3 additions & 3 deletions tests/functional/builtins/folding/test_abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hypothesis import strategies as st

from tests.utils import parse_and_fold
from vyper.exceptions import InvalidType
from vyper.exceptions import TypeMismatch


@pytest.mark.fuzzing
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_abs_upper_bound_folding(get_contract, a):
def foo(a: int256) -> int256:
return abs({a})
"""
with pytest.raises(InvalidType):
with pytest.raises(TypeMismatch):
get_contract(source)


Expand All @@ -56,5 +56,5 @@ def test_abs_lower_bound_folded(get_contract, tx_failed):
def foo() -> int256:
return abs(min_value(int256))
"""
with pytest.raises(InvalidType):
with pytest.raises(TypeMismatch):
get_contract(source)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from vyper.compiler import compile_code
from vyper.exceptions import (
InvalidLiteral,
InvalidType,
NonPayableViolation,
StateAccessViolation,
TypeMismatch,
UndeclaredDefinition,
)

Expand Down Expand Up @@ -404,31 +404,31 @@ def foo(xx: int128, y: int128 = xx): pass
@external
def foo(a: uint256 = -1): pass
""",
InvalidType,
TypeMismatch,
),
(
"""
# value out of range for int128
@external
def foo(a: int128 = 170141183460469231731687303715884105728): pass
""",
InvalidType,
TypeMismatch,
),
(
"""
# value out of range for uint256 array
@external
def foo(a: uint256[2] = [13, -42]): pass
""",
InvalidType,
TypeMismatch,
),
(
"""
# value out of range for int128 array
@external
def foo(a: int128[2] = [12, 170141183460469231731687303715884105728]): pass
""",
InvalidType,
TypeMismatch,
),
(
"""
Expand All @@ -444,7 +444,7 @@ def foo(a: uint256[2] = [12, True]): pass
@external
def foo(a: uint256[2] = [1, 2, 3]): pass
""",
InvalidType,
TypeMismatch,
),
(
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from vyper import compile_code
from vyper.exceptions import InvalidType
from vyper.exceptions import TypeMismatch

pytestmark = pytest.mark.usefixtures("memory_mocker")

Expand Down Expand Up @@ -159,5 +159,5 @@ def test_tuple_return_typecheck(tx_failed, get_contract_with_gas_estimation):
def getTimeAndBalance() -> (bool, address):
return block.timestamp, self.balance
"""
with pytest.raises(InvalidType):
with pytest.raises(TypeMismatch):
compile_code(code)
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def test_for() -> int128:
a = i
return a
""",
InvalidType,
TypeMismatch,
),
(
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/codegen/features/test_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def foo2() -> uint256:
x += 1
return x
"""
assert_compile_failed(lambda: get_contract_with_gas_estimation(code), InvalidType)
assert_compile_failed(lambda: get_contract_with_gas_estimation(code), TypeMismatch)


def test_invalid_uin256_assignment_calculate_literals(get_contract_with_gas_estimation):
Expand Down
14 changes: 7 additions & 7 deletions tests/functional/codegen/features/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def foo_():
log MyLog(b'yo')
"""

with tx_failed(InvalidType):
with tx_failed(TypeMismatch):
get_contract_with_gas_estimation(loggy_code)


Expand All @@ -580,7 +580,7 @@ def foo():
log MyLog(b'bars')
"""

with tx_failed(InvalidType):
with tx_failed(TypeMismatch):
get_contract_with_gas_estimation(loggy_code)


Expand Down Expand Up @@ -608,7 +608,7 @@ def foo():
log MyLog(b'bars')
"""

with tx_failed(InvalidType):
with tx_failed(TypeMismatch):
get_contract_with_gas_estimation(loggy_code)


Expand Down Expand Up @@ -1241,15 +1241,15 @@ def foo():
def foo():
raw_log([1, 2], b"moo")
""",
InvalidType,
TypeMismatch,
),
(
"""
@external
def foo():
raw_log([1, 2], b"moo")
""",
InvalidType,
TypeMismatch,
),
(
"""
Expand All @@ -1266,7 +1266,7 @@ def foo():
def foo():
raw_log([b"cow"], b"dog")
""",
(StructureException, InvalidType),
(StructureException, TypeMismatch),
),
(
"""
Expand All @@ -1275,7 +1275,7 @@ def foo():
# bytes20 instead of bytes32
raw_log([], 0x1234567890123456789012345678901234567890)
""",
InvalidType,
TypeMismatch,
),
]

Expand Down
13 changes: 9 additions & 4 deletions tests/functional/codegen/types/numbers/test_signed_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import pytest

from vyper import compile_code
from vyper.exceptions import InvalidOperation, InvalidType, OverflowException, ZeroDivisionException
from vyper.exceptions import (
InvalidOperation,
OverflowException,
TypeMismatch,
ZeroDivisionException,
)
from vyper.semantics.types import IntegerT
from vyper.utils import evm_div, evm_mod

Expand Down Expand Up @@ -214,7 +219,7 @@ def num_sub() -> {typ}:
return 1-2**{typ.bits}
"""

exc = OverflowException if typ.bits == 256 else InvalidType
exc = OverflowException if typ.bits == 256 else TypeMismatch
with pytest.raises(exc):
compile_code(code)

Expand Down Expand Up @@ -331,7 +336,7 @@ def foo() -> {typ}:
get_contract(code_2).foo(x)
with tx_failed():
get_contract(code_3).foo(y)
with pytest.raises((InvalidType, OverflowException)):
with pytest.raises((TypeMismatch, OverflowException)):
compile_code(code_4)


Expand Down Expand Up @@ -430,5 +435,5 @@ def test_binop_nested_intermediate_underflow():
def foo():
a: int256 = -2**255 * 2 - 10 + 100
"""
with pytest.raises(InvalidType):
with pytest.raises(TypeMismatch):
compile_code(code)
11 changes: 8 additions & 3 deletions tests/functional/codegen/types/numbers/test_unsigned_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import pytest

from vyper import compile_code
from vyper.exceptions import InvalidOperation, InvalidType, OverflowException, ZeroDivisionException
from vyper.exceptions import (
InvalidOperation,
OverflowException,
TypeMismatch,
ZeroDivisionException,
)
from vyper.semantics.types import IntegerT
from vyper.utils import SizeLimits, evm_div, evm_mod

Expand Down Expand Up @@ -164,7 +169,7 @@ def foo() -> {typ}:
get_contract(code_2).foo(x)
with tx_failed():
get_contract(code_3).foo(y)
with pytest.raises((InvalidType, OverflowException)):
with pytest.raises((TypeMismatch, OverflowException)):
get_contract(code_4)


Expand Down Expand Up @@ -223,7 +228,7 @@ def test() -> {typ}:

for val in bad_cases:
exc = (
InvalidType
TypeMismatch
if SizeLimits.MIN_INT256 <= val <= SizeLimits.MAX_UINT256
else OverflowException
)
Expand Down
8 changes: 4 additions & 4 deletions tests/functional/codegen/types/test_bytes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from vyper.exceptions import InvalidType, TypeMismatch
from vyper.exceptions import TypeMismatch


def test_test_bytes(get_contract_with_gas_estimation, tx_failed):
Expand Down Expand Up @@ -310,23 +310,23 @@ def assign():
def assign():
xs: bytes6 = b"abcdef"
""",
InvalidType,
TypeMismatch,
),
(
"""
@external
def assign():
xs: bytes4 = 0xabcdef # bytes3 literal
""",
InvalidType,
TypeMismatch,
),
(
"""
@external
def assign():
xs: bytes4 = 0x1234abcdef # bytes5 literal
""",
InvalidType,
TypeMismatch,
),
]

Expand Down
3 changes: 1 addition & 2 deletions tests/functional/codegen/types/test_dynamic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ArgumentException,
ArrayIndexException,
ImmutableViolation,
InvalidType,
OverflowException,
StateAccessViolation,
TypeMismatch,
Expand Down Expand Up @@ -1124,7 +1123,7 @@ def foo() -> DynArray[{subtyp}, 3]:
x.append({lit})
return x
"""
assert_compile_failed(lambda: get_contract(code), InvalidType)
assert_compile_failed(lambda: get_contract(code), TypeMismatch)


invalid_appends_pops = [
Expand Down
22 changes: 0 additions & 22 deletions tests/functional/syntax/exceptions/test_invalid_type_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,12 @@ def test_unknown_type_exception(bad_code, get_contract, assert_compile_failed):


invalid_list = [
"""
@external
def foo():
raw_log(b"cow", b"dog")
""",
"""
@external
def foo():
xs: uint256[1] = []
""",
# Must be a literal string.
"""
@external
def mint(_to: address, _value: uint256):
assert msg.sender == self,msg.sender
""",
# literal longer than event member
"""
event Foo:
message: String[1]
@external
def foo():
log Foo("abcd")
""",
# Raise reason must be string
"""
@external
Expand All @@ -58,10 +40,6 @@ def mint(_to: address, _value: uint256):
# Key of mapping must be a base type
"""
b: HashMap[(int128, decimal), int128]
""",
# Address literal must be checksummed
"""
a: constant(address) = 0x3cd751e6b0078be393132286c442345e5dc49699
""",
"""
x: String <= 33
Expand Down
Loading

0 comments on commit c6b29c7

Please sign in to comment.