Registry / web-framework / zope-traversing

zope-traversing

JSON →
library6.0pypypi✓ verified 84d ago

Zope Traversing (current version 6.0) provides a robust and extensible mechanism for resolving paths within an object hierarchy, common in Zope-based applications and web frameworks. It allows developers to define how objects are looked up by name or through adapters, enabling flexible URL dispatching and content navigation. It has a steady release cadence, typically releasing new major versions every few years, with minor releases and bug fixes more frequently.

pip install zope-traversing
INSTALL
IMPORT
SIG · ZOPE-TRAVERSING
Z
zope-traversing
web-frameworkpythonv6.0
Install
3.9s avg
Import
233ms
Disk
37MB
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.239s · 33.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.9s · import 0.226s · 35MB
37MB installed
● package 37MB
Code
Verified usage

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

traverse
from zope.traversing.api import traverse
ITraversable
from zope.traversing.interfaces import ITraversable
IPathAdapter
from zope.traversing.interfaces import IPathAdapter
absoluteURL
from zope.traversing.browser import absoluteURL
from zope.traversing.browser import AbsoluteURL
The class AbsoluteURL was deprecated and removed in favor of the function absoluteURL in v4.0.

This quickstart demonstrates how to define a simple object hierarchy using `ITraversable` and then navigate it using `zope.traversing.api.traverse`. It sets up a root folder containing sub-folders and documents, and shows how to retrieve an item by its path, as well as how `KeyError` (or `zope.traversing.interfaces.NotFound` in more complex setups) is raised for non-existent paths.

from zope.interface import implementer from zope.traversing.interfaces import ITraversable from zope.traversing.api import traverse # Define a simple traversable object (e.g., a Folder) @implementer(ITraversable) class Folder: def __init__(self, name, parent=None): self.__name__ = name self.__parent__ = parent self.contents = {} def __setitem__(self, name, item): item.__name__ = name item.__parent__ = self self.contents[name] = item def __getitem__(self, name): if name in self.contents: return self.contents[name] raise KeyError(f"No item named '{name}'") def traverse(self, name, furtherPath): # furtherPath is usually ignored for simple dict-like traversal # Default traversal: look up in contents return self.contents[name] def __repr__(self): return f"<Folder '{self.__name__}'>" class Document: def __init__(self, name, content="", parent=None): self.__name__ = name self.__parent__ = parent self.content = content def __repr__(self): return f"<Document '{self.__name__}'>" # Build a hierarchy root = Folder('') products_folder = Folder('products') root['products'] = products_folder products_folder['book1'] = Document('book1', "The first book.") products_folder['book2'] = Document('book2', "The second book.") # Traverse to an item try: item = traverse(root, 'products/book1') print(f"Found item at 'products/book1': {item}") # Attempt to traverse to a non-existent item non_existent_item = traverse(root, 'products/nonexistent') print(f"Found non-existent item: {non_existent_item}") # This line won't be reached except KeyError as e: print(f"Error traversing to 'products/nonexistent': {e} (expected behavior)") # Traverse to a sub-path within an item if it also implements ITraversable # (not shown in this basic example for Document, but would work for nested Folders)
Debug
Known issues
breakingzope.traversing version 6.0 and higher require Python 3.9 or newer. Running on older Python versions will result in `ImportError` or `SyntaxError` depending on the exact Python features used by the package.
fix
Upgrade your Python environment to 3.9 or newer, or pin your `zope-traversing` dependency to a version compatible with your Python (e.g., `<6.0` for Python 3.8).
affects: >=6.0
deprecatedThe `zope.traversing.browser.AbsoluteURL` class and the `zope.traversing.browser.interfaces.IAbsoluteURL` interface were removed in version 4.0. Direct instantiation of `AbsoluteURL` or implementing `IAbsoluteURL` will no longer work.
fix
Use the simpler utility function `zope.traversing.browser.absoluteURL` directly. This function now provides the same functionality without requiring interface implementation.
affects: >=4.0
gotchaWhen implementing custom traversal logic using `ITraversable` or `IPathAdapter`, incorrect handling of names or edge cases can lead to `KeyError`, `AttributeError`, or even infinite recursion if not carefully managed. Ensure your `traverse` method explicitly raises `KeyError` (or an appropriate `NotFound` exception) for unresolvable names.
fix
Always explicitly handle cases where a name cannot be resolved in your `traverse` method by raising `KeyError` or a specific `NotFound` exception. For complex scenarios, consider using `zope.component.adapter` for `IPathAdapter` rather than implementing `ITraversable` directly on every object.
affects: all
Errors
Common errors & fixes
ImportError: cannot import name 'AbsoluteURL' from 'zope.traversing.browser'
Attempting to import the `AbsoluteURL` class, which was deprecated and removed in `zope.traversing` version 4.0.
fix
Instead of `from zope.traversing.browser import AbsoluteURL`, use the utility function: `from zope.traversing.browser import absoluteURL`.
TypeError: traverse() missing 1 required positional argument: 'path'
The `zope.traversing.api.traverse` function requires at least two arguments: the object to start traversing from and the path string.
fix
Ensure you call `traverse` with both the root object and the path, e.g., `traverse(root_object, 'segment1/segment2')`.
KeyError: 'some_missing_name'
The traversal mechanism (`ITraversable`'s `traverse` method or `IPathAdapter`) could not find an object matching the current path segment.
fix
This is often expected behavior. If it's not, verify the path string is correct, ensure the intermediate objects in the hierarchy are correctly set up and implement `ITraversable` (or have registered `IPathAdapter`s), and that the names exist within their respective containers.
Upgrade
Version history
6.0latest on PyPI · released Sep 12, 2025
Audit
Dependencies
zope.interfacerequiredCore Zope component for defining interfaces, central to Zope's object model and traversal contracts.
zope.locationrequiredProvides __name__ and __parent__ attributes, which are fundamental for defining an object hierarchy that traversal operates on.
zope.schemarequiredUsed for schema definitions, potentially for defining traversal-related configuration or data structures.
Agent activity
43 hits · last 30 days
node
34
OpenAI (training)
1
Resources
zope-traversing — pip install zope-traversing · libregistry