context
Context display utilities for error messages - available keys, resolution chains, etc.
_format_value_repr(value, max_length=50)
Format a value for compact display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to format |
required |
max_length
|
int
|
Maximum length for the representation |
50
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string representation |
Examples:
>>> _format_value_repr("hello")
'"hello"'
>>> _format_value_repr(42)
'42'
>>> _format_value_repr({"a": 1, "b": 2})
'{a: 1, b: 2}'
Source code in src/sparkwheel/errors/context.py
format_available_keys(config, max_keys=10)
Format available keys for display in error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary to extract keys from |
required |
max_keys
|
int
|
Maximum number of keys to display (default: 10) |
10
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with available keys and their values (truncated if needed) |
Examples:
>>> config = {"_target_": "torch.nn.Linear", "in_features": 784, "out_features": 10}
>>> print(format_available_keys(config))
Available keys:
- _target_: "torch.nn.Linear"
- in_features: 784
- out_features: 10
Source code in src/sparkwheel/errors/context.py
format_resolution_chain(chain, title='Resolution chain:')
Format a resolution chain for display in error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chain
|
list[tuple[str, str, bool]]
|
List of (id, reference, success) tuples representing the resolution chain |
required |
title
|
str
|
Title for the chain display |
'Resolution chain:'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with the resolution chain visualization |
Examples:
>>> chain = [
... ("training::optimizer", "@optimizer", True),
... ("optimizer::lr", "@base::learning_rate", True),
... ("base::learning_rate", "", False),
... ]
>>> print(format_resolution_chain(chain))
Resolution chain:
1. training::optimizer = "@optimizer" ✓
2. optimizer::lr = "@base::learning_rate" ✓
3. base::learning_rate = ❌ NOT FOUND