0

I have a type like the following, with an extra string argument all. I was hoping something like this would work.

Note: MY_LIST is a list of strings.

list[Literal[MY_LIST]] | Literal["all"]

Error:

  File "/home/Me/.pyenv/versions/3.11.1/lib/python3.11/typing.py", line 1672, in __hash__
    return hash(frozenset(_value_and_type_iter(self.__args__)))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'

What is the correct command?

GlaceCelery
  • 921
  • 1
  • 13
  • 30
  • 4
    `Literal[MY_LIST]` is not a valid annotation. Why did you think you can use anything other than "literal ints, byte and unicode strings, bools, Enum values and None."? – juanpa.arrivillaga Aug 21 '23 at 21:55
  • It works without the `Literal["all"]` – GlaceCelery Aug 21 '23 at 21:58
  • 3
    What is `MY_LIST`? – Ry- Aug 21 '23 at 22:19
  • 1
    Please post the [full error message](//meta.stackoverflow.com/q/359146/4518341) for completeness. – wjandrea Aug 21 '23 at 22:36
  • Maybe `list[Literal[*MY_LIST]]` would work? I'm not sure how useful that is for static typing, but at least there's no runtime error if I set `MY_LIST = [0, 1, 3]` for example. The resulting annotation is `typing.Union[list[typing.Literal[0, 1, 3]], typing.Literal['all']]`. – wjandrea Aug 21 '23 at 22:38
  • Turns out mypy doesn't like that: `Invalid type: Literal[...] cannot contain arbitrary expressions`. But I can fix it by doing `MY_VALUES = Literal[0, 1, 3]; list[MY_VALUES] | Literal["all"]`. Maybe that's what you actually want? – wjandrea Aug 21 '23 at 22:49
  • `MY_LIST` is a bit like an enum. They are strings which my function uses as options – GlaceCelery Aug 22 '23 at 01:10
  • 3
    Does not tell us what they are "a bit like", just provide a [mcve]. Don't make people guess. Tell us what you are trying to accomplish – juanpa.arrivillaga Aug 22 '23 at 01:13
  • 3
    "It works without the Literal["all"]" - it might *not crash* without the `Literal["all"]`, but that doesn't mean it's valid. `typing` doesn't promise to crash if you do something invalid. It does very little runtime checking at all. – user2357112 Aug 22 '23 at 01:20
  • 2
    [Try running mypy on it](https://mypy-play.net/?mypy=latest&python=3.11&gist=dd6759e79d6385eb323722da56d25520), and it'll tell you `error: Parameter 1 of Literal[...] is invalid`, even with the `Literal["all"]`. – user2357112 Aug 22 '23 at 01:22
  • @user2357112 what is the easiest way tell the typing system that my function arguments come from a list of strings? – GlaceCelery Aug 22 '23 at 02:02
  • @GlaceCelery: That is not a valid static type. [You can go the other way around, though.](https://stackoverflow.com/questions/64522040/typing-dynamically-create-literal-alias-from-list-of-valid-values/64522240#64522240) – user2357112 Aug 22 '23 at 02:11
  • @GlaceCelery: Please ask that in your question (by [edit]ing) with enough detail for people to understand what you mean. – Ry- Aug 22 '23 at 02:19

0 Answers0