-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel.py
More file actions
29 lines (19 loc) · 854 Bytes
/
Copy pathsentinel.py
File metadata and controls
29 lines (19 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""The Sentinel Object pattern as importable, typed building blocks.
``Sentinel`` is a named, unforgeable marker (a PEP 661-inspired shape;
unlike the PEP's proposal, two same-named sentinels here are deliberately
distinct objects — tested). ``MISSING`` is the one most APIs need. A
sentinel's identity is its meaning: compare with ``is``, never ``==``.
"""
from __future__ import annotations
class Sentinel:
"""A unique marker object with a readable repr.
>>> MISSING = Sentinel("MISSING")
>>> value is MISSING # identity is the only correct check
"""
__slots__ = ("_name",)
def __init__(self, name: str) -> None:
self._name = name
def __repr__(self) -> str:
return f"<{self._name}>"
#: The workhorse: "no value here", even where None is a legitimate value.
MISSING = Sentinel("MISSING")