boltons iterutils remap and get_path for nested dict transforms: working versions (tested 2026-10-16)
Recursively redact, drop and rename keys in nested dicts with boltons remap, then read values back safely with get_path.
passed6 passed · 0 failed2026-10-16 09:57 UTC- Status
- passed
- Last passed
- 2026-10-16 09:57 UTC
- Tests
- 6 / 6
- Language
- Python
- Runtime
- Python 3.13.16
pinned versions2
boltons@26.2.0pytest@9.1.1
run it
uv venv -q .venv && uv pip install -q --python .venv/bin/python -r requirements.txt .venv/bin/python -m pytest -q
test environment
Python 3.13.16 linux 6.17.0-1022-azure x64 runner: github-actions 38883899161
The code
Exactly the files that passed. Your agent can pull them with get_recipe("boltons-iterutils-remap") (install), or see them on GitHub.
requirements.txt
boltons==26.2.0 pytest==9.1.1
transform.py
from boltons.iterutils import get_path, remap
SECRET_KEYS = {"password", "token", "secret", "api_key"}
_UNSET = object()
def redact_secrets(obj, keys=SECRET_KEYS, mask="***"):
def visit(path, key, value):
if isinstance(key, str) and key.lower() in keys:
return key, mask
return True
return remap(obj, visit)
def drop_nones(obj):
def visit(path, key, value):
if value is None:
return False
return True
return remap(obj, visit)
def rename_keys(obj, mapping):
def visit(path, key, value):
if key in mapping:
return mapping[key], value
return True
return remap(obj, visit)
def nested_get(obj, path, default=_UNSET):
if default is _UNSET:
return get_path(obj, path)
return get_path(obj, path, default=default)
tests/test_transform.py
import pytest
from boltons.iterutils import PathAccessError, remap
from transform import drop_nones, nested_get, redact_secrets, rename_keys
CONFIG = {
"service": {"name": "api", "password": "p", "replicas": 3},
"token": "top-secret",
"debug": None,
}
def test_redacts_secrets_at_every_depth():
clean = redact_secrets(CONFIG)
assert clean["token"] == "***"
assert clean["service"]["password"] == "***"
assert clean["service"]["replicas"] == 3
def test_only_literal_false_drops_an_item():
assert "debug" not in drop_nones(CONFIG)
# Returning None (or any falsy non-False value) is treated as a (key, value)
# pair, so default_exit chokes instead of dropping the item.
def visit(path, key, value):
return None if key == "debug" else True
with pytest.raises(TypeError):
remap(CONFIG, visit)
def test_rename_visits_every_level_and_collisions_lose_data():
assert rename_keys({"old": 1, "deep": {"old": 2}}, {"old": "new"}) == {
"new": 1,
"deep": {"new": 2},
}
# remap is post-order; a pre-existing target key is silently overwritten.
assert rename_keys({"old": 1, "new": 2}, {"old": "new"}) == {"new": 2}
def test_get_path_default_only_covers_missing_paths():
assert nested_get(CONFIG, ("service", "name")) == "api"
assert nested_get(CONFIG, ("service", "nope"), default="fallback") == "fallback"
# A present None is returned as-is; the default does not kick in.
assert nested_get(CONFIG, ("debug",), default="fallback") is None
def test_dotted_string_path_cannot_address_dotted_keys():
payload = {"a.b": 1}
assert nested_get(payload, ("a.b",)) == 1
with pytest.raises(PathAccessError):
nested_get(payload, "a.b")
def test_pathaccesserror_is_a_keyerror_and_indexerror():
with pytest.raises(PathAccessError) as exc:
nested_get({"a": 5}, "a.b")
assert isinstance(exc.value, KeyError)
assert isinstance(exc.value, IndexError)
pytest.ini
[pytest] pythonpath = .
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.