Skip to content

Commit

Permalink
fix: error message for kwargs at call site (#2798)
Browse files Browse the repository at this point in the history
Throw an ArgumentException error during validation of kwargs other than
gas, value and skip_contract_check.
  • Loading branch information
tserg authored Apr 20, 2022
1 parent c270363 commit ff6ddce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,14 @@ def bar() -> bool: view
def foo(a: address):
Bar(a).bar(1)
""",
"""
interface Bar:
def bar(x: uint256, y: uint256) -> uint256: view
@external
def foo(a: address, x: uint256, y: uint256):
Bar(a).bar(x, y=y)
""",
]


Expand Down
9 changes: 9 additions & 0 deletions tests/parser/features/test_internal_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,15 @@ def bar(a: int128) -> int128:
def foo() -> int128:
return self.bar(1, 2)
""",
"""
@internal
def _foo(x: uint256, y: uint256 = 1):
pass
@external
def foo(x: uint256, y: uint256):
self._foo(x, y=y)
""",
]


Expand Down
21 changes: 20 additions & 1 deletion vyper/semantics/types/function.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import warnings
from collections import OrderedDict
from typing import Any, Dict, List, Optional, Tuple
Expand Down Expand Up @@ -469,7 +470,25 @@ def fetch_call_return(self, node: vy_ast.Call) -> Optional[BaseTypeDefinition]:
if not isinstance(kwarg.value, vy_ast.NameConstant):
raise InvalidType("skip_contract_check must be literal bool", kwarg.value)
else:
validate_expected_type(kwarg.arg, kwarg.value)
# Generate the modified source code string with the kwarg removed
# as a suggestion to the user.
kwarg_pattern = fr"{kwarg.arg}\s*=\s*{re.escape(kwarg.value.node_source_code)}"
modified_line = re.sub(
kwarg_pattern, kwarg.value.node_source_code, node.node_source_code
)
error_suggestion = (
f"\n(hint: Try removing the kwarg: `{modified_line}`)"
if modified_line != node.node_source_code
else ""
)

raise ArgumentException(
(
"Usage of kwarg in Vyper is restricted to gas=, "
f"value= and skip_contract_check=. {error_suggestion}"
),
kwarg,
)

return self.return_type

Expand Down

0 comments on commit ff6ddce

Please sign in to comment.