I suppose I'm hoping for some variant of the Use class objects rejected idea from the PEP: define your own specific sentinel class, which subclasses a Sentinel base class, or has a Sentinel metaclass.  The base class would be responsible for providing the __repr__ and __str__ implementations. Then, one uses the specific subclass as the sentinel, and it can be easily type checked. So, ideally, something like:

class MySentinel(Sentinel):
    pass

Paul


On Tue, 2021-06-08 at 21:02 +0200, Sebastian Rittau wrote:
Am 08.06.21 um 20:54 schrieb Paul Bryan:

On Tue, 2021-06-08 at 20:40 +0200, Sebastian Rittau wrote:


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.


Why can't a sentinel class itself be used? Why would it need to be instantiated?

Do you mean something like this?

    # sentinels.pyi

    class Sentinel: ...  # this is currently not part of the PEP and wouldn't exist at runtime
    def sentinel(...) -> Sentinel: ...

    # foo.py

    X = sentinel()
    def bar(x: X) -> None: ...

Apart from the part that a "Sentinel" class currently isn't part of the PEP (although I'd prefer if it was), this means that all sentinels would be type compatible with each other and using the wrong sentinel wouldn't be caught at type checking time.

If this is not what you meant, could you give an example?

 - Sebastian

_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: pbryan@anode.ca