Skip to content

Commit 73a3228

Browse files
move AnnAssign validation to vyper/ast/nodes.py
1 parent 7ab359c commit 73a3228

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

vyper/ast/nodes.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,16 @@ def __init__(self, *args, **kwargs):
12611261

12621262

12631263
class AnnAssign(VyperNode):
1264-
__slots__ = ("target", "annotation", "value", "simple")
1264+
__slots__ = ("target", "annotation", "value")
1265+
1266+
def validate(self):
1267+
if not isinstance(self.target, Name):
1268+
raise VariableDeclarationException("Invalid variable declaration", self.target)
1269+
1270+
if self.value is None:
1271+
raise VariableDeclarationException(
1272+
"Local variables must be declared with an initial value", self
1273+
)
12651274

12661275

12671276
class VariableDecl(VyperNode):

vyper/ast/nodes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Assign(VyperNode): ...
208208

209209
class AnnAssign(VyperNode):
210210
target: Name = ...
211-
value: VyperNode = ...
211+
value: ExprNode = ...
212212
annotation: VyperNode = ...
213213

214214
class VariableDecl(VyperNode):

vyper/semantics/analysis/local.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
StructureException,
1818
TypeCheckFailure,
1919
TypeMismatch,
20-
VariableDeclarationException,
2120
VyperException,
2221
)
2322
from vyper.semantics.analysis.base import (
@@ -326,14 +325,11 @@ def visit(self, node):
326325
super().visit(node)
327326

328327
def visit_AnnAssign(self, node):
329-
name = node.get("target.id")
330-
if name is None:
331-
raise VariableDeclarationException("Invalid assignment", node)
328+
name = node.target.id
332329

333-
if not node.value:
334-
raise VariableDeclarationException(
335-
"Memory variables must be declared with an initial value", node
336-
)
330+
# sanity check postconditions of AnnAssign.validate()
331+
assert isinstance(node.target, vy_ast.Name)
332+
assert node.value is not None
337333

338334
typ = type_from_annotation(node.annotation, DataLocation.MEMORY)
339335

vyper/semantics/analysis/module.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
StateAccessViolation,
2121
StructureException,
2222
UndeclaredDefinition,
23-
VariableDeclarationException,
2423
VyperException,
2524
tag_exceptions,
2625
)

0 commit comments

Comments
 (0)