Skip to content

Commit 8146be9

Browse files
committed
[GR-31741] Github #178: __annotations__ attribute of a function have no "return" key
1 parent 25dbf66 commit 8146be9

File tree

11 files changed

+1339
-1290
lines changed

11 files changed

+1339
-1290
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ public void for01() throws Exception {
455455
" return 10");
456456
}
457457

458+
@Test
459+
public void functionAnnotationsTest() throws Exception {
460+
checkSerialization("def a() -> int: pass");
461+
checkSerialization("def a() -> max(1,3): pass");
462+
checkSerialization("def fn(a:'x', b:int, c: list, d: 5+ 6) -> int: pass");
463+
}
464+
458465
@Test
459466
public void functionDecoratorTest() throws Exception {
460467
checkSerialization("@some.path.to.decorator\n" + "def fn(): pass");

graalpython/com.oracle.graal.python.test/src/tests/test_parser.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def fn():
690690
assert "Function Documentation" in code1.co_consts
691691
assert "Function Documentation" not in code2.co_consts
692692
assert exec(code1) == exec(code2)
693-
693+
694694
def test_annotations_in_global():
695695
test_globals = {'__annotations__': {}}
696696
code = compile ("a:int", "<test>", "exec")
@@ -725,7 +725,32 @@ def test_annotations_in_global():
725725
assert test_globals['__annotations__']['a'] == int
726726
assert test_globals['a'] == 1
727727

728+
def test_annotations_in_function_declaration():
729+
def fn1(a:int, b: 5+6): pass
730+
assert len(fn1.__annotations__) == 2
731+
assert fn1.__annotations__['a'] == int
732+
assert fn1.__annotations__['b'] == 11
733+
734+
def fn2(a: list, b: "Hello") -> int: pass
735+
assert len(fn2.__annotations__) == 3
736+
assert fn2.__annotations__['a'] == list
737+
assert fn2.__annotations__['b'] == "Hello"
738+
assert fn2.__annotations__['return'] == int
739+
740+
def fn3() -> sum((1,2,3,4)): pass
741+
assert len(fn3.__annotations__) == 1
742+
assert fn3.__annotations__['return'] == 10
743+
744+
def fn4() -> f'hello {1+4}': pass
745+
assert len(fn4.__annotations__) == 1
746+
assert fn4.__annotations__['return'] == 'hello 5'
728747

748+
x = "Superman"
749+
def fn5() -> f'hello {x}': pass
750+
assert len(fn5.__annotations__) == 1
751+
assert fn5.__annotations__['return'] == 'hello Superman'
752+
753+
729754
def test_annotations_in_function():
730755
test_globals = {'__annotations__': {}}
731756
source = '''def fn():
@@ -878,4 +903,4 @@ def test_ComprehensionListExpr():
878903
assert [3,5,7] == [e+1 for e in [i*2 for i in (1,2,3)]]
879904
assert [('a', 1), ('a', 2), ('b', 1), ('b', 2)] == [ (c,s) for c in ('a','b') for s in (1,2)]
880905
assert [(1, 'a'), (2, 'a'), (1, 'b'), (2, 'b')] == [ (s,c) for c in ('a','b') for s in (1,2)]
881-
906+

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,11 @@ decorated:
563563

564564
async_funcdef: ASYNC funcdef;
565565
funcdef
566-
:
566+
:
567+
{ SSTNode resultType = null; }
567568
'def' n=NAME parameters
568569
(
569-
'->' test
570+
'->' test { resultType = $test.result; }
570571
)? ':'
571572
{
572573
String name = factory.mangleNameInCurrentScope($n.getText());
@@ -579,7 +580,7 @@ funcdef
579580
}
580581
s = suite
581582
{
582-
SSTNode funcDef = new FunctionDefSSTNode(scopeEnvironment.getCurrentScope(), name, enclosingClassName, $parameters.result, optimize($s.result), getStartIndex(_localctx), getStopIndex(((FuncdefContext)_localctx).s));
583+
SSTNode funcDef = new FunctionDefSSTNode(scopeEnvironment.getCurrentScope(), name, enclosingClassName, $parameters.result, optimize($s.result), resultType, getStartIndex(_localctx), getStopIndex(((FuncdefContext)_localctx).s));
583584
scopeEnvironment.popScope();
584585
loopState = savedLoopState;
585586
push(funcDef);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3.interp

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)