@@ -37,8 +37,10 @@ uncommon enough that there hasn't been a clear need for standardization.
3737However, the common implementations, including some in the stdlib, suffer from
3838several significant drawbacks.
3939
40- This PEP proposes adding a built-in utility for defining sentinel values, to
40+ This PEP proposes adding a built-in class for defining sentinel values, to
4141be used in the stdlib and made publicly available to all Python code.
42+ Sentinels can be defined in Python with the ``sentinel() `` built-in class,
43+ and in C with the ``PySentinel_New() `` C API function.
4244
4345Note: Changing all existing sentinels in the stdlib to be implemented this
4446way is not deemed necessary, and whether to do so is left to the discretion
@@ -117,8 +119,8 @@ The criteria guiding the chosen implementation were:
1171193. It should be simple to define as many distinct sentinel values as needed.
1181204. The sentinel objects should have a clear and short repr.
1191215. It should be possible to use clear type signatures for sentinels.
120- 6. The sentinel objects should behave correctly after copying, and module-scope
121- sentinels should preserve identity when pickled and unpickled.
122+ 6. The sentinel objects should behave correctly after copying, and sentinels
123+ should have predictable behavior when pickled and unpickled.
1221247. Such sentinels should work when using CPython 3.x and PyPy3, and ideally
123125 also with other implementations of Python.
1241268. As simple and straightforward as possible, in implementation and especially
@@ -249,6 +251,9 @@ To support usage in type expressions, the runtime implementation
249251of sentinel objects should have the ``__or__ `` and ``__ror__ ``
250252methods, returning :py:class: `typing.Union ` objects.
251253
254+ The Typing Council `supports <https://github.com/python/steering-council/issues/258#issuecomment-2417524379 >`_
255+ this part of the proposal.
256+
252257C API
253258-----
254259
@@ -268,6 +273,9 @@ Adding a new builtin means that code which currently relies on the bare name
268273``sentinel `` raising ``NameError `` will instead see the new builtin. This is
269274the usual compatibility consideration for new builtins. Existing local,
270275global, and imported names called ``sentinel `` are unaffected.
276+ Code that already uses the name ``sentinel `` will have to be adapted to use
277+ the new builtin and may receive new linter errors from linters that warn
278+ about collisions with builtin names.
271279
272280
273281How to Teach This
@@ -326,6 +334,10 @@ A sketch of the intended behavior follows::
326334 def __ror__(self, other):
327335 return typing.Union[other, self]
328336
337+ A backport `exists <https://typing-extensions.readthedocs.io/en/latest/#typing_extensions.Sentinel >`_
338+ in the :pypi: `typing-extensions ` module, though its behavior does not precisely
339+ match the current iteration of this PEP.
340+
329341
330342Rejected Ideas
331343==============
@@ -448,7 +460,7 @@ Use a registry of per-module sentinel names
448460-------------------------------------------
449461
450462Earlier drafts proposed making sentinel names unique within each module. Under
451- that design, repeated calls such as ``Sentinel ("MISSING") `` from the same
463+ that design, repeated calls such as ``sentinel ("MISSING") `` from the same
452464module would return the same object, using a process-global registry keyed by
453465module name and sentinel name.
454466
@@ -517,10 +529,6 @@ advantages of not requiring an import and being much shorter.
517529Additional Notes
518530================
519531
520- * This PEP and an initial implementation were drafted in a dedicated GitHub
521- repo [7 ]_. The implementation should be updated to match this simplified
522- proposal.
523-
524532* For sentinels defined in a class scope, to avoid potential name clashes,
525533 or when a qualified repr would be clearer, one should pass the desired
526534 qualified name explicitly. For example::
0 commit comments