On Tue, Jun 8, 2021 at 11:09 PM Guido van Rossum <guido@python.org> wrote:
On Tue, Jun 8, 2021 at 11:40 AM Sebastian Rittau <srittau@rittau.biz> wrote:
Am 08.06.21 um 18:51 schrieb Guido van Rossum:
Why do type checkers have to special-case sentinels? Until this idiom has become popular, using `Literal[SENTINEL]` seems the most logical approach. The only change to type checkers in that case would be to treat `sentinel` as one of the types whose values can be used in `Literal[]`.

Unless I misunderstand something (quite possible!), the following idiom does not currently work with type checkers and is not supposed to work:

    X = foo()
    def bar(x: Literal[X]) -> None: ...

This means that "X = sentinel()" would need to be special-cased.


Hm, I think you're right. I was thinking of

X = 42

def bar(x: Literal[X]):
    ...

but I don't even know if that works, and even if it did, it wouldn't work if you replaced 42 with a call to int(...).

The precedent here is Enum values. How does Literal work with Enum values? Could sentinels use the same mechanism?

- Tal