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

DLPX-79760 SnapshotDefinitionDataPaths object is not hashable since py2-3 transition #430

Merged
merged 11 commits into from
Apr 6, 2022

Conversation

vimleshmishra
Copy link
Contributor

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

def __eq__(self, other):
        """Returns true if both objects are equal"""
        return self.to_dict() == other.to_dict()

In Python 3

A class that overrides eq() and does not define hash() will have its hash() implicitly set to None. When the hash() method of a class is None, instances of the class will raise an appropriate TypeError

In Python 2

User-defined classes have cmp() and hash() methods by default; with them, all objects compare unequal (except with themselves) and x.hash() returns a result derived from id(x).

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"

@SumoSourabh
Copy link
Contributor

  • For test cases to pass, you will have to update test_get_version () test case of test_package_util.py file.
  • Also, you will have to update the PR with JIRA ID and Description for General GitHub PrePush Checks to pass.

@vimleshmishra vimleshmishra changed the title __hash__ is needed in python 3 DLPX-79760 SnapshotDefinitionDataPaths object is not hashable since py2-3 transition Apr 1, 2022
@vimleshmishra
Copy link
Contributor Author

  • For test cases to pass, you will have to update test_get_version () test case of test_package_util.py file.
  • Also, you will have to update the PR with JIRA ID and Description for General GitHub PrePush Checks to pass.

Thanks for highlighting.

@vimleshmishra vimleshmishra merged commit ebace08 into delphix:develop Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants