Commit e6c94984 authored by Stefan Behnel's avatar Stefan Behnel

allow signature return type annotations to provide a more specific return...

allow signature return type annotations to provide a more specific return object type for def functions
parent 52486e96
...@@ -78,6 +78,34 @@ def embed_position(pos, docstring): ...@@ -78,6 +78,34 @@ def embed_position(pos, docstring):
return doc return doc
def _analyse_signature_annotation(annotation, env):
base_type = None
explicit_pytype = explicit_ctype = False
if annotation.is_dict_literal:
for name, value in annotation.key_value_pairs:
if not name.is_string_literal:
continue
if name.value in ('type', b'type'):
explicit_pytype = True
if not explicit_ctype:
annotation = value
elif name.value in ('ctype', b'ctype'):
explicit_ctype = True
annotation = value
if explicit_pytype and explicit_ctype:
warning(annotation.pos, "Duplicate type declarations found in signature annotation")
arg_type = annotation.analyse_as_type(env)
if arg_type is not None:
if explicit_pytype and not explicit_ctype and not arg_type.is_pyobject:
warning(annotation.pos,
"Python type declaration in signature annotation does not refer to a Python type")
base_type = CAnalysedBaseTypeNode(
annotation.pos, type=arg_type, is_arg=True)
else:
warning(annotation.pos, "Unknown type declaration found in signature annotation")
return base_type, arg_type
def write_func_call(func, codewriter_class): def write_func_call(func, codewriter_class):
def f(*args, **kwds): def f(*args, **kwds):
if len(args) > 1 and isinstance(args[1], codewriter_class): if len(args) > 1 and isinstance(args[1], codewriter_class):
...@@ -843,29 +871,9 @@ class CArgDeclNode(Node): ...@@ -843,29 +871,9 @@ class CArgDeclNode(Node):
annotation = self.annotation annotation = self.annotation
if not annotation: if not annotation:
return None return None
explicit_pytype = explicit_ctype = False base_type, arg_type = _analyse_signature_annotation(annotation, env)
if annotation.is_dict_literal: if base_type is not None:
for name, value in annotation.key_value_pairs: self.base_type = base_type
if not name.is_string_literal:
continue
if name.value in ('type', b'type'):
explicit_pytype = True
if not explicit_ctype:
annotation = value
elif name.value in ('ctype', b'ctype'):
explicit_ctype = True
annotation = value
if explicit_pytype and explicit_ctype:
warning(annotation.pos, "Duplicate type declarations found in signature annotation")
arg_type = annotation.analyse_as_type(env)
if arg_type is not None:
if explicit_pytype and not explicit_ctype and not arg_type.is_pyobject:
warning(annotation.pos,
"Python type declaration in signature annotation does not refer to a Python type")
self.base_type = CAnalysedBaseTypeNode(
annotation.pos, type=arg_type, is_arg=True)
else:
warning(annotation.pos, "Unknown type declaration found in signature annotation")
return arg_type return arg_type
def calculate_default_value_code(self, code): def calculate_default_value_code(self, code):
...@@ -2724,6 +2732,13 @@ class DefNode(FuncDefNode): ...@@ -2724,6 +2732,13 @@ class DefNode(FuncDefNode):
self.analyse_signature(env) self.analyse_signature(env)
self.return_type = self.entry.signature.return_type() self.return_type = self.entry.signature.return_type()
# if a signature annotation provides a more specific return object type, use it
if self.return_type is py_object_type and self.return_type_annotation:
if env.directives['annotation_typing'] and not self.entry.is_special:
_, return_type = _analyse_signature_annotation(self.return_type_annotation, env)
if return_type and return_type.is_pyobject:
self.return_type = return_type
self.create_local_scope(env) self.create_local_scope(env)
self.py_wrapper = DefNodeWrapper( self.py_wrapper = DefNodeWrapper(
......
...@@ -62,3 +62,13 @@ def pytypes_cdef(a, b=2, c=3, d=4): ...@@ -62,3 +62,13 @@ def pytypes_cdef(a, b=2, c=3, d=4):
TypeError: ... TypeError: ...
""" """
return c_pytypes_cdef(a, b, c, d) return c_pytypes_cdef(a, b, c, d)
def return_tuple_for_carray() -> tuple:
"""
>>> return_tuple_for_carray()
(1, 2, 3)
"""
cdef int[3] x
x = [1, 2, 3]
return x
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