Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore[ux]: improve module import hint #4260

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/functional/syntax/modules/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_counter() -> uint256:

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value.hint == expected_hint

Expand All @@ -49,7 +49,7 @@ def test_exports_no_uses_variable(make_input_bundle):

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value.hint == expected_hint

Expand Down
50 changes: 30 additions & 20 deletions tests/functional/syntax/modules/test_initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ def foo():
with pytest.raises(InitializerException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "module `lib1.vy` is used but never initialized!"
assert e.value._hint == "add `initializes: lib1` to the top level of your main contract"
assert (
e.value._hint == "add the line `initializes: lib1` to the top level of your main contract"
)


def test_initializer_no_references(make_input_bundle):
Expand Down Expand Up @@ -417,7 +419,7 @@ def foo():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -454,7 +456,7 @@ def __init__():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -495,7 +497,7 @@ def __init__():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -540,7 +542,7 @@ def __init__():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -575,7 +577,7 @@ def __init__():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -616,7 +618,7 @@ def __init__():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -660,7 +662,7 @@ def __init__():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -699,7 +701,7 @@ def foo():

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -738,7 +740,7 @@ def foo(new_value: uint256):

assert e.value._message == "Cannot access `lib2` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib2` or `initializes: lib2` as a "
expected_hint = "add the line `uses: lib2` or `initializes: lib2` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -780,7 +782,7 @@ def foo(new_value: uint256):

assert e.value._message == "Cannot access `lib2` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib2` or `initializes: lib2` as a "
expected_hint = "add the line `uses: lib2` or `initializes: lib2` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -823,7 +825,7 @@ def foo(new_value: uint256):

assert e.value._message == "Cannot access `lib2` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib2` or `initializes: lib2` as a "
expected_hint = "add the line `uses: lib2` or `initializes: lib2` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -857,7 +859,7 @@ def foo(new_value: uint256):

assert e.value._message == "Cannot access `lib1` state!" + NONREENTRANT_NOTE

expected_hint = "add `uses: lib1` or `initializes: lib1` as a "
expected_hint = "add the line `uses: lib1` or `initializes: lib1` as a "
expected_hint += "top-level statement to your contract"
assert e.value._hint == expected_hint

Expand Down Expand Up @@ -1122,7 +1124,10 @@ def __init__():
with pytest.raises(InitializerException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "tried to initialize `lib1`, but it is not in initializer list!"
assert e.value._hint == "add `initializes: lib1` as a top-level statement to your contract"
assert (
e.value._hint
== "add the line `initializes: lib1` as a top-level statement to your contract"
)


def test_init_uninitialized_function2(make_input_bundle):
Expand All @@ -1149,7 +1154,10 @@ def __init__():
with pytest.raises(InitializerException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "tried to initialize `lib1`, but it is not in initializer list!"
assert e.value._hint == "add `initializes: lib1` as a top-level statement to your contract"
assert (
e.value._hint
== "add the line `initializes: lib1` as a top-level statement to your contract"
)


def test_noinit_initialized_function(make_input_bundle):
Expand All @@ -1174,7 +1182,7 @@ def __init__():
with pytest.raises(InitializerException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "not initialized!"
assert e.value._hint == "add `lib1.__init__()` to your `__init__()` function"
assert e.value._hint == "add the line `lib1.__init__()` to your `__init__()` function"


def test_noinit_initialized_function2(make_input_bundle):
Expand All @@ -1197,7 +1205,7 @@ def __init__():
with pytest.raises(InitializerException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "not initialized!"
assert e.value._hint == "add `lib1.__init__()` to your `__init__()` function"
assert e.value._hint == "add the line `lib1.__init__()` to your `__init__()` function"


def test_ownership_decl_errors_not_swallowed(make_input_bundle):
Expand Down Expand Up @@ -1270,7 +1278,7 @@ def foo():
with pytest.raises(InitializerException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "`lib2` uses `lib3`, but it is not initialized with `lib3`"
assert e.value._hint == "add `lib3 := lib3` to its initializer list"
assert e.value._hint == "add the line `lib3 := lib3` to its initializer list"


def test_hint_for_missing_initializer_when_no_import(make_input_bundle, chdir_tmp_path):
Expand Down Expand Up @@ -1363,7 +1371,8 @@ def foo():
with pytest.raises(ImmutableViolation) as e:
compile_code(main, input_bundle=nonreentrant_library_bundle)
assert e.value._message == f"Cannot access `{lib}` state!" + NONREENTRANT_NOTE
hint = f"add `uses: {lib}` or `initializes: {lib}` as a top-level statement to your contract"
hint = f"add the line `uses: {lib}` or `initializes: {lib}` "
hint += "as a top-level statement to your contract"
assert e.value._hint == hint
assert e.value.annotations[0].lineno == 4

Expand All @@ -1381,7 +1390,8 @@ def foo():
compile_code(main, input_bundle=nonreentrant_library_bundle)
assert e.value._message == f"Cannot access `{lib}` state!" + NONREENTRANT_NOTE

hint = f"add `uses: {lib}` or `initializes: {lib}` as a top-level statement to your contract"
hint = f"add the line `uses: {lib}` or `initializes: {lib}` "
hint += "as a top-level statement to your contract"
assert e.value._hint == hint
assert e.value.annotations[0].lineno == 6

Expand Down
2 changes: 1 addition & 1 deletion vyper/semantics/analysis/global_.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _validate_global_initializes_constraint(module_t: ModuleT):
module_str = "the top level of your main contract"
else:
module_str = f"`{module_t}`"
hint = f"add `initializes: {found_module.alias}` to {module_str}"
hint = f"add the line `initializes: {found_module.alias}` to {module_str}"

err_list.append(InitializerException(msg, *uses, hint=hint))

Expand Down
2 changes: 1 addition & 1 deletion vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def check_module_uses(node: vy_ast.ExprNode) -> Optional[ModuleInfo]:
msg += " use of the `@nonreentrant` decorator is also considered"
msg += " state access"

hint = f"add `uses: {module_info.alias}` or "
hint = f"add the line `uses: {module_info.alias}` or "
hint += f"`initializes: {module_info.alias}` as "
hint += "a top-level statement to your contract"
raise ImmutableViolation(msg, hint=hint)
Expand Down
6 changes: 3 additions & 3 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def validate_initialized_modules(self):
if initialized_module.module_t not in should_initialize:
msg = f"tried to initialize `{initialized_module.alias}`, "
msg += "but it is not in initializer list!"
hint = f"add `initializes: {initialized_module.alias}` "
hint = f"add the line `initializes: {initialized_module.alias}` "
hint += "as a top-level statement to your contract"
raise InitializerException(msg, call_node.func, hint=hint)

Expand All @@ -378,7 +378,7 @@ def validate_initialized_modules(self):
err_list = ExceptionList()
for s in should_initialize.values():
msg = "not initialized!"
hint = f"add `{s.module_info.alias}.__init__()` to "
hint = f"add the line `{s.module_info.alias}.__init__()` to "
hint += "your `__init__()` function"

# grab the init function AST node for error message
Expand Down Expand Up @@ -520,7 +520,7 @@ def visit_InitializesDecl(self, node):
# it's `initializes: foo` instead of `initializes: foo[...]`
hint = f"did you mean {module_ref.id}[{lhs} := {rhs}]?"
else:
hint = f"add `{lhs} := {rhs}` to its initializer list"
hint = f"add the line `{lhs} := {rhs}` to its initializer list"
raise InitializerException(msg, node, hint=hint)

# note: try to refactor. not a huge fan of mutating the
Expand Down
Loading