Skip to content

Conversation

Asthestarsfalll
Copy link
Contributor

PR Category

Others

PR Types

Others

Description

为 Paddle 添加类型提示(Type Hints)中需要使用一些类型注解,而目前 Paddle 支持python 3.8 以上的版本,少数类型未被添加到标准库 typing 中,因此需要引入 typing_extensions。

目前 Paddle 只在开发过程中会间接依赖于 typing_extensions:
240419_14h48m56s_screenshot

若是通过 pypi 安装则没有 typing_extensions,因此需要添加该包。

Copy link

paddle-bot bot commented Apr 19, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Apr 19, 2024
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 🐾

typing_extensions 是 Python 官方维护的类型扩展包,主要用于高版本类型信息向低版本 backport

Python 目前主要类型提示信息由 typing 提供,但因为 typing 是内置标准库,Python 版本确定后其中所包含的类型是确定的,但随着新版本的发布,Python 提供了一些更加易用的类型信息,为了能够在低版本使用高版本才有的类型信息,Python 官方提供了第三方库 typing_extensions,由于是第三方库,所以可以在任何版本中使用,只需要将其作为依赖项即可

比如 Python 3.10 typing 模块才提供 TypeAlias,对于 Python 3.8、3.9 来说 typing 模块是没有 TypeAlias 的,但我们可以从 typing_extensions import

-from typing import TypeAlias
+from typing_extensions import TypeAlias

由于 typing_extensions 的存在可以极大提高类型提示的体验,因此已经成为添加类型标注的标配,PyTorch 也是明确将其声明为依赖项1

Footnotes

  1. https://github.com/pytorch/pytorch/blob/3af12447f85dfede191a113c052e58fa7b21a8b3/requirements.txt#L11

@phlrain phlrain self-requested a review April 22, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants