This can come in handy if you're working with large Python objects and you want to be certain that you're either unit testing everything you retrieve or certain that all the data you draw from a database is actually used in a report.
For example, you might have a SELECT fieldX, fieldY, fieldZ FROM ... SQL query, but in the report you only use fieldX, fieldY in your CSV export.
Example use case:
user = { "name": "John Doe", "age": 30, "email": "[email protected]", } user = TrackingDict(user) assert user["name"] == "John Doe" print("Accessed keys:", user.accessed_keys) print("Never accessed keys:", user.never_accessed_keys)This will print
Accessed keys: {'name'} Never accessed keys: {'email', 'age'}This can be useful if you have, for example, a pytest test that checks all the values of a dict object and you want to be sure that you're testing everything. For example:
assert not user.never_accessed_keys, f"You never checked {user.never_accessed_keys}"UPDATE (June 16)
Here's a typed version, by commenter Michael Cook:
from typing import TypeVar, Any K = TypeVar('K') V = TypeVar('V') class TrackingDict(dict[K, V]): """ A `dict` that keeps track of which keys are accessed. """ def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) self._accessed_keys: set[K] = set() def __getitem__(self, key: K) -> V: self._accessed_keys.add(key) return super().__getitem__(key) @property def accessed_keys(self) -> set[K]: return self._accessed_keys @property def never_accessed_keys(self) -> set[K]: return set(self.keys()) - self._accessed_keys