Commit d8cc99c7 authored by Stefan Behnel's avatar Stefan Behnel

infer types of special args/kwargs parameters

parent 8b7c9fef
...@@ -1872,8 +1872,11 @@ class DefNode(FuncDefNode): ...@@ -1872,8 +1872,11 @@ class DefNode(FuncDefNode):
def declare_python_arg(self, env, arg): def declare_python_arg(self, env, arg):
if arg: if arg:
entry = env.declare_var(arg.name, if env.directives['infer_types'] != 'none':
PyrexTypes.py_object_type, arg.pos) type = PyrexTypes.unspecified_type
else:
type = py_object_type
entry = env.declare_var(arg.name, type, arg.pos)
entry.used = 1 entry.used = 1
entry.init = "0" entry.init = "0"
entry.init_to_none = 0 entry.init_to_none = 0
......
import ExprNodes import ExprNodes
import Nodes
import Builtin
import PyrexTypes import PyrexTypes
from PyrexTypes import py_object_type, unspecified_type, spanning_type from PyrexTypes import py_object_type, unspecified_type, spanning_type
from Visitor import CythonTransform from Visitor import CythonTransform
...@@ -20,7 +22,7 @@ object_expr = TypedExprNode(py_object_type) ...@@ -20,7 +22,7 @@ object_expr = TypedExprNode(py_object_type)
class MarkAssignments(CythonTransform): class MarkAssignments(CythonTransform):
def mark_assignment(self, lhs, rhs): def mark_assignment(self, lhs, rhs):
if isinstance(lhs, ExprNodes.NameNode): if isinstance(lhs, (ExprNodes.NameNode, Nodes.PyArgDeclNode)):
if lhs.entry is None: if lhs.entry is None:
# TODO: This shouldn't happen... # TODO: This shouldn't happen...
# It looks like comprehension loop targets are not declared soon enough. # It looks like comprehension loop targets are not declared soon enough.
...@@ -100,6 +102,17 @@ class MarkAssignments(CythonTransform): ...@@ -100,6 +102,17 @@ class MarkAssignments(CythonTransform):
self.visitchildren(node) self.visitchildren(node)
return node return node
def visit_DefNode(self, node):
# use fake expressions with the right result type
if node.star_arg:
self.mark_assignment(
node.star_arg, TypedExprNode(Builtin.tuple_type))
if node.starstar_arg:
self.mark_assignment(
node.starstar_arg, TypedExprNode(Builtin.dict_type))
self.visitchildren(node)
return node
class PyObjectTypeInferer: class PyObjectTypeInferer:
""" """
......
...@@ -195,3 +195,11 @@ def safe_only(): ...@@ -195,3 +195,11 @@ def safe_only():
assert typeof(b) == "Python object", typeof(c) assert typeof(b) == "Python object", typeof(c)
c = MyType() c = MyType()
assert typeof(c) == "MyType", typeof(c) assert typeof(c) == "MyType", typeof(c)
@infer_types('safe')
def args_tuple_keywords(*args, **kwargs):
"""
>>> args_tuple_keywords(1,2,3, a=1, b=2)
"""
assert typeof(args) == "tuple object", typeof(args)
assert typeof(kwargs) == "dict object", typeof(kwargs)
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