Joe*_*out 1 python dictionary types
我想在 python 3.10 中用作witha 的键TypedDict。
我有:
from typing import TypedDict, Optional
class Operation(TypedDict, total=False):
uses: str
with: Optional[ActionCheckout]
Run Code Online (Sandbox Code Playgroud)
但是我的IDE说我不能这样做?
您将无法使用声明性语法,因为with(由语法定义的硬关键字)不是有效的标识符;请改用函数语法。
Operation = TypedDict('Operation', {'uses': str, 'with': Optional[ActionCheckout]})
Run Code Online (Sandbox Code Playgroud)
文档中具体解决了这一点:
当任何键不是有效标识符时(例如因为它们是关键字),也应使用函数语法。