DLPX-79760 SnapshotDefinitionDataPaths object is not hashable since py2-3 transition #430
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.
Problem:
Since python 2-3 transition, in DB2 plugin, generated.definition subclasses are no longer hashable. Since in one of the workflows in DB2, we add a Definition subclass (SnapshotDefinitionDataPaths) to a set to make sure we don’t record duplicates, we started getting this error,
Traceback (most recent call last): File "plugin/platform_server.py", line 66, in _run_helper response = internal_func(request) File "./_linked.py", line 300, in _internal_staged_pre_snapshot File "./plugin_runner.py", line 63, in linked_pre_snapshot File "./plugin_operations/staging.py", line 53, in pre_snapshot File "./plugin_libs/linked_source.py", line 2449, in staging_push_pre_snapshot_validation File "./plugin_libs/db2_operations.py", line 734, in get_data_paths File "./plugin_libs/db2_operations.py", line 865, in get_all_paths_from_db2dart TypeError: unhashable type: 'SnapshotDefinitionDataPaths'
The same codeflow works just fine in older plugin that used python2.
Solution:
We have not implemented hash method in our Model class while we have implemented equals as below
In Python 3
In Python 2
Due to this change in behavior, we will not be able to store the generated class in any hash-supported data structure like dictionary OR set and we will get unhashable type errors.
To solve this we have tried to override the hash() similar to equals() method.
Please note that we have changed the behavior of hash() with this diff where earlier the default hash() method was using id(x) and if two objects are equals as per equal() contract they might not have the same hashcode but with this diff, we are using the properties of the object and if two objects are equals as per equal() contract they will have the same hashcode.
Testing
Manually reproduced this issue and verified that fix is working.
git blackbox -s appdata_python_samples --extra-params="-p virt-sdk-repo=https://github.com/vimleshmishra/virtualization-sdk.git -p virt-sdk-branch=develop"
git blackbox -s appdata_basic -c APPDATA_PYTHON_DIRECT_CENTOS73 --extra-params="-p virt-sdk-repo=https://github.com/vimleshmishra/virtualization-sdk.git -p virt-sdk-branch=develop"
git blackbox -s appdata_basic -c APPDATA_PYTHON_STAGED_CENTOS73 --extra-params="-p virt-sdk-repo=https://github.com/vimleshmishra/virtualization-sdk.git -p virt-sdk-branch=develop"