Skip to content

Commit 151a399

Browse files
committed
changes (thanks Daniël Noord!)
1 parent e144fd1 commit 151a399

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

peps/pep-0661.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ uncommon enough that there hasn't been a clear need for standardization.
3737
However, the common implementations, including some in the stdlib, suffer from
3838
several 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
4141
be 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

4345
Note: Changing all existing sentinels in the stdlib to be implemented this
4446
way 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:
117119
3. It should be simple to define as many distinct sentinel values as needed.
118120
4. The sentinel objects should have a clear and short repr.
119121
5. 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.
122124
7. Such sentinels should work when using CPython 3.x and PyPy3, and ideally
123125
also with other implementations of Python.
124126
8. As simple and straightforward as possible, in implementation and especially
@@ -249,6 +251,9 @@ To support usage in type expressions, the runtime implementation
249251
of sentinel objects should have the ``__or__`` and ``__ror__``
250252
methods, 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+
252257
C 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
269274
the usual compatibility consideration for new builtins. Existing local,
270275
global, 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

273281
How 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

330342
Rejected Ideas
331343
==============
@@ -448,7 +460,7 @@ Use a registry of per-module sentinel names
448460
-------------------------------------------
449461

450462
Earlier 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
452464
module would return the same object, using a process-global registry keyed by
453465
module name and sentinel name.
454466

@@ -517,10 +529,6 @@ advantages of not requiring an import and being much shorter.
517529
Additional 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

Comments
 (0)