Unpacking an iterable into subscript/a type hint?

2020-07-26 07:42发布

问题:

Say I need a list of values both at runtime and as type hint:

from typing import Literal

valid_values = ('foo', 'bar', 'baz')

def f(arg: Literal[???]):
    assert arg in valid_values

Is it possible to unpack valid_values into the Literal type hint somehow, e.g. Literal[*valid_values] (which is invalid syntax)? Repeating the values would be sub-optimal.