Commit dec248ec authored by Stefan Behnel's avatar Stefan Behnel

Allow arbitrary type references in annotations, rather than just valid...

Allow arbitrary type references in annotations, rather than just valid executable Python expressions.
parent dd391121
...@@ -1639,10 +1639,17 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1639,10 +1639,17 @@ class FuncDefNode(StatNode, BlockNode):
elif default_seen: elif default_seen:
error(arg.pos, "Non-default argument following default argument") error(arg.pos, "Non-default argument following default argument")
def analyse_annotation(self, env, annotation):
if annotation is None:
return None
if annotation.analyse_as_type(env) is None:
annotation = annotation.analyse_types(env)
return annotation
def analyse_annotations(self, env): def analyse_annotations(self, env):
for arg in self.args: for arg in self.args:
if arg.annotation: if arg.annotation:
arg.annotation = arg.annotation.analyse_types(env) arg.annotation = self.analyse_annotation(env, arg.annotation)
def align_argument_type(self, env, arg): def align_argument_type(self, env, arg):
# @cython.locals() # @cython.locals()
...@@ -3040,7 +3047,7 @@ class DefNode(FuncDefNode): ...@@ -3040,7 +3047,7 @@ class DefNode(FuncDefNode):
self.analyse_default_values(env) self.analyse_default_values(env)
self.analyse_annotations(env) self.analyse_annotations(env)
if self.return_type_annotation: if self.return_type_annotation:
self.return_type_annotation = self.return_type_annotation.analyse_types(env) self.return_type_annotation = self.analyse_annotation(env, self.return_type_annotation)
if not self.needs_assignment_synthesis(env) and self.decorators: if not self.needs_assignment_synthesis(env) and self.decorators:
for decorator in self.decorators[::-1]: for decorator in self.decorators[::-1]:
......
...@@ -114,6 +114,21 @@ def return_tuple_for_carray() -> tuple: ...@@ -114,6 +114,21 @@ def return_tuple_for_carray() -> tuple:
return x return x
MyStruct = cython.struct(x=cython.int, y=cython.int, data=cython.double)
def struct_io(s : MyStruct) -> MyStruct:
"""
>>> d = struct_io(dict(x=1, y=2, data=3))
>>> sorted(d.items())
[('data', 3.0), ('x', 2), ('y', 1)]
"""
t = s
t.x, t.y = s.y, s.x
return t
_WARNINGS = """ _WARNINGS = """
8:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly. 8:32: Strings should no longer be used for type declarations. Use 'cython.int' etc. directly.
8:47: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly. 8:47: Dicts should no longer be used as type annotations. Use 'cython.int' etc. directly.
......
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