From aa6e05be0caf0d53e5dfc6de8f8853e7de02ba67 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 17 Sep 2024 10:11:49 -0400 Subject: [PATCH 1/2] fix[ux]: improve error message for bad hex literals --- tests/unit/ast/nodes/test_hex.py | 3 +++ vyper/ast/nodes.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/tests/unit/ast/nodes/test_hex.py b/tests/unit/ast/nodes/test_hex.py index 1b61764d57..25c70bf68f 100644 --- a/tests/unit/ast/nodes/test_hex.py +++ b/tests/unit/ast/nodes/test_hex.py @@ -33,6 +33,9 @@ def foo(): """ foo: constant(bytes4) = 0x12_34_56 """, + """ +foo: constant(bytes4) = 0X12345678 + """ ] diff --git a/vyper/ast/nodes.py b/vyper/ast/nodes.py index f4c99b7952..991edeca6e 100644 --- a/vyper/ast/nodes.py +++ b/vyper/ast/nodes.py @@ -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): """ From 6fd62d9b92a75129c652babec2697bc3cc86ccc4 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Wed, 18 Sep 2024 15:45:44 -0400 Subject: [PATCH 2/2] fix lint --- tests/unit/ast/nodes/test_hex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/ast/nodes/test_hex.py b/tests/unit/ast/nodes/test_hex.py index 25c70bf68f..7168defa99 100644 --- a/tests/unit/ast/nodes/test_hex.py +++ b/tests/unit/ast/nodes/test_hex.py @@ -35,7 +35,7 @@ def foo(): """, """ foo: constant(bytes4) = 0X12345678 - """ + """, ]