比方说,我有一个函数,它有一个与方法名称对应的字符串参数:
def func(method: str):
if method not in ('simple_method', 'some_other_method'):
raise ValueError('Unknown method')
Run Code Online (Sandbox Code Playgroud)
我可以添加所有可能支持的选项(字符串)作为此参数的类型提示吗?例如,像这样的东西(因为打字时没有 Str,所以不起作用):
from typing import Str
def func(method: Str['simple_method', 'some_other_method']):
...
Run Code Online (Sandbox Code Playgroud)