Skip to main content

Standalone back-port of Python 3.15's `sentinel` builtin.

Project description

sentinel

A back-port of Python 3.15's sentinel builtin for older Python versions.

On Python 3.15+, you can write:

MISSING = sentinel("MISSING")
DEFAULT = sentinel("DEFAULT", repr="<DEFAULT>")

This package provides the same behaviour on every supported Python version (3.10+) via a small, focused API.

Why?

Sentinel objects are an idiomatic way to distinguish “no value provided” from “value is None” — useful for default arguments, optional caches, and any function parameter that needs a tri-state (unset / None / real value).

from sentinel import sentinel

MISSING = sentinel("MISSING")

def fetch(key: str, default=None):
    """``default`` may legitimately be ``None``; ``MISSING`` means
    ``fetch`` should compute the fallback itself."""
    ...

Installation

pip install pythonbackport-sentinel

Usage

Basic sentinel

from sentinel import sentinel

MISSING = sentinel("MISSING")
MISSING
# -> 'MISSING'

Custom representation

from sentinel import sentinel

MISSING = sentinel("MISSING", repr="<MISSING>")
MISSING
# -> '<MISSING>'

Truthy and unique

from sentinel import sentinel

A = sentinel("A")
B = sentinel("A")      # a *different* object with the same name

assert A                # truthy
assert A is A           # identity equality
assert A is not B       # even with the same name, A != B

Type hints with |

from sentinel import sentinel

MISSING = sentinel("MISSING")

def next_value(default: int | MISSING = MISSING):
    ...

Pickle support (module scope)

import pickle
from sentinel import sentinel

PICKLABLE = sentinel("PICKLABLE")
assert pickle.loads(pickle.dumps(PICKLABLE)) is PICKLABLE

Pickle support (class scope)

import pickle
from sentinel import sentinel

class Cls:
    PICKLABLE = sentinel("Cls.PICKLABLE")

assert pickle.loads(pickle.dumps(Cls.PICKLABLE)) is Cls.PICKLABLE

API

sentinel(name, /, *, repr=None)

Return a new unique sentinel object.

  • name (positional-only, required) — str; the sentinel's name, used as the default representation.
  • repr (keyword-only, optional) — str; an alternative representation. Defaults to name.

Attributes on the returned object

Attribute Read Write Description
__name__ The sentinel's name.
__module__ The module that created the sentinel (writable, str).

Behaviour summary

  • Always truthy (bool(s) is True).
  • Equal only to itself; use the is operator.
  • Cannot be subclassed.
  • copy.copy(s) and copy.deepcopy(s) both return s.
  • s | T and T | s build a usable Union type for annotations.
  • Pickling preserves identity when the sentinel is defined at module or class scope with a name that matches the variable (or, for class scope, the qualified name).

Python 3.15+ native syntax

When you are running on Python 3.15+, sentinel is a true builtin. The package's own API still works identically, so you can use either:

# Native (Python 3.15+ only):
MISSING = sentinel("MISSING")

# Cross-version equivalent via this package:
from sentinel import sentinel
MISSING = sentinel("MISSING")

Compatibility

  • Python 3.10 through 3.15 (uses PEP 604 unions; falls back to typing.Union on older interpreters).
  • No third-party dependencies — uses only the standard library (pickle, sys, types).

Running the tests

python -m unittest test_sentinel.py -v

Retirement

This project will reach its end-of-life around October 1, 2031 — the official EOL date of Python 3.15 — and the exact timeline could be slightly delayed. We plan to ship the final stable release in November 2031. After this release, all support will cease and the repository will be officially archived, as this library is developed solely to bring sentinel() compatibility to Python 3.15 and older versions.

License

MIT

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

pythonbackport_sentinel-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

pythonbackport_sentinel-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pythonbackport_sentinel-0.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for pythonbackport_sentinel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b9d800d8b758ed96ceb3f518212f301f52ca93b68934a73ab80dd70331a637cb
MD5 5505c4d12d45113049409019175503b5
BLAKE2b-256 e04cdc620dc4f47780017085fd43a828d521a488c9e3579390232ae06b805aee

See more details on using hashes here.

File details

Details for the file pythonbackport_sentinel-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pythonbackport_sentinel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31833cd5826a2d6453b61c617b16cb20340355031269fe7945f475c8121ed7a9
MD5 291b0153e0918765bbda4911bd815ca5
BLAKE2b-256 91bb77aac4fcaae81650bbb3cb7923ab401726ccff596ce17c0daa64b7bd0ac4

See more details on using hashes here.

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