diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py b/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py index 368a01de81efc5dbfc2561f8a0023e0774e12f69..98e76c0f46ffc53abd84f8682b21e0c7ae204e8e 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py @@ -349,7 +349,11 @@ class StaticAnalysisVisitor(object): ret_type = {NodeVarType.type_from_annotation(node.annotation)} # if annotation and value(Constant) are diffent type, we use value type if node.value: - ret_type = self.node_to_wrapper_map[node.value].node_var_type + node_value_type = self.node_to_wrapper_map[ + node.value].node_var_type + if not (node_value_type & + {NodeVarType.UNKNOWN, NodeVarType.STATEMENT}): + ret_type = node_value_type if isinstance(node.target, gast.Name): self.node_to_wrapper_map[node.target].node_var_type = ret_type self.var_env.set_var_type(node.target.id, ret_type) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py index eb545e5ca26add0be3f61a6025833ddc8b376012..388291a51c22f4fa52fda5b99f30fb879df93447 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py @@ -147,6 +147,7 @@ result_var_type6 = { def func_to_test7(a: int, b: float, c: paddle.Tensor, d: float='diff'): a = True e, f = paddle.shape(c) + g: paddle.Tensor = len(c) result_var_type7 = { @@ -155,7 +156,8 @@ result_var_type7 = { 'c': {NodeVarType.TENSOR}, 'd': {NodeVarType.STRING}, 'e': {NodeVarType.PADDLE_RETURN_TYPES}, - 'f': {NodeVarType.PADDLE_RETURN_TYPES} + 'f': {NodeVarType.PADDLE_RETURN_TYPES}, + 'g': {NodeVarType.TENSOR} } test_funcs = [