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: improve error message for undeclared enum member #3264

Merged
merged 3 commits into from
Jan 31, 2023
Merged
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
13 changes: 13 additions & 0 deletions tests/parser/syntax/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
NamespaceCollision,
StructureException,
TypeMismatch,
UnknownAttribute,
)

fail_list = [
Expand Down Expand Up @@ -97,6 +98,18 @@ def foo():nonpayable
""",
EnumDeclarationException,
),
(
"""
enum Roles:
ADMIN
USER

@external
def foo() -> Roles:
return Roles.GUEST
""",
UnknownAttribute,
),
]


Expand Down
3 changes: 3 additions & 0 deletions vyper/semantics/types/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def __init__(self, name: str, members: dict) -> None:
# also conveniently checks well-formedness of the members namespace
self._helper = VyperType(members)

# set the name for exception handling in `get_member`
self._helper._id = name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit funky, basically a side effect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so that it can be handled by L300 here:

def get_member(self, key: str, node: vy_ast.VyperNode) -> "VyperType":
if key in self.members:
return self.members[key]
# special error message for types with no members
if not self.members:
raise StructureException(f"{self} instance does not have members", node)
suggestions_str = get_levenshtein_error_suggestions(key, self.members, 0.3)
raise UnknownAttribute(f"{self} has no member '{key}'. {suggestions_str}", node)
def __repr__(self):
return self._id


def get_type_member(self, key: str, node: vy_ast.VyperNode) -> "VyperType":
self._helper.get_member(key, node)
return self
Expand Down