Commit 8788cecf authored by Stefan Behnel's avatar Stefan Behnel

add support for annotations on "*args" and "**kwargs" arguments

parent 620274bf
...@@ -7812,6 +7812,14 @@ class PyCFunctionNode(ExprNode, ModuleNameMixin): ...@@ -7812,6 +7812,14 @@ class PyCFunctionNode(ExprNode, ModuleNameMixin):
if not arg.annotation.type.is_pyobject: if not arg.annotation.type.is_pyobject:
arg.annotation = arg.annotation.coerce_to_pyobject(env) arg.annotation = arg.annotation.coerce_to_pyobject(env)
annotations.append((arg.pos, arg.name, arg.annotation)) annotations.append((arg.pos, arg.name, arg.annotation))
for arg in (self.def_node.star_arg, self.def_node.starstar_arg):
if arg and arg.annotation:
arg.annotation = arg.annotation.analyse_types(env)
if not arg.annotation.type.is_pyobject:
arg.annotation = arg.annotation.coerce_to_pyobject(env)
annotations.append((arg.pos, arg.name, arg.annotation))
if self.def_node.return_type_annotation: if self.def_node.return_type_annotation:
annotations.append((self.def_node.return_type_annotation.pos, annotations.append((self.def_node.return_type_annotation.pos,
StringEncoding.EncodedString("return"), StringEncoding.EncodedString("return"),
......
# cython: language_level=3 # cython: language_level=3, binding=True
# mode: run # mode: run
# tag: generators, python3, exceptions # tag: generators, python3, exceptions
...@@ -437,11 +437,24 @@ def int_literals(): ...@@ -437,11 +437,24 @@ def int_literals():
print(cython.typeof(1UL)) print(cython.typeof(1UL))
print(cython.typeof(10000000000000UL)) print(cython.typeof(10000000000000UL))
def annotation_syntax(a : "test new test", b : "other" = 2) -> "ret": def annotation_syntax(a: "test new test", b : "other" = 2, *args: "ARGS", **kwargs: "KWARGS") -> "ret":
""" """
>>> annotation_syntax(1) >>> annotation_syntax(1)
3 3
>>> annotation_syntax(1,3) >>> annotation_syntax(1,3)
4 4
>>> len(annotation_syntax.__annotations__)
5
>>> print(annotation_syntax.__annotations__['a'])
test new test
>>> print(annotation_syntax.__annotations__['b'])
other
>>> print(annotation_syntax.__annotations__['args'])
ARGS
>>> print(annotation_syntax.__annotations__['kwargs'])
KWARGS
>>> print(annotation_syntax.__annotations__['return'])
ret
""" """
return a+b return a+b
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment