[Python] Unpacking an iterable into subscript/a type hint? [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página