From 3d7d317b810723729446586dbf1cadc24e5d03d8 Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Fri, 17 Nov 2023 19:07:48 +0800 Subject: [PATCH] improve error msg for as_wei_value --- tests/functional/syntax/test_as_wei_value.py | 24 +++++++++++++++++++- vyper/builtins/functions.py | 9 +++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/functional/syntax/test_as_wei_value.py b/tests/functional/syntax/test_as_wei_value.py index cbae262318..fd5f9e20ba 100644 --- a/tests/functional/syntax/test_as_wei_value.py +++ b/tests/functional/syntax/test_as_wei_value.py @@ -1,11 +1,25 @@ import pytest -from vyper.exceptions import ArgumentException, InvalidType, OverflowException, StructureException +from vyper.exceptions import ( + ArgumentException, + InvalidLiteral, + InvalidType, + OverflowException, + StructureException, +) fail_list = [ ( """ @external +def foo(): + x: uint256 = as_wei_value(5, szabo) + """, + ArgumentException, + ), + ( + """ +@external def foo(): x: uint256 = as_wei_value(5, "szaboo") """, @@ -39,6 +53,14 @@ def foo() -> uint256: """, OverflowException, ), + ( + """ +@external +def foo(): + x: uint256 = as_wei_value(-1, "szabo") + """, + InvalidLiteral, + ), ] diff --git a/vyper/builtins/functions.py b/vyper/builtins/functions.py index 0fbef45894..2aea650ea0 100644 --- a/vyper/builtins/functions.py +++ b/vyper/builtins/functions.py @@ -977,12 +977,16 @@ class AsWeiValue(BuiltinFunction): ("kether", "grand"): 10**21, } - def get_denomination(self, node): + def _get_denomination_node(self, node): value = node.args[1]._metadata.get("folded_value") if not isinstance(value, vy_ast.Str): raise ArgumentException( "Wei denomination must be given as a literal string", node.args[1] ) + return value + + def get_denomination(self, node): + value = self._get_denomination_node(node) try: denom = next(v for k, v in self.wei_denoms.items() if value.value in k) except StopIteration: @@ -1009,6 +1013,9 @@ def fetch_call_return(self, node): return self._return_type def infer_arg_types(self, node, expected_return_typ=None): + # raise a better error message by first calling this function + # for its side effects of checking the denom + self._get_denomination_node(node) self._validate_arg_types(node) # return a concrete type instead of abstract type value_type = get_possible_types_from_node(node.args[0]).pop()