Skip to main content

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

  • 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.1.1.tar.gz (5.6 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.1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for typed_sentinels-1.1.1.tar.gz
Algorithm Hash digest
SHA256 f7f93e8afee66ccd41ce9d8ec1e12cef0fcbd4eafefebec7767ac33c7f7ec611
MD5 557e55f2eb33f93104a38125d37a65e9
BLAKE2b-256 d80918e3901c675429c5f0d34ea4d949142010594fafef2dad41d1b435fc251d

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_sentinels-1.1.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.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_sentinels-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11b71aa2a75b3523e4f8505cd00e0ba0be437d87d4f2f733c1a9cb3116de9ceb
MD5 9a649b3fee53bacf2e56ac056d10fe37
BLAKE2b-256 28b94c693c3257982aea39785ba6abef86ad992c7e33ebb3ebd2a3a807240cdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_sentinels-1.1.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