A Python dict that can report which keys you did not use

peterbe.com

82 points by gilad 4 days ago


boothby - 21 hours ago

Just a heads up, this fails to track usage of get and setdefault. The ability to iterate over dicts makes the whole question rather murky.

IshKebab - 21 hours ago

I think if you feel like you need this then it's a bit of a red flag and you should be using Pydantic or `dataclass` instead, then your IDE can statically tell you which fields you don't access (among many other benefits). Dicts are mainly for when you don't know the keys up front.

golly_ned - 17 hours ago

I have a similar use case and this idea also occurred to me.

However: the dict in this case would also include dataclasses, and I’d be interested in finding what exact attributes within those dataclasses were accessed, and also be able to mark all attributes in those dataclasses as accessed if the parent dataclasses is accessed, and with those dataclasses, being config objects, being able to do the same to its own children, so that the topmost dictionary has a tree of all accessed keys.

I couldn’t figure out how to do that, but welcome to ideas.

ok123456 - 21 hours ago

If you're inheriting from dict to extend its behavior, there are a lot of side effects with that, and it's recommended to use https://docs.python.org/3/library/collections.html#collectio... instead.

codethief - 16 hours ago

Only tangentially related but I am really excited about PEP 764¹ (inline typed dictionaries). If it gets accepted, we can finally replace entire hierarchies of dataclasses with simple nested dictionary types and call it a day.

I am currently teaching (typed) Python to a team of Windows sysadmins and it's been incredibly difficult to explain when to use a dataclass, a NamedTuple, a Pydantic model, or a dictionary.

¹) https://peps.python.org/pep-0764/

jraph - 4 days ago

I did exactly the same thing in our Confluence to XWiki migrator to easily and automatically report which macro parameters we don't handle when converting Confluence macros to equivalent macros in XWiki.

This can be used to evaluate the migration quality and spot what can be improved.

https://github.com/xwiki-contrib/confluence/blob/7a95bf96787...

simon04 - 18 hours ago

Very useful. For configparser.ConfigParser I've found https://stackoverflow.com/a/57307141

larrik - 19 hours ago

I actually wrote something similar in nodejs for a data import system. Was very handy.

mrits - 14 hours ago

For giant dicts a bloomfilter would work great here

jgalt212 - 19 hours ago

why not inside of __init__

  self.accessed_keys = set()
instead of

    @property
    def accessed_keys(self):
        return self._accessed_keys
nurettin - 16 hours ago

AI front: We have models to generate pictures, videos and code. We have the best devs and are so fskin rich!

Rust front: Here's a faster ls called ls-rs with different defaults, you should use this!

Go front: Here's reverse proxy #145728283 it is an open source project that has slightly different parameters than all the others.

Python hobo front: Uhh guys here's a dict that kinda might remember what you've accessed if you used it in a particular way.