-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dy2stat]support Python3 type annotation #36544
[Dy2stat]support Python3 type annotation #36544
Conversation
Thanks for your contribution! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work~
# 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 | ||
if isinstance(node.target, gast.Name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里只会对类似 x: float = 2.1
作处理,对于类似self.x: float = 2.1
是不会处理? 因为self.x
是一个gast.Attribute,而不是一个gast.Name
.
此PR 若是不支持的,这里记一个TODO,需要确认下是否有类似非gats.Name的场景和需求
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在之前代码中的Assign node也没有支持类似self.x= 2.1
赋值语句,所以在AnnAssign node也暂时不支持self.x: float = 2.1
类似语句。后续可以确认是否需要支持此类赋值语句。
@@ -57,6 +57,9 @@ def func_to_test3(): | |||
h = None | |||
i = False | |||
j = None + 1 | |||
k: float = 1.0 | |||
l: paddle.Tensor = fluid.dygraph.to_variable( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to_variable 接口已经被弃用了,应该避免使用旧的接口
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改使用to_tensor接口
* Support Py3 type annotations in @to_static * support type hint for args in func * support type hint assign * if annotation and value(Constant) are diffent type, we use value type * polish type_from_annotation() * code format * code format * remove useless commentary * fix review Co-authored-by: Aurelius84 <zhangliujie@baidu.com>
PR types
Others
PR changes
Others
Describe
Dy2stat type annotation
背景
python3支持type hint语法,动转静模块中可以借助type hint语法进行变量类型分析。
功能描述
python3中的type hint语法,比如赋值语句
a: float = 1.0
和函数参数func(a: int, b: float = 1)
。对于下面的代码forward
函数虽然将texts
标示为paddle.Tensor
类型,但是由于动转静模块并未支持python3的type hint语法,无法将texts
识别为一个tensor,for语句动转静处理list时会出错。在此PR之后可以将函数参数texts
识别为一个tensor。测试
下面是两个测试样例,以及
StaticAnalysisVisitor
模块所识别出的变量类型。