
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? -- Thanks, Andrew Svetlov

Am 24.11.20 um 07:17 schrieb Andrew Svetlov:
There is some prior discussion about such a feature in this issue: https://github.com/python/typing/issues/689. - Sebastian

I recently ran across some Django code that has an interesting solution for typing a sentinel object: It uses a *type* object as a sentinel rather than just an `object()`: # django/db/models/fields/__init__.py class NOT_PROVIDED: pass class Field(RegisterLookupMixin): def __init__(self, ..., default=NOT_PROVIDED, ...): ... Presumably the type of the `default` parameter could then be written as: Union[Type[NOT_PROVIDED], Any] Not exactly direct, but may work. -- David Foster | Seattle, WA, USA Contributor to TypedDict support for mypy

That's a very expensive sentinel though. Classes are notoriously heavy objects. On Tue, Nov 24, 2020 at 7:17 PM David Foster <davidfstr@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>

Am 24.11.20 um 07:17 schrieb Andrew Svetlov:
There is some prior discussion about such a feature in this issue: https://github.com/python/typing/issues/689. - Sebastian

I recently ran across some Django code that has an interesting solution for typing a sentinel object: It uses a *type* object as a sentinel rather than just an `object()`: # django/db/models/fields/__init__.py class NOT_PROVIDED: pass class Field(RegisterLookupMixin): def __init__(self, ..., default=NOT_PROVIDED, ...): ... Presumably the type of the `default` parameter could then be written as: Union[Type[NOT_PROVIDED], Any] Not exactly direct, but may work. -- David Foster | Seattle, WA, USA Contributor to TypedDict support for mypy

That's a very expensive sentinel though. Classes are notoriously heavy objects. On Tue, Nov 24, 2020 at 7:17 PM David Foster <davidfstr@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>
participants (4)
-
Andrew Svetlov
-
David Foster
-
Guido van Rossum
-
Sebastian Rittau