Skip to content

Commit 81aa8f9

Browse files
committed
[GR-18908] Variable definition with type annotation doesn't require an assignment.
PullRequest: graalpython/693
2 parents 38f7fac + 71f484c commit 81aa8f9

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/AssignmentTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,9 @@ public void nonLocal01() throws Exception {
208208
" inner()\n",
209209
"SyntaxError: name 'x' is assigned to before nonlocal declaration");
210210
}
211+
212+
@Test
213+
public void annotationType01() throws Exception {
214+
checkTreeResult("j: int");
215+
}
211216
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ModuleRootNode Name: <module 'annotationType01'> SourceSection: [0,6]`j: int`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,6]`j: int`
8+
ExpressionWithSideEffect SourceSection: [0,6]`j: int`
9+
Expression:
10+
EmptyNode SourceSection: None
11+
SideEffect:
12+
WriteNameNodeGen SourceSection: [0,6]`j: int`
13+
Identifier: j
14+
ObjectLiteralNode SourceSection: None

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3New.g4

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,15 @@ expr_stmt
550550
(
551551
':' t=test
552552
(
553-
'=' test { rhs = $test.result; rhsStopIndex = getStopIndex($test.stop);}
553+
'=' test { rhs = $test.result;}
554554
)?
555-
{ push(new AnnAssignmentSSTNode($lhs.result, $t.result, rhs, getStartIndex($ctx), rhsStopIndex)); }
555+
{
556+
rhsStopIndex = getStopIndex($test.stop);
557+
if (rhs == null) {
558+
rhs = new SimpleSSTNode(SimpleSSTNode.Type.NONE, -1, -1);
559+
}
560+
push(new AnnAssignmentSSTNode($lhs.result, $t.result, rhs, getStartIndex($ctx), rhsStopIndex));
561+
}
556562
|
557563
augassign
558564
(

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/FactorySSTVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,9 @@ public PNode visit(SimpleSSTNode node) {
997997
break;
998998
case NONE:
999999
result = nodeFactory.createObjectLiteral(PNone.NONE);
1000-
result.assignSourceSection(createSourceSection(node.startOffset, node.endOffset));
1000+
if (node.startOffset < node.endOffset) {
1001+
result.assignSourceSection(createSourceSection(node.startOffset, node.endOffset));
1002+
}
10011003
break;
10021004
case ELLIPSIS:
10031005
result = nodeFactory.createObjectLiteral(PEllipsis.INSTANCE);

0 commit comments

Comments
 (0)