Skip to content

Commit

Permalink
chore: improve error message for subscripted type validation (#3313)
Browse files Browse the repository at this point in the history
type annotations like `Bytestring` with no length annotation would fail
with an unhelpful error message
  • Loading branch information
tserg authored May 5, 2023
1 parent d62125c commit 9cc56b6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
26 changes: 0 additions & 26 deletions tests/parser/syntax/test_byte_string.py

This file was deleted.

16 changes: 15 additions & 1 deletion tests/parser/syntax/test_bytes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import pytest

from vyper import compiler
from vyper.exceptions import InvalidOperation, InvalidType, SyntaxException, TypeMismatch
from vyper.exceptions import (
InvalidOperation,
InvalidType,
StructureException,
SyntaxException,
TypeMismatch,
)

fail_list = [
(
Expand Down Expand Up @@ -77,6 +83,14 @@ def test() -> Bytes[1]:
""",
SyntaxException,
),
(
"""
@external
def foo():
a: Bytes = b"abc"
""",
StructureException,
),
]


Expand Down
8 changes: 8 additions & 0 deletions tests/parser/syntax/test_dynamic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
""",
StructureException,
),
(
"""
@external
def foo():
a: DynArray = [1, 2, 3]
""",
StructureException,
),
]


Expand Down
7 changes: 7 additions & 0 deletions tests/parser/syntax/test_invalids.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ def a():
UnknownAttribute,
)

must_fail(
"""
a: HashMap
""",
StructureException,
)


@pytest.mark.parametrize("bad_code,exception_type", fail_list)
def test_compilation_fails_with_exception(bad_code, exception_type):
Expand Down
23 changes: 23 additions & 0 deletions tests/parser/syntax/test_string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from vyper import compiler
from vyper.exceptions import StructureException

valid_list = [
"""
Expand All @@ -27,9 +28,31 @@ def foo() -> bool:
y: String[12] = "test"
return x != y
""",
"""
@external
def test() -> String[100]:
return "hello world!"
""",
]


@pytest.mark.parametrize("good_code", valid_list)
def test_string_success(good_code):
assert compiler.compile_code(good_code) is not None


invalid_list = [
(
"""
@external
def foo():
a: String = "abc"
""",
StructureException,
)
]


@pytest.mark.parametrize("bad_code,exc", invalid_list)
def test_string_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code, exc):
assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc)
9 changes: 8 additions & 1 deletion vyper/semantics/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ def _failwith(type_name):
if node.id not in namespace:
_failwith(node.node_source_code)

return namespace[node.id]
typ_ = namespace[node.id]
if hasattr(typ_, "from_annotation"):
# cases where the object in the namespace is an uninstantiated
# type object, ex. Bytestring or DynArray (with no length provided).
# call from_annotation to produce a better error message.
typ_.from_annotation(node)

return typ_


def get_index_value(node: vy_ast.Index) -> int:
Expand Down

0 comments on commit 9cc56b6

Please sign in to comment.