Skip to content

emcd/python-absence

Repository files navigation

absence

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 = '[email protected]' )
>>> # Clear name but leave email unchanged
>>> updated = apply_partial_update( user, name = None )
>>> updated.name  # Cleared to None
>>> updated.email  # Unchanged
'[email protected]'
>>> # Update both fields
>>> updated = apply_partial_update( user, name = 'Bob', email = '[email protected]' )
>>> updated.name
'Bob'
>>> updated.email
'[email protected]'

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.

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

Other Projects by This Author 🌟

  • python-accretive (accretive on PyPI)

    🌌 A Python library package which provides accretive data structures - collections which can grow but never shrink.

  • python-classcore (classcore on PyPI)

    🏭 A Python library package which provides foundational class factories and decorators for providing classes with attributes immutability and concealment and other custom behaviors.

  • python-dynadoc (dynadoc on PyPI)

    📝 A Python library package which bridges the gap between rich annotations and automatic documentation generation with configurable renderers and support for reusable fragments.

  • python-falsifier (falsifier on PyPI)

    🎭 A very simple Python library package which provides a base class for falsey objects - objects that evaluate to False in boolean contexts.

  • python-frigid (frigid on PyPI)

    🔒 A Python library package which provides immutable data structures - collections which cannot be modified after creation.

  • python-icecream-truck (icecream-truck on PyPI)

    🍦 Flavorful Debugging - A Python library which enhances the powerful and well-known icecream package with flavored traces, configuration hierarchies, customized outputs, ready-made recipes, and more.

  • python-mimeogram (mimeogram on PyPI)

    📨 A command-line tool for exchanging collections of files with Large Language Models - bundle multiple files into a single clipboard-ready document while preserving directory structure and metadata... good for code reviews, project sharing, and LLM interactions.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages