-2

I have a Literal type of particular string values. I'm trying to generate a list of those strings as well dynamically:

from typing import List, Literal, get_args

Color = Literal['red', 'green', 'blue']

colors = List[Color] = get_args(Color)

The third line at List is throwing this error:

Generic class type cannot be assigned    Pylance(reportGeneralTypeIssues)

I'm attempting to replicate this answer https://stackoverflow.com/a/64522240/12574341

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
Michael Moreno
  • 947
  • 1
  • 7
  • 24

1 Answers1

1

There should be a colon instead of an equal sign. If you want to make it a list, a type cast is required to cast from tuple[Any, ...] to list[Color].

from typing import cast, Literal, get_args


Color = Literal["red", "green", "blue"]

colors = cast(list[Color], list(get_args(Color)))
Paweł Rubin
  • 2,030
  • 1
  • 14
  • 25