Skip to content

[query] Add AlleleType Enum - #14360

Merged
hail-ci-robot merged 2 commits into
hail-is:mainfrom
chrisvittal:query/AlleleType
Mar 5, 2024
Merged

hail-ci-robot merged 2 commits into
hail-is:mainfrom
chrisvittal:query/AlleleType

Conversation

@chrisvittal

@chrisvittal chrisvittal commented Feb 26, 2024 •

Copy link
Copy Markdown
Collaborator

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 chrisvittal added the stacked PR Part of a stack. Blocks CI from testing or merging this PR. label Feb 26, 2024
@chrisvittal

Copy link
Copy Markdown
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 removed the stacked PR Part of a stack. Blocks CI from testing or merging this PR. label Feb 27, 2024
@chrisvittal
chrisvittal marked this pull request as ready for review February 27, 2024 17:51

@chrisvittal chrisvittal left a comment

Copy link
Copy Markdown
Collaborator Author

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.

Comment thread hail/python/hail/ir/ir.py Outdated
def __init__(self, x):
super().__init__()
self.x = x
self.x = int(x)

Copy link
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator Author

@patrick-schultz bump

@patrick-schultz patrick-schultz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks Chris!

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