CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Typing] 修改一些示例代码中的类型错误 part 1 #65461
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
@@ -44,59 +44,63 @@ class saved_tensors_hooks: | |||
|
|||
Examples: | |||
.. code-block:: python | |||
:name: code-example1 |
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.
拆分示例代码会影响中文文档 COPY-FROM,记得同步修改~
python/paddle/amp/debugging.py
Outdated
@@ -87,7 +88,7 @@ def check_layer_numerics(func): | |||
... # return 1/x * self._w + self._b open it you will see the error log | |||
... return x @ self._w + self._b | |||
... | |||
>>> dtype = 'float32' | |||
>>> dtype: DTypeLike = 'float32' |
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.
还是不暴露 DTypeLike 给用户了吧,现有的其它代码我都是改成了
dtype: Literal["float32"] = "float32"
因为 pyright 的推导就是这样的
pyright 作者的回复也是如此,type checker 理应做到这点
在这一行 ignore 是不是没用?如果可以在这行简单地 ignore 就没问题的话,是最好的
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.
from typing import Literal
def foo(x: Literal["a"]):
return x
x = "a" # type: Literal["a"]
foo(x)
还有一种方法,python 也是支持这种注释形式的类型提示的,我们可以这样非侵入地添加提示,然后在示例代码呈现时候删掉就好了,看看这样呢?
(不过 from typing import Literal
省不掉……)
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.
import paddle
dtype = 'float32'
x = paddle.to_tensor([1,2], dtype=dtype) # type: ignore
可以把 # type: ignore
放到下面 ~ 要不加这个?
x = "a" # type: Literal["a"]
感觉这样也挺奇怪的,毕竟上面要 from typing import Literal
~
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.
可以把
# type: ignore
放到下面 ~ 要不加这个?
往往要加多个,因为一般他这么写,就是为了复用的,不然直接写到函数调用处了
我觉得都可以,ignore 就可以
Update 20240626
|
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.
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! LGTM
PR Category
User Experience
PR Types
Bug fixes
Description
关联 PR #65008 #65397
@SigureMo