Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.108s · 49.8MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 3.1s · import 0.106s · 50MB
48MB installed
● package 48MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
TimestampAttribute
✓ from pynamodb_attributes import TimestampAttribute
✗ from pynamodb.attributes import TimestampAttribute
Custom attributes from this library are imported directly from `pynamodb_attributes`, not from PynamoDB's built-in attributes.
TimedeltaMsAttribute
✓ from pynamodb_attributes import TimedeltaMsAttribute
Used for serializing Python `timedelta` objects to milliseconds.
IntegerSetAttribute
✓ from pynamodb_attributes import IntegerSetAttribute
Allows storing a set of integers, providing stronger type guarantees than `NumberSetAttribute`.
UnicodeProtobufEnumAttribute
✓ from pynamodb_attributes import UnicodeProtobufEnumAttribute
Serializes Python Enums to DynamoDB strings, compatible with Protobuf enum definitions.
This quickstart demonstrates how to define a PynamoDB model using custom attributes like `TimestampAttribute`, `TimedeltaMsAttribute`, and `IntegerSetAttribute`. It shows how to initialize an item with these types and how they would be stored and retrieved (commented out save/get operations requiring a running DynamoDB instance).
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
from pynamodb_attributes import TimestampAttribute, TimedeltaMsAttribute, IntegerSetAttribute
import datetime
# Configure PynamoDB (for local DynamoDB for example)
# from pynamodb.connection import Connection
# Connection.tables = {}
class MyModel(Model):
class Meta:
table_name = 'my_test_model_table'
region = 'us-east-1'
# host = 'http://localhost:8000' # Uncomment for local DynamoDB
read_capacity_units = 1
write_capacity_units = 1
id = UnicodeAttribute(hash_key=True)
created_at = TimestampAttribute(null=True)
processing_time = TimedeltaMsAttribute(null=True)
favorite_numbers = IntegerSetAttribute(null=True)
# Example Usage:
if __name__ == '__main__':
# MyModel.create_table(wait=True) # Uncomment to create table
item = MyModel(
id='item-123',
created_at=datetime.datetime.now(datetime.timezone.utc),
processing_time=datetime.timedelta(seconds=5, milliseconds=250),
favorite_numbers={1, 5, 10}
)
# item.save() # Uncomment to save item
# retrieved_item = MyModel.get('item-123') # Uncomment to retrieve item
# print(f"Retrieved: {retrieved_item.id}, Created: {retrieved_item.created_at}, "
# f"Processed in: {retrieved_item.processing_time}, Favorites: {retrieved_item.favorite_numbers}")
Errors
Common errors & fixes
AttributeError: module 'pynamodb.attributes' has no attribute 'TimestampAttribute'
Attempting to import custom attributes from `pynamodb.attributes` instead of `pynamodb_attributes`.
fixChange your import statement from `from pynamodb.attributes import TimestampAttribute` to `from pynamodb_attributes import TimestampAttribute`.
TypeError: argument of type 'float' is not iterable (for IntegerSetAttribute) or TypeError: 'float' object cannot be interpreted as an integer (for IntegerAttribute)
Attempting to assign a float value or a set containing floats to an `IntegerSetAttribute` or `IntegerAttribute` which expects strict integer types.
fixEnsure all values passed to `IntegerSetAttribute` or `IntegerAttribute` are Python `int` types. Explicitly cast floats to integers if needed (e.g., `int(value)`).
pynamodb.exceptions.PynamoDBException: The provided attribute value is not of type N (for attribute: 'created_at')
You are attempting to store a non-numeric type (e.g., a string) into an attribute that expects a Number type in DynamoDB, such as `TimestampAttribute` or `TimedeltaAttribute`.
fixEnsure you are passing a Python `datetime.datetime` object to `TimestampAttribute` or a `datetime.timedelta` object to `TimedeltaAttribute`. These attributes handle the conversion to a numeric type automatically.
Upgrade
Version history
0.5.0latest on PyPI · released Mar 12, 2024
Audit
Dependencies
pynamodbrequiredThis library provides custom attributes for PynamoDB models and requires PynamoDB to function.