Skip to content

Commit

Permalink
fix[ux]: fix empty hints in error messages (#4351)
Browse files Browse the repository at this point in the history
remove empty hint message from exceptions. this regression was
introduced in 03095ce. the root cause of the issue, however,
was that `hint=""` could be constructed in the first place. this commit
fixes the `get_levenshtein_error_suggestions` helper so that it returns
`None` on failure to find a suggestion rather than the empty string
`""`.

---------

Co-authored-by: Charles Cooper <[email protected]>
  • Loading branch information
sandbubbles and charles-cooper authored Nov 19, 2024
1 parent dbf9fa0 commit 7d54f32
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ def foo():

@pytest.mark.parametrize("bad_code", fail_list)
def test_undeclared_def_exception(bad_code):
with pytest.raises(UndeclaredDefinition):
with pytest.raises(UndeclaredDefinition) as e:
compiler.compile_code(bad_code)
assert "(hint: )" not in str(e.value)
15 changes: 15 additions & 0 deletions tests/functional/syntax/exceptions/test_unknown_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from vyper import compiler
from vyper.exceptions import UnknownType


def test_unknown_type_exception():
code = """
@internal
def foobar(token: IERC20):
pass
"""
with pytest.raises(UnknownType) as e:
compiler.compile_code(code)
assert "(hint: )" not in str(e.value)
8 changes: 4 additions & 4 deletions vyper/semantics/analysis/levenshtein_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable
from typing import Any, Callable, Optional


def levenshtein_norm(source: str, target: str) -> float:
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_levenshtein_error_suggestions(*args, **kwargs) -> Callable:

def _get_levenshtein_error_suggestions(
key: str, namespace: dict[str, Any], threshold: float
) -> str:
) -> Optional[str]:
"""
Generate an error message snippet for the suggested closest values in the provided namespace
with the shortest normalized Levenshtein distance from the given key if that distance
Expand All @@ -100,11 +100,11 @@ def _get_levenshtein_error_suggestions(
"""

if key is None or key == "":
return ""
return None

distances = sorted([(i, levenshtein_norm(key, i)) for i in namespace], key=lambda k: k[1])
if len(distances) > 0 and distances[0][1] <= threshold:
if len(distances) > 1 and distances[1][1] <= threshold:
return f"Did you mean '{distances[0][0]}', or maybe '{distances[1][0]}'?"
return f"Did you mean '{distances[0][0]}'?"
return ""
return None

0 comments on commit 7d54f32

Please sign in to comment.