-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[Typing][A-26] Add type annotations for paddle/vision/transforms/transforms.py
#65378
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
from paddle import Tensor | ||
from paddle._typing import DataLayout1D | ||
|
||
_DataT = TypeVar("_DataT", bound=Tensor | PILImage | npt.NDArray[Any]) |
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.
有两个小问题:
- python 3.8 不能用
|
- 这里好像不需要 bound?需要继承关系?
以下代码本地验证通过:
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal, TypeVar, overload, Union, Sequence
import numpy as np
import numpy.typing as npt
from typing_extensions import TypeAlias
from PIL import Image
from PIL.Image import Image as PILImage
class Tensor: ...
_DataT = TypeVar("_DataT", Tensor, PILImage, npt.NDArray[Any])
_Keys: TypeAlias = Sequence[Literal["image", "coords", "boxes", "mask"]]
def test(d: _DataT, k: _Keys | None = None) -> _DataT:
return d
t1 = Tensor()
t2 = np.array(123)
t3 = 123
t4 = Image.open('/home/shun/Pictures/Selection_147.png')
test(t1)
test(t2)
test(t2, ['coords'])
# test(t3) fail
test(t4)
data_format: DataLayout1D | ||
|
||
def __init__( | ||
self, data_format: DataLayout1D = 'CHW', keys: _Keys | None = None |
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.
是不是应该用 DataLayoutImage: TypeAlias = Literal["HWC", "CHW"]
@@ -394,7 +444,15 @@ class Resize(BaseTransform): | |||
(150, 200) | |||
""" | |||
|
|||
def __init__(self, size, interpolation='bilinear', keys=None): | |||
size: int | Sequence[int] |
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.
可以用 Size2: TypeAlias = Union[int, Tuple[int, int], List[int]]
还有其他的几个 size 应该都有对应的别名 ~
padding: int | Sequence[int] | None | ||
pad_if_needed: bool | ||
fill: int | tuple[int, ...] | ||
padding_mode: Literal['constant', 'edge', 'reflect', 'symmetric'] |
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.
_PaddingMode ?
|
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 paddle._typing import DataLayoutImage, Size2, Size3, Size4 | ||
|
||
_DataT = TypeVar("_DataT", Tensor, PILImage, npt.NDArray[Any]) | ||
_Keys: TypeAlias = Sequence[Literal["image", "coords", "boxes", "mask"]] |
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.
感觉 _Keys
有点太宽泛了,有更具体的名字嘛?比如 _TransformKeys
?
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.
…nsforms.py` (PaddlePaddle#65378) --------- Co-authored-by: SigureMo <sigure.qaq@gmail.com>
…nsforms.py` (PaddlePaddle#65378) --------- Co-authored-by: SigureMo <sigure.qaq@gmail.com>
PR Category
User Experience
PR Types
Improvements
Description
类型标注:
Related links
@SigureMo @megemini