Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #940 - KeyError in get_dict_from_list #947

Merged
merged 2 commits into from
Apr 12, 2021

Conversation

rblenis
Copy link
Contributor

@rblenis rblenis commented Apr 12, 2021

  • cause of error : defaultdict added more defaultdict while attempting to traverse the tree, but once a 'potential leaf' node was added, it was added as a dict, not a defaultdict.

example: (annotated output from 'borg diff --info --log-json repo::archive1 archive2')
added directory home/rblenis/tmp/borgtest <-- 'home','rblenis','tmp' added as defaultdict, 'borgtest' added as dict
added 6.93 kB home/rblenis/tmp/borgtest/CircleFit.cpp <-- 'CircleFit.cpp' added as dict
added 1.61 kB home/rblenis/tmp/borgtest/CircleFit.h <-- 'CircleFit.h' added as dict
added directory home/rblenis/tmp/borgtest/folder2 <-- 'folder2' added as dict
added 6.93 kB home/rblenis/tmp/borgtest/folder2/CircleFit.cpp <-- 'CircleFit.cpp' added as dict
added 1.61 kB home/rblenis/tmp/borgtest/folder2/CircleFit.h <-- 'CirclFit.h' added as dict
removed directory home/rblenis/tmp/borgtest/foo/bar1 <-- 'borgtest' is dict, so trying to traverse to non existent foo/bar1 throws KeyError.

  • two possible solutions:
    • 1 - change everywhere that adds a 'potential' leaf node to add a defaultdict (ie nested_dict()) - this occurs in several places, but not many.
    • 2 - change get_dict_from_list to add a default dict (not defaultdict) when traversing the tree, for the case where a multi-level node is added on top of an existing node. This requires only changing a single location, and means that the dictionaries returned by accessing the tree will behave like normal dict (ie, won't by default add missing keys).

- cause of error : defaultdict added more defaultdict while attempting to traverse the tree, but once a 'potential leaf' node was added, it was added as a dict, not a defaultdict.
- two possible solutions:
    - 1 - change everywhere that adds a 'potential' leaf node to add a defaultdict (ie nested_dict()) - this occurs in several places, but not many.
    - 2 - change get_dict_from_list to add a default dict (not defaultdict) when traversing the tree, for the case where a multi-level node is added on top of an existing node. This requires only changing a single location, and means that the dictionaries returned by accessing the tree will behave like normal dict (ie, won't by default add missing keys).
@rblenis
Copy link
Contributor Author

rblenis commented Apr 12, 2021

Also, probably addresses #925

@m3nu
Copy link
Contributor

m3nu commented Apr 12, 2021

Good stuff. I see you went with option 2.

I'll try to add a test for the failed case tomorrow and then merge it.

Thanks for handling it! 👍

@m3nu
Copy link
Contributor

m3nu commented Apr 12, 2021

I added a test case following the steps here and can confirm that this PR fixes the exception. 👍

@rblenis
Copy link
Contributor Author

rblenis commented Apr 12, 2021

Yes, I went with option 2 so that all nodes in the nested dictionary were the same type (dict vs defaultdict), and didn't run into potential future confusion over the dictionaries auto-adding keys when not expected. This option only auto-adds keys while building the nested dictionaries. Thanks for looking into the test case. I ran out of time this weekend, but wanted to at least present the PR so people knew it was being worked on.

@m3nu
Copy link
Contributor

m3nu commented Apr 12, 2021

It's perfect. 2 issues closed with 2 lines changed 😄

@rblenis rblenis deleted the issue_940 branch April 13, 2021 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants