Registry / database / zope-annotation

zope-annotation

JSON →
library6.0pypypi✓ verified 84d ago

The `zope.annotation` package provides a robust object annotation mechanism, allowing objects to be transparently extended with additional information without modifying their original class. It is part of the Zope Foundation ecosystem, currently at version 6.0, and maintains a stable release cadence aligned with Python and Zope community standards.

pip install zope.annotation
INSTALL
IMPORT
SIG · ZOPE-ANNOTATION
Z
zope-annotation
databasepythonv6.0
Install
2.6s avg
Import
143ms
Disk
24MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.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
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.148s · 22.1MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.137s · 23MB
24MB installed
● package 24MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

IAnnotations
from zope.annotation.interfaces import IAnnotations
from zope.annotation import IAnnotations
Interfaces are typically found in the `.interfaces` submodule within Zope packages.
IAnnotatable
from zope.annotation.interfaces import IAnnotatable
Required to mark an object as capable of being annotated.

This example demonstrates how to make an arbitrary object annotatable using `directlyProvides(obj, IAnnotatable)` and then use the `IAnnotations` adapter to store and retrieve key-value data on it. This pattern is fundamental for extending objects without inheritance in Zope-based applications.

from zope.interface import directlyProvides from zope.annotation.interfaces import IAnnotations, IAnnotatable # Define a simple class that can be annotated class MyObject: pass # Instantiate the object obj = MyObject() # An object must provide IAnnotatable to be annotated. # In a real Zope application, this might be handled by decorators or base classes. # For a standalone example, we directly provide the interface. directlyProvides(obj, IAnnotatable) # Get the annotations adapter for the object # If no annotations exist, a new empty dictionary-like object is created. annotations = IAnnotations(obj) # Store and retrieve data on the object via annotations annotations['my_data_key'] = 'This is some data stored as an annotation.' annotations['another_key'] = {'list': [1, 2, 3], 'dict_val': 'value'} print(f"Annotation 'my_data_key': {annotations['my_data_key']}") print(f"Annotation 'another_key': {annotations['another_key']}") # Check if an annotation exists if 'my_data_key' in annotations: print("'my_data_key' exists in annotations.") # Delete an annotation del annotations['my_data_key'] if 'my_data_key' not in annotations: print("'my_data_key' successfully deleted.")
Debug
Known issues
breakingVersion 6.0 of `zope.annotation` dropped support for Python 3.8. It now requires Python 3.9 or newer.
fix
Upgrade your Python environment to 3.9 or a newer supported version. Alternatively, pin `zope.annotation` to `<6.0` if you must remain on Python 3.8.
affects: 6.0+
gotchaObjects must explicitly provide the `IAnnotatable` interface to be annotated. If an object does not provide this interface, attempting to adapt it to `IAnnotations` will fail with a `TypeError`.
fix
Ensure your object provides `IAnnotatable`. In simple cases, use `from zope.interface import directlyProvides; directlyProvides(obj, IAnnotatable)`. In more complex scenarios, objects might inherit from a base class providing `IAnnotatable` or have an adapter registered for it via `zope.component`.
affects: all
gotchaWhen using `zope.annotation` with persistent objects (e.g., in a ZODB), any values stored as annotations must also be persistent objects themselves (or basic Python types like int, str, dict, list of basic types). Storing non-persistent objects will lead to `TypeError` or `AttributeError` during storage.
fix
Ensure that the values you assign to `IAnnotations` are either basic Python types or objects that are themselves persistent (e.g., ZODB Persistent objects or types that can be pickled by ZODB).
affects: all
Errors
Common errors & fixes
TypeError: Could not adapt <__main__.MyObject object at 0x...> to <InterfaceClass zope.annotation.interfaces.IAnnotations>
The object you are trying to annotate does not provide the `IAnnotatable` interface, which is a prerequisite for `zope.annotation` to function.
fix
Before attempting to get `IAnnotations(obj)`, ensure `obj` provides `IAnnotatable`. For testing or simple cases, use `from zope.interface import directlyProvides; directlyProvides(obj, IAnnotatable)`.
ImportError: cannot import name 'IAnnotations' from 'zope.annotation'
The `IAnnotations` interface is not directly under the `zope.annotation` package but within its `interfaces` submodule.
fix
Change your import statement to `from zope.annotation.interfaces import IAnnotations`.
AttributeError: 'dict' object has no attribute '_p_jar'
You are attempting to store a non-persistent Python dictionary (or other non-persistent object) as an annotation on a persistent object within a ZODB, and ZODB is trying to make the *value* persistent but it doesn't know how.
fix
If you need a persistent dictionary for annotations, use a persistent collection type from `ZODB.blob` or `BTrees`. For example, `from persistent.mapping import PersistentMapping; annotations['my_persistent_dict'] = PersistentMapping({'key': 'value'})`.
Upgrade
Version history
6.0latest on PyPI · released Sep 12, 2025
Audit
Dependencies
zope.interfacerequiredProvides the core interface (`IAnnotations`, `IAnnotatable`) definitions and implementation features required by `zope.annotation`.
Agent activity
42 hits · last 30 days
node
37
OpenAI (training)
1
Resources
zope-annotation — pip install zope-annotation · libregistry