Statically-typed sentinel objects with singleton qualities.
Project description
typed-sentinels
Statically-typed sentinel objects with singleton qualities.
Installation
pip install typed-sentinels
Quick Start
Sentinelinstances are useful for creating unique, type-annotated placeholders, typically used when a default value other thanNoneis desirable. Only a single instance ofSentinelmay exist for a given type.
from typed_sentinels import Sentinel
# Annotate with the type the sentinel should appear to be to the type-checker
SENTINEL_STR: str = Sentinel(str)
def process_data(value: str = SENTINEL_STR) -> str:
# Sentinels are always falsy
if not value:
return 'No value provided'
return f'Processing: {value}'
# Type-safe usage
result = process_data() # "No value provided"
result = process_data('demo123') # "Processing: demo123"
- The
Sentinelclass is 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.
from typed_sentinels import Sentinel
class Custom:
@property
def value(self) -> str:
return self._value
def __init__(self, required: str, /) -> None:
self._value = required
# Appears to the type-checker as an instance of `Custom`
CUSTOM: Custom = Sentinel(Custom)
def func(c: Custom = CUSTOM) -> str:
if c is not CUSTOM:
return c.value
return 'c was not provided'
Sentinelinstances have singleton qualities, but remain distinct as specific to the type they represent:
S1 = Sentinel(dict[str, Any])
S2 = Sentinel(dict[str, Any])
S3 = Sentinel(dict[str, bytes])
assert S1 is S2 # True
assert S2 is not S3 # True
- In the simplest form, supposing you don't want or need a distinct instance for different types,
you could use the
Sentinelwithout parameterization or arguments, and the type-checker will still be happy:
class Custom:
def __init__(self, arg1: str, arg2: int, arg3: bool) -> None: ...
# This appears to the type-checker as an actual instance of Custom
CUSTOM: Custom = Sentinel()
reveal_type(CUSTOM) # Runtime type is 'Sentinel'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typed_sentinels-1.1.0.tar.gz.
File metadata
- Download URL: typed_sentinels-1.1.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58eece3b511890628026933a0b3fe7bd68db56116823b5d28287dbbc90779474
|
|
| MD5 |
f17ec601b1459bc8f40e65fb07b8589f
|
|
| BLAKE2b-256 |
9b963bf512698ee1fbbfea6148978f5a49f6f518320b68ae059f2559477f8a45
|
Provenance
The following attestation bundles were made for typed_sentinels-1.1.0.tar.gz:
Publisher:
build-and-publish.yml on Blockmage/typed_sentinels
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typed_sentinels-1.1.0.tar.gz -
Subject digest:
58eece3b511890628026933a0b3fe7bd68db56116823b5d28287dbbc90779474 - Sigstore transparency entry: 458613775
- Sigstore integration time:
-
Permalink:
Blockmage/typed_sentinels@bdfa5b36729f52bf23f54fbd7c218a989f35ce2b -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/Blockmage
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yml@bdfa5b36729f52bf23f54fbd7c218a989f35ce2b -
Trigger Event:
push
-
Statement type:
File details
Details for the file typed_sentinels-1.1.0-py3-none-any.whl.
File metadata
- Download URL: typed_sentinels-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8a68ff086a0990fcf7d075104cddd07f7e258cea02ba112f574b63cbb12c686
|
|
| MD5 |
457f179f54148a2ca97d411643c00aa4
|
|
| BLAKE2b-256 |
803d0f20c2c8a14a8b389040c440ba9364c0137f3fafdb24f65dad345a0fe2e0
|
Provenance
The following attestation bundles were made for typed_sentinels-1.1.0-py3-none-any.whl:
Publisher:
build-and-publish.yml on Blockmage/typed_sentinels
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typed_sentinels-1.1.0-py3-none-any.whl -
Subject digest:
a8a68ff086a0990fcf7d075104cddd07f7e258cea02ba112f574b63cbb12c686 - Sigstore transparency entry: 458613803
- Sigstore integration time:
-
Permalink:
Blockmage/typed_sentinels@bdfa5b36729f52bf23f54fbd7c218a989f35ce2b -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/Blockmage
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yml@bdfa5b36729f52bf23f54fbd7c218a989f35ce2b -
Trigger Event:
push
-
Statement type: