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.0.tar.gz (7.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.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

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

Hashes for typed_sentinels-1.1.0.tar.gz
Algorithm Hash digest
SHA256 58eece3b511890628026933a0b3fe7bd68db56116823b5d28287dbbc90779474
MD5 f17ec601b1459bc8f40e65fb07b8589f
BLAKE2b-256 9b963bf512698ee1fbbfea6148978f5a49f6f518320b68ae059f2559477f8a45

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for typed_sentinels-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8a68ff086a0990fcf7d075104cddd07f7e258cea02ba112f574b63cbb12c686
MD5 457f179f54148a2ca97d411643c00aa4
BLAKE2b-256 803d0f20c2c8a14a8b389040c440ba9364c0137f3fafdb24f65dad345a0fe2e0

See more details on using hashes here.

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

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