Skip to main content

Statically-typed sentinel objects with singleton qualities.

Project description

typed-sentinels

Statically-typed sentinel objects with singleton qualities (for Python 3.12+).

Installation

pip install typed-sentinels

Quick Start

  • Sentinel instances are useful for creating unique, type-annotated placeholders, typically used when a default value other than None is desirable. Only a single instance of Sentinel may 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 Sentinel class 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'
  • Sentinel instances 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 Sentinel without 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'

Example of Sentinel class mimicking parameterized type

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typed_sentinels-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typed_sentinels-1.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file typed_sentinels-1.0.1.tar.gz.

File metadata

  • Download URL: typed_sentinels-1.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for typed_sentinels-1.0.1.tar.gz
Algorithm Hash digest
SHA256 429e188449d7c062865b0fd785e9c5f0cd23f2899232439ad30d255f144e1b4d
MD5 33a808d1d5088ec42af626ed0b8d1783
BLAKE2b-256 7836f2bdd3f12225362894a7dee4d9f89936e7ea2ccc55ed8207462221834712

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_sentinels-1.0.1.tar.gz:

Publisher: build-and-publish.yml on Blockmage/typed_sentinels

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typed_sentinels-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_sentinels-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 95c63440a346a56a8f2545ed2f697c8b65c5b260cc3c7f4c8c70de6928cae289
MD5 764def1c1716ea30968f9a4e379e0e13
BLAKE2b-256 1317fe413d3c77a84b8bdfc7ba2e339b0990ab07cd21ef776e07d9b29c039022

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_sentinels-1.0.1-py3-none-any.whl:

Publisher: build-and-publish.yml on Blockmage/typed_sentinels

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page