Håk*_* T. 5 python typechecking mypy python-typing
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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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.