Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sentinels/sentinels.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ class Sentinel:

*module_name*, if supplied, will be used instead of inspecting the call
stack to find the name of the module from which

*truthy*, should be the value that is returned on Boolean evaluation.
if `None`, a `ValueError` is raised.
"""
_name: str
_repr: str
_module_name: str
_truthy: str | None

def __new__(
cls,
name: str,
repr: str | None = None,
module_name: str | None = None,
truthy: bool | None = None,
):
name = str(name)
repr = str(repr) if repr else f'<{name.split(".")[-1]}>'
Expand All @@ -71,9 +76,16 @@ def __new__(
sentinel._name = name
sentinel._repr = repr
sentinel._module_name = module_name
sentinel._truthy = truthy
with _lock:
return _registry.setdefault(registry_key, sentinel)

def __bool__(self) -> bool:
if self._truthy is None:
raise ValueError(f"{self._name} is ambiguous.")
else:
return self._truthy

def __repr__(self):
return self._repr

Expand Down