Skip to content
New issue

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

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

Already on GitHub? # to your account

fix[ux]: improve error message for bad hex literals #4244

Merged
merged 4 commits into from
Sep 19, 2024
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
3 changes: 3 additions & 0 deletions tests/unit/ast/nodes/test_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def foo():
"""
foo: constant(bytes4) = 0x12_34_56
""",
"""
foo: constant(bytes4) = 0X12345678
""",
]


Expand Down
5 changes: 5 additions & 0 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,15 @@ class Hex(Constant):

def validate(self):
if "_" in self.value:
# TODO: revisit this, we should probably allow underscores
raise InvalidLiteral("Underscores not allowed in hex literals", self)
if len(self.value) % 2:
raise InvalidLiteral("Hex notation requires an even number of digits", self)

if self.value.startswith("0X"):
hint = f"Did you mean `0x{self.value[2:]}`?"
raise InvalidLiteral("Hex literal begins with 0X!", self, hint=hint)

@property
def n_nibbles(self):
"""
Expand Down
Loading