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

[query] Add AlleleType Enum #14360

Merged
merged 2 commits into from
Mar 5, 2024
Merged

Conversation

chrisvittal
Copy link
Collaborator

@chrisvittal chrisvittal commented Feb 26, 2024

This exposes functionality previously present in the private hail.expr.functions._allele_ints and hail.expr.functions._num_allele_type, used to avoid strings in the query when determining allele type.

Implement AlleleType as a python IntEnum.

Replace _num_allele_type with the now public numeric_allele_type.

As a note to developers of hail, it is unlikely that the enum values of AlleleType will change, but they are documented as though they could.

CHANGELOG: Exposed previously internal _num_allele_type as numeric_allele_type and deprecated it. Add new AlleleType enumeration for users to be able to easily use the values returned by numeric_allele_type.

@chrisvittal
Copy link
Collaborator Author

Stacked on #14297

@chrisvittal chrisvittal force-pushed the query/AlleleType branch 3 times, most recently from c2f4bea to b6abd2b Compare February 27, 2024 17:50
@chrisvittal chrisvittal marked this pull request as ready for review February 27, 2024 17:51
Copy link
Collaborator Author

@chrisvittal chrisvittal left a comment

Choose a reason for hiding this comment

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

I have one unknown about the design right now. See comment for what to do about generating IR with IntEnum values.

@@ -56,7 +56,7 @@ class I32(IR):
@typecheck_method(x=int)
def __init__(self, x):
super().__init__()
self.x = x
self.x = int(x)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This conversion is because if we pass an IntEnum to the IR constructors, it will generally be assigned as is. When rendered, we (generally) use format strings (__format__ method, which generally delegates to the __str__ method) to render these items. I would like the __str__ method of AlleleType to be the string that allele_type would return, but then when used as a number, we need to make sure to actually convert it to a number.

I'm not sure what to do here. Let's discuss.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems to me the root of the problem is you've made AlleleType a subtype of int, but its str doesn't act like an int. I'd say it should either fully quack like an int, or you should be forced to call x.value to get the int representation.

If we instead forced calling x.name to get the string name, where would that actually impact users?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(Note that the default str of IntEnum doesn't return the int representation either, but they fixed that in 3.11, so we can just override it to match the future behavior)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the commentary. I'm going add a pretty_name that matches with the result of allele_type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And now we're back to no changes to ir.py

This exposes functionality previously present in the private
`hail.expr.functions._allele_ints` and
`hail.expr.functions._num_allele_type`, used to avoid strings in the
query when determining allele type.

Implement AlleleType as a python [IntEnum].

Replace `_num_allele_type` with the now public `numeric_allele_type`.

As a note to developers of hail, it is _unlikely_ that the enum values
of AlleleType will change, but they are documented as though they could.

CHANGELOG: Exposed previously internal _num_allele_type as
           numeric_allele_type and deprecated it. Add new AlleleType
           enumeration for users to be able to easily use the values
           returned by numeric_allele_type.

[IntEnum]: https://docs.python.org/3/library/enum.html#enum.IntEnum
@chrisvittal
Copy link
Collaborator Author

@patrick-schultz bump

Copy link
Collaborator

@patrick-schultz patrick-schultz left a comment

Choose a reason for hiding this comment

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

Thanks Chris!

@hail-ci-robot hail-ci-robot merged commit 89ca474 into hail-is:main Mar 5, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants