在 python 中,我有一个对象data,它可能是任何对象。
在 vscodev1, v2 = data # type: str, str语句中我想要v1, v2 弹出 str 方法。
在vscodev1, v2 = data # type: dict, set句子中我想要v1, v2 弹出字典,设置方法。
data = (object, object)
v1, v2 = data # type: str, str
v11, v22= data # type: dict, set
Run Code Online (Sandbox Code Playgroud)
但它在 pylance 中显示错误
Type annotation not supported for this type of expression
Unexpected token at end of expression
Run Code Online (Sandbox Code Playgroud)
不确定我是否正确理解了您的观点,但如果需要,您可以声明数据类型,然后声明变量类型:
import typing as ty
data = ({}, 0.0) # type: ty.Tuple[dict, float]
v1: "dict"
v2: "str"
v1, v2 = data
Run Code Online (Sandbox Code Playgroud)
我无法在 vscode 上测试它,但上面给出了与 Pyright 一致的类型检查(由 pylance 使用)
编辑:集成 @Abhijit 注释,对于 python 3.9+ 将是:
data: tuple[dict, float] = ({}, 0.0)
v1: "dict"
v2: "str"
v1, v2 = data
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5394 次 |
| 最近记录: |