diff --git a/tests/parser/syntax/test_byte_string.py b/tests/parser/syntax/test_byte_string.py deleted file mode 100644 index 90bbe197b0..0000000000 --- a/tests/parser/syntax/test_byte_string.py +++ /dev/null @@ -1,26 +0,0 @@ -import pytest - -from vyper import compiler - -valid_list = [ - """ -@external -def foo() -> String[10]: - return "badminton" - """, - """ -@external -def foo(): - x: String[11] = "¡très bien!" - """, - """ -@external -def test() -> String[100]: - return "hello world!" - """, -] - - -@pytest.mark.parametrize("good_code", valid_list) -def test_byte_string_success(good_code): - assert compiler.compile_code(good_code) is not None diff --git a/tests/parser/syntax/test_bytes.py b/tests/parser/syntax/test_bytes.py index d732ec3ea5..a7fb7e77ce 100644 --- a/tests/parser/syntax/test_bytes.py +++ b/tests/parser/syntax/test_bytes.py @@ -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 = [ ( @@ -77,6 +83,14 @@ def test() -> Bytes[1]: """, SyntaxException, ), + ( + """ +@external +def foo(): + a: Bytes = b"abc" + """, + StructureException, + ), ] diff --git a/tests/parser/syntax/test_dynamic_array.py b/tests/parser/syntax/test_dynamic_array.py index e7dc2d1183..0c23bf67da 100644 --- a/tests/parser/syntax/test_dynamic_array.py +++ b/tests/parser/syntax/test_dynamic_array.py @@ -16,6 +16,14 @@ """, StructureException, ), + ( + """ +@external +def foo(): + a: DynArray = [1, 2, 3] + """, + StructureException, + ), ] diff --git a/tests/parser/syntax/test_invalids.py b/tests/parser/syntax/test_invalids.py index 3c51075e60..33478fcff1 100644 --- a/tests/parser/syntax/test_invalids.py +++ b/tests/parser/syntax/test_invalids.py @@ -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): diff --git a/tests/parser/syntax/test_string.py b/tests/parser/syntax/test_string.py index 5b5b9e5bb5..6252011bd9 100644 --- a/tests/parser/syntax/test_string.py +++ b/tests/parser/syntax/test_string.py @@ -1,6 +1,7 @@ import pytest from vyper import compiler +from vyper.exceptions import StructureException valid_list = [ """ @@ -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) diff --git a/vyper/semantics/types/utils.py b/vyper/semantics/types/utils.py index 6ae677e451..1549684460 100644 --- a/vyper/semantics/types/utils.py +++ b/vyper/semantics/types/utils.py @@ -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: