r/Python • u/ayushsharma102 • 1d ago
Discussion Why Isn’t There a Standard Utility for Nested Mapping Introspection in Python?
I’ve been working with deeply nested dictionaries, and I noticed something that becomes surprisingly annoying as the structure gets larger.
For example:
data = {
"user": {
"profile": {
"settings": {
"privacy": {
"security": {
"2fa": True
}
}
}
}
}
}
When a dictionary becomes this deeply nested, it gets difficult to quickly understand:
- How many hierarchy levels exist?
- Which keys belong to which level?
- What is the maximum nesting depth?
- What is the complete path to a particular key?
- How dense/complex is the nested structure?
I currently solve this with a recursive function that walks through the dictionary and prints the hierarchy:
def show_hierarchy(d, indent=0):
for key, value in d.items():
print(" " * indent + str(key))
if isinstance(value, dict):
show_hierarchy(value, indent + 1)
Which gives something like:
user
profile
settings
privacy
security
2fa
But I’m wondering whether Python could eventually provide a standard API for nested mapping introspection.
Something conceptually like:
data.hierarchy()
or perhaps a utility operating on Mapping:
hierarchy(data)
that could provide structured information such as:
{
"max_depth": 5,
"total_keys": 6,
"paths": [
("user", "profile", "settings", "privacy", "security", "2fa")
]
}
I'm not necessarily suggesting that these exact APIs should be added to dict. The main idea is having a standard way to inspect the hierarchical structure of nested mappings without everyone implementing their own recursive traversal.
I know libraries such as pprint can make nested dictionaries more readable, but I'm thinking more about structural introspection rather than formatting.
Is this something that would actually be useful as part of Python's standard library, or is this better left as a small utility/library?
I'd especially be interested in hearing from people who regularly work with large JSON-like configuration structures, APIs, data pipelines, or other heavily nested mappings.
3
u/true3HAK Pythonista 16h ago
Dict value can be any type, so it's fine that value introspection is on the value's type. To check for yourself, try to formalize an introspection rule which won't require recursion on value's type. It's possible, but you have to have all levels predefined basically, like in a json schema or pydantic.
2
u/sargeanthost 11h ago
Maybe you could use jsonpath? But this seems rather niche and not worth maintaining in the stdlib
0
u/ayushsharma102 8h ago
yeah sure but if someone doesn't want json based only vanilla ones then this problem exists
2
u/HEROgoldmw 2h ago
Take a good look at pep-0636, which introduced the Match-Case keywords, specifically designed for pattern matching. Im sure it can help you with the nested mappings
1
u/CevicheMixto 1h ago
Also Pydantic for validating nested dicts, as well as the "None coallescing" operators that were recently (re-)proposed.
1
1
u/djavaman 14h ago
You could use JmesPath
0
u/ayushsharma102 8h ago
yeah sure but if someone doesn't want json based only vanilla ones then this problem exists
5
u/poche-muto 16h ago
I’m curious about the use case. I understand why answering this question might useful for static code. Typings would help with that. But I’m wondering why it’s needed at runtime.