CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Typing][B-10] Add type annotations for python/paddle/distribution/distribution.py
#65769
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提交成功,感谢你对开源项目的贡献! |
@@ -18,7 +18,10 @@ | |||
# 'Normal', | |||
# 'Uniform'] |
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.
这块 TODO 和 __all__
的注释删掉吧,看着也没啥用
def _to_tensor(self, *args): | ||
def _to_tensor( | ||
self, *args: TensorLike | NestedNumbericSequence | ||
) -> tuple[Tensor, ...]: |
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.
应该是 Tensor?
from typing import TypeGuard
from typing_extensions import TypeIs
def is_str_seq(*args: str | int) -> TypeGuard[tuple[str, ...]]:
return all(isinstance(arg, str) for arg in args)
def foo(x: str | int):
if is_str_seq(x, x):
reveal_type(x) # tuple[str, ...]
else:
reveal_type(x)
这里好像 guard 住的是每个元素的类型
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.
应该是 Tensor?
?输出?
return tuple(variable_args)
from typing import TypeGuard from typing_extensions import TypeIs def is_str_seq(self, *args: str | int) -> TypeGuard[tuple[str, ...]]: return all(isinstance(arg, str) for arg in args) def foo(x: str | int): if is_str_seq(x, x): reveal_type(x) # tuple[str, ...] else: reveal_type(x)这里好像 guard 住的是每个元素的类型
这是指这里?
def _validate_args(
self, *args: TensorLike | NestedNumbericSequence
) -> TypeGuard[tuple[Tensor, ...]]:
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.
def _validate_args(
self, *args: TensorLike | NestedNumbericSequence
) -> TypeGuard[tuple[Tensor, ...]]:
"""
Argument validation for distribution args
Args:
value (float, list, numpy.ndarray, Tensor)
Raises
ValueError: if one argument is Tensor, all arguments should be Tensor
"""
is_variable = False
is_number = False
for arg in args:
if isinstance(arg, (Variable, paddle.pir.Value)):
is_variable = True
else:
is_number = True
if is_variable and is_number:
raise ValueError(
'if one argument is Tensor, all arguments should be Tensor'
)
return is_variable
是 guard 每个元素 ~ 有啥问题?
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.
每个元素都是 Tensor?那为啥是 TypeGuard[tuple[Tensor, ...]
?
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 TypeGuard
from typing_extensions import TypeIs
def is_str_seq(*args: str | int) -> TypeGuard[str]:
return all(isinstance(arg, str) for arg in args)
def foo(x: str | int, y: str | int):
if is_str_seq(x, y):
reveal_type(x) # str
reveal_type(y) # str | int
else:
reveal_type(x)
reveal_type(y)
我试了下 guard 不住,pyright 只能保证第一个参数 guard 住,这是 TypeGuard 规范中写的
mypy 甚至连第一个参数都 guard 不住
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.
每个元素都是 Tensor?那为啥是
TypeGuard[tuple[Tensor, ...]]
?
这个方法的作用不就是保证每一个元素都是 Tensor ? 输入是 *args
,所以用的 tuple[Tensor, ...]
~
我试了下 guard 不住,pyright 只能保证第一个参数 guard 住,这是 TypeGuard 规范中写的
那应该是实现的还不够完善吧 ~ 那咋办?写 TypeGuard[Tensor]
?
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.
就 TypeGuard[Tensor]
吧,大多数用例是没问题的,而且是非公开 API,也还好
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.
PR Category
User Experience
PR Types
Improvements
Description
类型标注:
python/paddle/distribution/distribution.py
Related links
@SigureMo @megemini