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 #13045 - Comparison of Structs and Struct Exprs #13226

Merged
merged 6 commits into from Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion hail/python/hail/utils/struct.py
Expand Up @@ -94,7 +94,9 @@ def __str__(self):
)

def __eq__(self, other):
return isinstance(other, Struct) and self._fields == other._fields
return self._fields == other._fields \
if isinstance(other, Struct) \
else NotImplemented
Comment on lines +97 to +99
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def __hash__(self):
return 37 + hash(tuple(sorted(self._fields.items())))
Expand Down
19 changes: 19 additions & 0 deletions hail/python/test/hail/utils/test_utils.py
Expand Up @@ -421,3 +421,22 @@ def test_hadoop_ls_negated_group(glob_tests_directory):
glob_tests_directory + '/abc/ghi/?23']
actual = [x['path'] for x in hl.hadoop_ls(glob_tests_directory + '/abc/ghi/[!1]23')]
assert set(actual) == set(expected)


def test_struct_rich_comparison():
"""Asserts comparisons between structs and struct expressions are symmetric"""
struct = hl.Struct(
locus=hl.Locus(contig=10, position=60515, reference_genome='GRCh37'),
alleles=['C', 'T']
)

expr = hl.struct(
locus=hl.locus(contig='10', pos=60515, reference_genome='GRCh37'),
alleles=['C', 'T']
)

assert hl.eval(struct == expr) and hl.eval(expr == struct)
assert hl.eval(struct >= expr) and hl.eval(expr >= struct)
assert hl.eval(struct <= expr) and hl.eval(expr <= struct)
assert not (hl.eval(struct < expr) or hl.eval(expr < struct))
assert not (hl.eval(struct > expr) or hl.eval(expr > struct))