Add utility functions for working with nested dictionary keys.

Review Request #13993 — Created June 23, 2024 and submitted

Information

Djblets
release-5.x

Reviewers

A pattern that crops up from time to time is the need to work with
nested dictionary keys, setting or retrieving something deep in a
dictionary while handling type conflicts and avoiding unwanted
alteration of data.

This introduces two dict_get_path_value() and dict_set_path_value()
for working with this pattern. These standard methods can be used to
more easily work with path-based key management in dictionaries.

Unit tests pass.

Summary ID
Add utility functions for working with nested dictionary keys.
A pattern that crops up from time to time is the need to work with nested dictionary keys, setting or retrieving something deep in a dictionary while handling type conflicts and avoiding unwanted alteration of data. This introduces two `dict_get_path_value()` and `dict_set_path_value()` for working with this pattern. These standard methods can be used to more easily work with path-based key management in dictionaries.
91496adb36fb1733fb92c8926698fc4f6e0cfa29
Description From Last Updated

ValueError seems more appropriate.

daviddavid
maubin
  1. Ship It!
  2. 
      
david
  1. 
      
  2. djblets/util/datastructures.py (Diff revision 1)
     
     
    Show all issues

    ValueError seems more appropriate.

    1. Yeah I debated this and wasn't sure which way to lean. I'll go over my mental process when writing this.

      It's a problem with a value along that key path, so it could be a ValueError, but then it could also be a KeyError because the key given is invalid for the structure of the object being accessed. So it's a question of, is it an error on the caller's end (I'd then say a KeyError -- they provided a bad key for the data they're operating on) or on the data end (a value that should have been a dictionary but wasn't, therefore a ValueError makes sense there).

      I leaned toward KeyError in this case, simplifying what the caller had to worry about catching and handling. My thought was also that a ValueError might be more appropriate for an error involving a value being set.

      But then, I could be convinced the other way as well.

    2. Actually, how about TypeError. That's what you get if you try to subscript something like an int. That way there's a distinction between "the key didn't exist" and "something wasn't a dict"

  3. 
      
chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-5.x (65da7d8)