Skip to content

Conversation

megemini
Copy link
Contributor

@megemini megemini commented Jun 7, 2024

PR Category

User Experience

PR Types

Improvements

Description

将 typehints 显示在描述中,而不是函数签名中 ~

建议:

  • 在中文文档中不要写 type 。目前 torch、tf、keras 等,均没有在文档中的函数签名中写 type。
  • 在 api 的 docstring 中需要写类型

以本 PR 中的 api paddle.audio.save 为例,原 api 代码中:

  • 增加 from __future__ import annotations
  • Optional 改为 |
  • docstring 中增加数据类型
def save(
    filepath: str,
    src: paddle.Tensor,
    sample_rate: int,
    channels_first: bool = True,
    encoding: Optional[str] = None,
    bits_per_sample: Optional[int] = 16, # 注:此处不应该使用 Optional
):
    """
    Save audio tensor to file.

    Args:
        filepath: ...
        src: ...
        sample_rate: ...
        channels_first: ...
        encoding: ...
        bits_per_sample: ...

    Returns:
        None
...
    """

需要改为:

from __future__ import annotations
...
def save(
    filepath: str,
    src: paddle.Tensor,
    sample_rate: int,
    channels_first: bool = True,
    encoding: str|None = None,
    bits_per_sample: int = 16,
) -> None:
    """
    Save audio tensor to file.

    Args:
        filepath (str): ...
        src (Tensor): ...
        sample_rate (int): ...
        channels_first (bool, optional): ...
        encoding (str|None, optional): ...
        bits_per_sample (int, optional): ...

    Returns:
        None
...
    """

此 api 的中文文档中,需要修改:

  • 签名中不写 type
.. py:function:: paddle.audio.save(filepath: str, src: paddle.Tensor, sample_rate: int, channels_first: bool = True, encoding: Optional[str] = None, bits_per_sample: Optional[int] = 16)

需要改为

.. py:function:: paddle.audio.save(filepath, src, sample_rate, channels_first=True, encoding=None, bits_per_sample=16)

修改后,原中文文档的页面:

image

变更为:

image

原英文文档的页面:

image

变更为 :

image

注意:此时还未修改此 api 的 docstring,英文文档中,描述中的参数类型,是 sphinx 自动加进去的 ~

但是,如果修改了 api 的 docstring,添加了数据类型,sphinx 理论上会 merge 函数签名中的类型与描述中的参数类型 ~ 因此,此处具体效果还需要后续观察 ~

@sunzhongkai588 @SigureMo

Copy link

paddle-bot bot commented Jun 7, 2024

感谢你贡献飞桨文档,文档预览构建中,Docs-New 跑完后即可预览,预览链接:http://preview-pr-6676.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/api/index_cn.html
预览工具的更多说明,请参考:飞桨文档预览工具

@megemini megemini changed the title [Add] typehints conf 【Type Hints】将 typehints 显示在描述中而不是函数签名中 Jun 7, 2024
@@ -3,19 +3,19 @@
save
-------------------------------

.. py:function:: paddle.audio.save(filepath: str, src: paddle.Tensor, sample_rate: int, channels_first: bool = True, encoding: Optional[str] = None, bits_per_sample: Optional[int] = 16)
.. py:function:: paddle.audio.save(filepath, src, sample_rate, channels_first=True, encoding=None, bits_per_sample=16)

保存音频数据。

参数
::::::::::::

- **filepath** (str 或者 Path) - 保存音频路径。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **filepath** (str 或者 Path) - 保存音频路径。
- **filepath** (str) - 保存音频路径。

我试了下,pathlib.Path 并不是合法输入


保存音频数据。

参数
::::::::::::

- **filepath** (str 或者 Path) - 保存音频路径。
- **src** (paddle.Tensor) - 音频数据。
- **src** (Tensor) - 音频数据。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

话说这里改成 Tensor 后,为什么变更后的中文文档页面显示的还是 paddle.Tensor 呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是 Tensor ~ 上面的图是我把 paddle.Tensor 改为 Tensor 之前的 ~ 现已更新 ~

- **bits_per_sample** (Optional[int],可选) - 默认是 16,编码位长。
- **channels_first** (bool,可选) - 如果是 True,那么 src 的 Tensor 形状是[channel,time],如果是 False,则是[time,channel]。默认是 True。
- **encoding** (str|None,可选) - 默认是 None,编码信息。
- **bits_per_sample** (int,可选) - 默认是 16,编码位长。
Copy link
Member

@SigureMo SigureMo Jun 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里看起来也能接收 None,虽然接收 None 后会挂就是了

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow @sunzhongkai588 看看呢?另外英文的可以按照描述修改下~

@luotao1 luotao1 added the PaddlePaddle Hackathon 飞桨黑客松活动issue与PR label Jun 11, 2024
Copy link
Collaborator

@sunzhongkai588 sunzhongkai588 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 顺师傅修的太快了

@SigureMo SigureMo merged commit 58fe0d1 into PaddlePaddle:develop Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor PaddlePaddle Hackathon 飞桨黑客松活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants