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

fix[ux]: fix empty hints in error messages #4351

Merged
merged 12 commits into from
Nov 19, 2024
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
Loading