Live data from Hacker News

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

peterbe.com

1–10 of 45 posts

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

#2
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...

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

#3
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.

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

#6
post #3

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.

Static analysis could only tell you which fields are never used, across all usage of the class. Not on a given instance.

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

#7
post #4

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.

From right above where you linked to:

> The need for this class has been partially supplanted by the ability to subclass directly from dict; however, this class can be easier to work with because the underlying dictionary is accessible as an attribute.

Sounds like (unless you need the dict as a separate data member) this class is a historical artefact. Unless there's some other issue you know of not mentioned in the documentation?

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

#8
post #5

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.

I didn't know about the setdefault method, and wouldn't have guessed it lets you read a value. Interesting, thanks.

Another way to get data out would be to use the new | operator (i.e. x = {} | y essentially copies dictionary x to y) or the update method or ** unpacking operator (e.g. x = {**y}). But maybe those come under the umbrella of iterating as you mentioned.

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

#9
post #3

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.

Counterpoint, something like this for dataclasses would also be very useful.

That is, it isn't just knowing whether or not the data is ever used. It is useful to know if it was used in this specific run. And often times, seeing what parts of the data was not used is a good clue as to what went wrong. At the least, you can use it to rule out what code was not hit.

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

#10
post #5

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.

I didn't know about the setdefault method, and wouldn't have guessed it lets you read a value. Interesting, thanks. Another way to get data out would be to use the new | operator (i.e. x = {} | y essentially copies dictionary x to y) or the update method or ** unpacking operator (e.g. x = {**y}). But maybe those come under the umbrella of iterating as you mentioned.

setdefault was a go to method before defaultdict was added to the collections module in Python 2.5, which replaced the biggest use case.
Post reply on HN