Core¶
typed_sentinels._core
¶
Classes¶
Sentinel
¶
Statically-typed sentinel object with singleton qualities.
Sentinel
instances provide unique placeholder objects that maintain singleton behavior for a given type. They are
particularly well-suited for use with types requiring parameters which are only available at runtime, where creating
a default instance of the type may not be possible in advance, but the structural contract of the type is otherwise
guaranteed to be fulfilled once present.
Examples:
Basic usage:
from typed_sentinels import Sentinel
SNTL = Sentinel() # Same as `typing.Any`
SNTL_BYTES = Sentinel(bytes) # Same as `bytes`
SNTL_TUPLE = Sentinel[tuple[str, ...]]() # Same as `tuple[str, ...]`
assert SNTL is not SNTL_BYTES # True
assert Sentinel() is SNTL # True - `Sentinel` objects are singletons specific to the assigned type
assert Sentinel(tuple[str, ...]) is SNTL_TUPLE # True
assert Sentinel(tuple[bytes, ...]) is not SNTL_TUPLE # True
Sentinel
objects are indistinguishable from an instance of the assigned type to the type-checker:
from typing import reveal_type
class Custom:
def __init__(self, req_str: str, req_bytes: bytes) -> None:
if not req_str or not req_bytes:
raise ValueError
SNTL_CUSTOM = Sentinel(Custom)
reveal_type(SNTL_CUSTOM) # Revealed type is `Custom` -> Runtime type is `Sentinel`
def example_func(b: bytes = SNTL_BYTES, c: Custom = SNTL_CUSTOM) -> Any:
if not b:
print('Sentinels are falsey so this check works')
if not tup:
...
This even works for complex types like Callable
:
# Note: It's incorrect to pass `Callable` directly to the constructor; instead, parameterize by subscription:
SNTL_CALLABLE = Sentinel[Callable[..., str]]()
reveal_type(SNTL_CALLABLE) # Type of "SNTL_CALLABLE" is "(...) -> str"
Attributes¶
Functions¶
__new__
¶
Create or retrieve a Sentinel
instance for the given hint
type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hint
|
T
|
Type that this |
_OBJECT
|
Returns:
Type | Description |
---|---|
T
|
|
Raises:
Type | Description |
---|---|
InvalidHintError
|
If |
SubscriptedTypeError
|
If provided both a subscripted type parameter and a direct type argument and the types should differ (e.g.,
|
Source code in src/typed_sentinels/_core.py
__class_getitem__
¶
Set the class-level _cls_hint
to key
and return the class object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
T
|
Type to be associated with the class and inherited by future instances. |
required |
Returns:
Type | Description |
---|---|
T
|
|
Source code in src/typed_sentinels/_core.py
__getitem__
¶
Return the Sentinel
instance for indexing operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
T
|
Index or key (ignored in this implementation). |
required |
Returns:
Type | Description |
---|---|
T
|
|
Source code in src/typed_sentinels/_core.py
__call__
¶
__str__
¶
Return a string representation of the Sentinel
instance.
Returns:
Type | Description |
---|---|
str
|
String value in the format of |
Source code in src/typed_sentinels/_core.py
__repr__
¶
Return a detailed string representation of the Sentinel
instance.
Returns:
Type | Description |
---|---|
str
|
String value in the format of |
__hash__
¶
Return a hash value derived from the instance __class__
and hint
.
Returns:
Type | Description |
---|---|
int
|
Hash of the tuple |
__bool__
¶
Return False
, as Sentinel
objects are always falsey.
Returns:
Type | Description |
---|---|
Literal[False]
|
Always |
__eq__
¶
Check equality with another object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
object
|
Object with which to compare. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/typed_sentinels/_core.py
__copy__
¶
Return the Sentinel
instance for shallow copy operations.
Returns:
Type | Description |
---|---|
Sentinel[T]
|
|
__deepcopy__
¶
__reduce__
¶
__reduce_ex__
¶
Support for pickle serialization with protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol
|
SupportsIndex
|
The pickle protocol (ignored, delegates to |
required |
Returns:
Type | Description |
---|---|
tuple[Callable[..., Sentinel[T]], tuple[T]]
|
Tuple containing the instance |
Source code in src/typed_sentinels/_core.py
__setattr__
¶
Raise an error to prevent attribute modification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Attribute name (ignored). |
required |
value
|
Any
|
Value to be set (ignored). |
required |
Raises:
Type | Description |
---|---|
AttributeError
|
Always raised. |
Source code in src/typed_sentinels/_core.py
__delattr__
¶
Raise an error to prevent attribute deletion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Attribute name (ignored). |
required |
Raises:
Type | Description |
---|---|
AttributeError
|
Always raised. |
Source code in src/typed_sentinels/_core.py
Functions¶
is_sentinel
¶
Return True
if obj
is a Sentinel
instance, optionally further narrowed to be of typ
type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
Possible |
required |
typ
|
T | None
|
Optional type to be used to further narrow the type of the |
None
|
Returns:
Type | Description |
---|---|
TypeGuard[Sentinel[T]]
|
|