[Fix] Fix unsafe dictionary access #3751
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The method
init_model
in mmseg/apis/inference.py has an unsafe dictionary access in line 61. The key'meta'
is accessed directly, even though the following lines always usecheckpoint.get('meta')
to ensure it even exists and have functional handling for the case that it doesn't (in theelse
case). As a result, the program will throw a KeyError before ever reaching the already implemented handling of this very case. This bug is the reason for #3558.Modification
Simply moving the dictionary access AFTER the check that the key exists will suffice. This will allow
init_model
to reach theelse
case whencheckpoint['meta']
does not exist.Checklist