Exceptions¶
The structure modules raise the exceptions of Exceptions.
sds.utils adds only the errors of its own tooling, all deriving from
DataStructureError. Today there is one: the
error raised when a feature needs an extra that is not installed.
Exception Hierarchy¶
graph TD
A[Exception] --> B[DataStructureError]
A --> I[ImportError]
B --> O[OptionalDependencyError]
I --> O
style A fill:#f9f,stroke:#333
style B fill:#bbf,stroke:#333
style I fill:#fdd,stroke:#333
style O fill:#bfb,stroke:#333
OptionalDependencyError has two parents, and each one serves a
different reader:
as a
DataStructureError, it is caught by the sameexcept DataStructureErrorclause as every other error the library reports;as an
ImportError, it is caught by code that already guards optional imports, and itsnameattribute holds the missing package, as for any failed import.
OptionalDependencyError¶
- class sds.utils.OptionalDependencyError(package, extra)[source]¶
Bases:
DataStructureError,ImportErrorException raised when a feature needs an extra that is not installed.
The message names the package that failed to import and the
pysds-toolsextra that provides it. Also anImportError, withnameset to the missing package.- Parameters:
Examples
>>> err = OptionalDependencyError("matplotlib", "image") >>> print(err) matplotlib is required for this feature: pip install "pysds-tools[image]" >>> err.extra 'image'
Attributes¶
Attribute |
Value |
|---|---|
|
Importable name of the missing package, such as |
|
Extra of |
|
Same as |
|
The full message, ending with the |
Usage Example¶
Offer the image only when it can be produced, and fall back on text:
from sds.tree import AVLTree
from sds.utils import OptionalDependencyError
from sds.utils.visualizer import save_image, to_text
tree = AVLTree()
for key in (40, 20, 60, 10, 30):
tree.insert(key)
try:
save_image(tree, "tree.png")
except OptionalDependencyError as err:
print(f"No image ({err.package} missing), text instead:")
print(to_text(tree))
The error is raised when the feature is called: importing
sds.utils.visualizer always succeeds, with or without matplotlib.
Best Practices¶
✅ Catch OptionalDependencyError around the call that needs the extra,
not around the import: the import never fails.
✅ Show str(err) to the user: it names the command that fixes the
problem.
❌ Do not catch a bare ImportError to detect a missing extra: a broken
install of the package itself (one of its dependencies missing) raises a
plain ModuleNotFoundError, which should not be mistaken for an extra
to install.