fix: avoid KeyError when getting type converter #132
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.
When I attempted to read the patient metadata using the following code, an exception was raised:
This results in the following traceback:
The root cause of the issue can be found in the following section of the code:
mffpy/mffpy/xml_files.py
Lines 420 to 441 in 2509423
As shown there,
_type_converteronly defines converters for the keys'string'andNone. However, patient metadata fields are not limited to these two data types. As a result, when encountering a field with adataTypesuch as'date', the following line:attempts to access a non-existent key in
_type_converter, which leads to aKeyError.In this PR,
dict.get(key, default)is used to make the conversion more robust. When the specifieddataTypeis not found in_type_converter, the logic now falls back to the default converter associated withNone(i.e.,lambda x: x), thereby avoiding the exception and preserving the original value.