Cyclic
The `cyclic` Python library, currently at version 1.0.0, is designed to handle and detect cyclic relations between objects. It allows you to add parent-child relationships and then query if a specific object is part of a cyclic dependency. The library has a low release cadence, with its last update in 2018.
Common errors
-
from cyclic import Cyclic; ModuleNotFoundError: No module named 'cyclic'
cause The `cyclic` package has not been installed in your Python environment.fixRun `pip install cyclic` to install the package. -
AttributeError: 'Cyclic' object has no attribute 'add_relation' (or similar method name)
cause Incorrect method name used. The primary method for adding relationships is `add(child, parent)`.fixUse the correct method `cy.add(child_object, parent_object)` to establish a dependency. -
TypeError: unhashable type: 'list' (or other mutable type) when using `add()` or `is_cyclic()`
cause The `cyclic` library internally uses sets for tracking relationships, requiring that the objects involved in dependencies (both child and parent) are hashable. Mutable types like lists or dictionaries are not hashable by default.fixEnsure that the objects you are tracking with `cyclic.Cyclic` are immutable and thus hashable (e.g., strings, numbers, tuples, or custom objects with a `__hash__` method). If you need to track mutable objects, use a unique immutable identifier for each object instead.
Warnings
- gotcha The `cyclic` library is designed to *detect* circular dependencies based on added relationships, not to prevent Python's own circular import errors during module loading. If you encounter `ImportError` due to modules importing each other, this library will not resolve that underlying architectural issue.
- deprecated The `cyclic` library has not been updated since 2018. While it appears stable for its specific use case, its long-term maintenance and compatibility with very recent Python versions (beyond 3.6/3.7 for which it was likely current) are uncertain. Consider this when integrating into new projects.
Install
-
pip install cyclic
Imports
- Cyclic
from cyclic import Cyclic
Quickstart
from cyclic import Cyclic
# Initialize the Cyclic checker
cy = Cyclic()
# Let's define some objects (can be anything hashable)
A = 'Object A'
B = 'Object B'
C = 'Object C'
D = 'Object D'
# Add relationships: A is parent of B, B is parent of C
cy.add(B, A)
cy.add(C, B)
# Check for cycles (C -> B -> A, no cycle yet)
print(f"Is C in a cyclic relation? {cy.is_cyclic(C)}")
# Introduce a cycle: C is parent of A (now A -> B -> C -> A)
cy.add(A, C)
# Check for cycles again
print(f"Is C now in a cyclic relation? {cy.is_cyclic(C)}")
print(f"Is A now in a cyclic relation? {cy.is_cyclic(A)}")
# Adding a non-cyclic relationship to D
cy.add(D, C)
print(f"Is D in a cyclic relation? {cy.is_cyclic(D)}")