I have a function that accepts certain literals for a specific argument:
from typing import Literal
def fn(x: Literal["foo", "bar", "foo|bar"]) -> None:
reveal_type(x)
The third contains a pipe symbol (|), "foo|bar". This is interpreted by mypy as an error, as the name foo is not defined.
I guess this happens due to how forward references are evaluated? I use Python 3.8 with:
from __future__ import annotations
Is there a way to make this work? I can not change the string due to breaking backward compatibility, but currently, the whole annotation is revealed as Any, i.e. it holds no value.
Any"Literal['Color.RED']interprets'Color.RED'as a string instead of a forward reference, I think mypy should be changed to interpret"foo|bar"as a string instead of a forward reference here too.