Registry / web-framework / acquisition

acquisition

JSON →
library6.2pypypi✓ verified 52d ago

Acquisition is a mechanism that allows objects to obtain attributes from the containment hierarchy they're in. This foundational Zope library, currently at version 6.2, facilitates dynamic attribute lookup based on an object's position within a hierarchy or explicitly set context. While it does not have a fixed release cadence, it is actively maintained by the Zope Foundation.

web-framework
pip install Acquisition
Install & Compatibility
Where this runs
tested against v6.2 · 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.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.0s · import 0.055s · 21MB
19MB installed
● package 19MB
Code
Verified usage

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

Implicit
from Acquisition import Implicit
import Acquisition.Implicit
Common mistake is to try and import 'Implicit' directly as a submodule rather than from the Acquisition package.
aq_acquire
from Acquisition import aq_acquire
obj.aq_acquire
While 'obj.aq_acquire' is a method on an acquirable object, 'aq_acquire' can also be imported as a module function for explicit acquisition calls.

This example demonstrates basic implicit acquisition. An object `b` acquires attributes from `a` when `a` is set as its context using `__of__`. Subsequent objects in the context chain can also acquire attributes from higher up the hierarchy.

from Acquisition import Implicit class C(Implicit): pass a = C() b = C() a.color = "red" # 'b' acquires 'color' from 'a' because 'a' is put into b's context print(b.__of__(a).color) class X(Implicit): pass x = X().__of__(b.__of__(a)) # 'x' acquires 'color' from 'a' through 'b' print(x.color)
Debug
Known issues
gotchaC methods defined in extension base classes that define their own data structures cannot use acquired attributes. This is because acquisition wrapper objects do not conform to the data structures expected by these methods, leading to potential `AttributeError` or `TypeError` if invoked on an wrapped object.
fix
Avoid using Acquisition with C methods in extension base classes that rely on specific internal data structures. Consider refactoring to use Python methods or explicitly unwrapping the object (`obj.aq_base`) before calling C methods, though this removes acquisition capabilities for that call.
affects: All versions
gotchaThe behavior of acquisition can become complex due to subtle differences between 'acquiring from context' and 'acquiring from containment'. Misunderstanding these distinctions can lead to unexpected attribute resolution or attributes not being found.
fix
Carefully review the official documentation on explicit vs. implicit acquisition and the roles of `__of__` (context) and object containment. Debug acquisition paths using `obj.aq_chain()` to visualize the hierarchy and `obj.aq_base` to get the unwrapped object.
affects: All versions
gotchaOver-reliance on implicit acquisition can make code harder to reason about and debug, as attribute sources are not immediately obvious. This can lead to unexpected side effects or performance overhead from dynamic lookups.
fix
Use acquisition judiciously for genuinely hierarchical patterns. For critical paths or where clarity is paramount, consider more explicit dependency injection or passing attributes directly rather than relying solely on the acquisition chain.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'ClassName' object has no attribute 'attribute_name'
The requested attribute 'attribute_name' could not be found on the object itself, nor was it successfully acquired from its containment hierarchy or explicit context. This often means the attribute is missing, or the acquisition path is not correctly configured.
fix
Inspect the object's acquisition chain using `obj.aq_chain()` to verify the available parents and their attributes. Ensure the attribute exists on one of the objects in the expected acquisition path. Use `obj.aq_acquire(attribute_name, default=None)` to safely test for acquirable attributes.
ModuleNotFoundError: No module named 'Acquisition'
The 'Acquisition' library is not installed in the current Python environment, or the environment is not correctly activated.
fix
Install the library using pip: `pip install Acquisition`. If using a virtual environment, ensure it is activated before installation and execution.
TypeError: descriptor '__of__' requires a 'ClassName' object but received a 'AnotherClass' object
The `__of__` method, used to set acquisition context, expects an instance of an Acquisition-aware class (e.g., one that inherits from `Acquisition.Implicit` or `Acquisition.Explicit`). Providing an object of a different type will raise a TypeError.
fix
Ensure that both the object on which `__of__` is called and the object passed as its argument are instances of classes that properly support Acquisition, typically by inheriting from `Acquisition.Implicit` or `Acquisition.Explicit`.
Upgrade
Version history
6.2latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
81 hits · last 30 days
node
14
claudebot
4
ahrefsbot
3
applebot
1
petalbot
1
amazonbot
1
Resources