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-0.1.0.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-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typed_sentinels-0.1.0.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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 77e5cd0c1744cbee40b2b562eeb1316291865cb0568d1570e232d7d45446888c
MD5 1fbfef19843b73ea9828e715de677715
BLAKE2b-256 c74f34fe855eb83708b6f06c491a9f1c714b23345bad08857e67c490d119beb5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for typed_sentinels-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c4801736680df8403a5db8f36b8c3deaddd06f2c5de5a4fbac0cff85dac050b
MD5 d8d91e063f851122fd3625f805572f7e
BLAKE2b-256 a83e6c7b63b3ae9ab6b00c9996dab616447f283471329b478daa016fdf7fc0d6

See more details on using hashes here.

Provenance

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