
Am 24.11.20 um 07:17 schrieb Andrew Svetlov:
In Python there is a wide-spread idiom of using specific object instances as markers:
MARKER = object()
def f(arg) -> if arg is MARKER: do_something_special() else: process_arg_as_string(arg)
When I add type hints my initial thought was decorating MARKER as constant literal:
from typing import Final, Literal, Union
MARKER: Final[object] = object()
def f(arg: Union[str, Literal[MARKER]]) -> None: pass
MARKER is declared as a final value, it cannot be changed from the typing perspective and IMHO satisfies requirements for Literal.
Sure, it doesn't work with the current mypy but maybe the proposal makes sense?
There is some prior discussion about such a feature in this issue: https://github.com/python/typing/issues/689. - Sebastian