El mar, 8 jun 2021 a las 8:28, Tal Einat (<taleinat@gmail.com>) escribió:
Hi,

I'm working on PEP 661: Sentinel Values [1].

One thing I think it should get right is that it should be possible to have strict type annotations for sentinel values. Many sentinel implementations don't support this.

I see that this has come up before on this mailing list [2] and as an issue in the GitHub repo [3].

Using Literal and Final, as suggested in the discussions of the PEP [4] and as suggested by Guido on the aforementioned GitHub issue [5], seems to be a very nice approach.
Looking at your PEP, I think it would be more user-friendly if the sentinel can be used directly as a type annotation, something like this:

SENTINEL = sentinel("SENTINEL")

def print_exception(*, value=Exception | SENTINEL) -> None: ...

This would be similar to how None works, and it would make the type annotation more concise than when we use Literal.
 

1. What would be needed to make that work with mypy?
Somebody would need to contribute an implementation. I don't think it would be very difficult since this isn't introducing any deep new concept to the type system. This doesn't necessarily need to be done right now, though; the implementation can come after the PEP is accepted.

Is there going to be a backport for the sentinels module on PyPI? That would make it easier for type checkers to already implement support.
 
2. What should the process be, in terms of what the PEP defines and what is later implemented in mypy and similar tools?
My preference would be that the PEP specifies how this new feature interacts with typing, either by allowing sentinels as types directly (my proposal above) or by saying that PEP 586 is amended to allow sentinels in Literal.
 

Please note that I'm far from well-versed on typing in Python, so assume I know only the very basics.

- Tal Einat

[1]: https://www.python.org/dev/peps/pep-0661/
[2]: https://mail.python.org/archives/list/typing-sig@python.org/thread/TI5Y2HTVT6DRUT6R6S7VW4SY5N6ZE4WF/
[3]: https://github.com/python/typing/issues/689
[4]: https://discuss.python.org/t/pep-661-sentinel-values/9126/2
[5]: https://github.com/python/typing/issues/689#issuecomment-561425237
_______________________________________________
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: jelle.zijlstra@gmail.com