Skip to main content

Sentinel for absent values.

Project description

Package Version PyPI - Status Tests Status Code Coverage Percentage Project License Python Versions

🕳️ A Python library package which provides a sentinel for absent values - a falsey, immutable singleton that represents the absence of a value in contexts where None or False may be valid values.

Key Features ⭐

  • 1️⃣ Absence Sentinel: A falsey singleton which represents absence.

  • 🏭 Absence Factory: Create custom absence sentinels for package-specific or arbitrary needs.

  • 𝒇 Predicate Functions: Determine if a value is absent.

  • 🔍 Type Support: Type alias for optional values which may be absent. (Similar to typing.Optional and its relation to None.)

  • 🌟 Builtins Integration: Can install singleton and predicate function into Python builtins.

Installation 📦

pip install absence

Examples 💡

Use the absent sentinel to represent missing values:

>>> from dataclasses import dataclass
>>> from absence import absent, is_absent, Absential
>>> @dataclass
... class User:
...     name: str | None
...     email: str | None
>>> def apply_partial_update(
...     user: User,
...     name: Absential[ str | None ] = absent,
...     email: Absential[ str | None ] = absent,
... ) -> User:
...     ''' Updates user fields if values provided.
...
...         Absent value means "don't change".
...         None value means "clear field".
...     '''
...     if not is_absent( name ): user.name = name
...     if not is_absent( email ): user.email = email
...     return user
>>> user = User( name = 'Alice', email = 'alice@example.com' )
>>> # Clear name but leave email unchanged
>>> updated = apply_partial_update( user, name = None )
>>> updated.name  # Cleared to None
>>> updated.email  # Unchanged
'alice@example.com'
>>> # Update both fields
>>> updated = apply_partial_update( user, name = 'Bob', email = 'bob@example.com' )
>>> updated.name
'Bob'
>>> updated.email
'bob@example.com'

Create package-specific absence sentinels:

>>> from absence import AbsenceFactory
>>> MISSING = AbsenceFactory( )
>>> bool( MISSING )
False

Use Cases 🎯

  • 🔄 Optional Arguments: When None is a valid argument value but you need to detect absence.

  • 🎯 Sentinel Values: When you need a unique, falsey object to represent missing or invalid states.

  • 🧩 Type Safety: When you want explicit typing for values that may be absent.

Comparison with Alternatives 🤔

Alternative

Truthy?

Unique?

Picklable?

Scope

object()

Yes

Yes

No

Arbitrary

PEP 661 Sentinels

Optional

Yes

Yes

Per-Module

dataclasses.MISSING

Yes

Yes

No

Global

typing.NoDefault

Yes

Yes

Yes

Global

absence.absent

No

Yes

No

Global

The absent sentinel combines falsey behavior with global uniqueness, making it particularly suitable for representing missing values in contexts where None might be a valid value. The companion AbsenceFactory allows creation of arbitrary absence sentinels, when needed, such as for specific packages.

See PEP 661 (“Sentinel Values”), typing.NoDefault, and dataclasses.MISSING for more details on alternatives.

More Flair

GitHub last commit Copier Hatch pre-commit Bandit Pylint Pyright Ruff PyPI - Implementation PyPI - Wheel

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

absence-1.0.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

absence-1.0.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: absence-1.0.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for absence-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6cf3a052d5e89edc58e4624d9f8d87232d038052f08a2ff9bab4071e1b592783
MD5 7544b614bf0b67ca347cea1463ee641d
BLAKE2b-256 fcbd1087c348d8ac98a94bb564354596d32b5e8337fd444c7a9f0a26673b85ae

See more details on using hashes here.

Provenance

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

Publisher: releaser.yaml on emcd/python-absence

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

File details

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

File metadata

  • Download URL: absence-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for absence-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cfff30bc7e15b74b2bcde9af61c6954fadc50c8a2bf377c8710d0032b3a806f1
MD5 36883f551a97c6a0a9bfebd0ac97b8db
BLAKE2b-256 81e378e55ab085db8956f5be5e8d186d41511ef059bd6cb94fd6b8f014d02e59

See more details on using hashes here.

Provenance

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

Publisher: releaser.yaml on emcd/python-absence

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