Skip to content
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] Rewrite type annotations specification #6836

Merged
merged 9 commits into from
Aug 19, 2024

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Aug 17, 2024

明确类型提示标注规范内容,为其他开发者提供参考

最佳实践大多数内容是通用的,并不仅限于 Paddle,对于非 Paddle 框架开发也有参考价值

preview link: http://preview-pr-6836.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/dev_guides/style_guide_and_references/type_annotations_specification_cn.html

@megemini @zrr1999 @gouzil @sunzhongkai588

Copy link

paddle-bot bot commented Aug 17, 2024

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

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

赞!!!本来还想过些日子,等任务都搞完了再更新这个文档,结果被抢先了 ~~~ 😆😆😆 🎉🎉🎉

另外,CI 那边是不是可以加个引导了?

Comment on lines +9 to +11
## 类型提示标注范围

Paddle 是一个大型开源项目,包含了数十万行的 Python 代码,不乏有历史悠久的代码,为全部函数都标注类型提示是不可取的,也是不现实的。因此,我们需要明确界定哪些代码是需要标注类型提示的,哪些代码是不需要标注类型提示的。
Copy link
Contributor

Choose a reason for hiding this comment

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

是否加一个,如何界定公开与非公开 API ~ 类?函数?枚举?类中的公开方法、私有方法、魔法方法?

Copy link
Member Author

Choose a reason for hiding this comment

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

这个我觉得不应该在这里说明,这应该是 开发 API Python 端 的事情

Comment on lines +175 to +184
from typing import TypedDict

class SaveOptions(TypedDict):
path: str
id: int

def save(options: SaveOptions) -> None: ...
```

这样的类型提示不仅能够提示 `options` 是一个字典,还能够提示 `options` 的键值对的类型,这会带来如下几点优点:
Copy link
Contributor

Choose a reason for hiding this comment

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

能不能加一个 TypedDict, total=False 的例子?total=False 的情况更多,之前基本都是用 NotRequired ,感觉太繁琐了 ... ...

Copy link
Member Author

Choose a reason for hiding this comment

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

这个我单拉一个对 kwargs 标注的主题来说明,不然会使得这里篇幅过多,抓不到重点

@SigureMo
Copy link
Member Author

另外,CI 那边是不是可以加个引导了?

对,这个和代码风格指南是同等地位的,在检查失败时应该引导参考本指南

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 356bbb4 into PaddlePaddle:develop Aug 19, 2024
2 checks passed
@SigureMo SigureMo deleted the rewrite-type-annotations-spec branch August 19, 2024 07:53
Aoraki-Dream pushed a commit to Aoraki-Dream/docs that referenced this pull request Sep 4, 2024
---------

Co-authored-by: megemini <megemini@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants