absence¶
🕳️ 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.
📦 Absence Cell: Immutable container wrapping absent values with conditional extraction, evaluation, and transformation API.
𝒇 Predicate Functions: Determine if a value is absent.
🔍 Type Support: Type alias for optional values which may be absent. (Similar to
typing.Optionaland its relation toNone.)🌟 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_present, 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 is_present( name ): user.name = name
... if is_present( 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
Wrap absent values with AbsenceCell for conditional chains:
>>> from absence import AbsenceCell, absent, Absential
>>> def adjust_columns( width: Absential[ int ] ) -> int:
... ''' Returns adjusted width, or 0 if width is absent. '''
... return AbsenceCell( width ).evaluate_or( lambda w: w - 4, 0 )
>>> adjust_columns( 80 )
76
>>> adjust_columns( absent )
0
Use Cases 🎯¶
🔄 Optional Arguments: When
Noneis 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 |
|---|---|---|---|---|
|
Yes |
Yes |
No |
Arbitrary |
PEP 661 Sentinels |
Optional |
Yes |
Yes |
Per-Module |
|
Yes |
Yes |
No |
Global |
|
Yes |
Yes |
Yes |
Global |
|
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.
Installation 📦¶
Method: Install Python Package¶
Install via uv pip
command:
uv pip install absence
Or, install via pip:
pip install absence
Contribution 🤝¶
Contribution to this project is welcome! However, it must follow the code of conduct for the project.
Please file bug reports and feature requests in the issue tracker or submit pull requests to improve the source code or documentation.
For development guidance and standards, please see the development guide.