You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PEP 661: Some changes after submission (before SC review) (#4232)
* Use "sentinellib" for the new module name
This avoids breaking code using the existing "sentinels" PyPI package.
* Change type annotations to use the bare name rather than Literal
* Use sys._getframemodulename() instead of sys._getframe() in inline reference impl
* Simplify, dropping support for custom repr and setting truthiness
* Tweak some wording
* A few more words about the advantages of the bare name for type signatures
* Correct __reduce__ in the reference impl
* Use the fully qualified name for the repr even when defined in a class
* Remove custom repr in the example for the sentinel decorator suggestion
@@ -421,21 +392,40 @@ idiom were unpopular, with the highest-voted option being voted for by only
421
392
25% of the voters.
422
393
423
394
395
+
Allowing customization of repr
396
+
------------------------------
397
+
398
+
This was desirable to allow using this for existing sentinel values without
399
+
changing their repr. However, this was eventually dropped as it wasn't
400
+
considered worth the added complexity.
401
+
402
+
403
+
Using ``typing.Literal`` in type annotations
404
+
--------------------------------------------
405
+
406
+
This was suggested by several people in discussions and is what this PEP
407
+
first went with. However, it was pointed out that this would cause potential
408
+
confusion, due to e.g. ``Literal["MISSING"]`` referring to the string value
409
+
``"MISSING"`` rather than being a forward-reference to a sentinel value
410
+
``MISSING``. Using the bare name was also suggested often in discussions.
411
+
This follows the precedent and well-known pattern set by ``None``, and has the
412
+
advantages of not requiring an import and being much shorter.
413
+
414
+
424
415
Additional Notes
425
416
================
426
417
427
418
* This PEP and the initial implementation are drafted in a dedicated GitHub
428
419
repo [7]_.
429
420
430
421
* For sentinels defined in a class scope, to avoid potential name clashes,
431
-
one should use the fully-qualified name of the variable in the module. Only
432
-
the part of the name after the last period will be used for the default
433
-
repr. For example::
422
+
one should use the fully-qualified name of the variable in the module. The
423
+
full name will be used as the repr. For example::
434
424
435
425
>>> class MyClass:
436
426
... NotGiven = sentinel('MyClass.NotGiven')
437
427
>>> MyClass.NotGiven
438
-
<NotGiven>
428
+
MyClass.NotGiven
439
429
440
430
* One should be careful when creating sentinels in a function or method, since
441
431
sentinels with the same name created by code in the same module will be
@@ -482,7 +472,6 @@ Footnotes
482
472
.. [7] `Reference implementation at the taleinat/python-stdlib-sentinels GitHub repo <https://github.com/taleinat/python-stdlib-sentinels>`_
483
473
.. [8] `bpo-35712: Make NotImplemented unusable in boolean context <https://github.com/python/cpython/issues/79893>`_
484
474
.. [9] `Discussion thread about type signatures for these sentinels on the typing-sig mailing list <https://mail.python.org/archives/list/[email protected]/thread/NDEJ7UCDPINP634GXWDARVMTGDVSNBKV/#LVCPTY26JQJW7NKGKGAZXHQKWVW7GOGL>`_
485
-
.. [10] `sentinels package on PyPI <https://pypi.org/project/sentinels/>`_
0 commit comments