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

Add support for numpy struct dtype #211

Merged
merged 1 commit into from
Jun 13, 2024

Conversation

alexfanqi
Copy link
Contributor

Fixes #204

Comment on lines 752 to 789
def make_numpy_struct_dtype(dtype, name):
"""This is a convenience function to create type annotation for numpy structured array
It does exact match on the name, order, and dtype of all its fields

!!! Example:

```python
label_t = np.dtype([('first', np.uint8), ('second', np.int8)])
Label = make_numpy_struct_dtype(label_t, Label)
```
after that, you can use it just like any AbstractDtype
```python
Label[np.array, 'a b c']
```
"""
assert isinstance(dtype, np.dtype)
Copy link
Owner

Choose a reason for hiding this comment

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

Nits:

  • Can you add type annotations?
  • Can you add **Arguments:** and **Returns:** sections? (See jaxtyped for an example.)
  • Can you switch this from an assert to a ValueError? (The former I reserve for internal "developer mistake" errors, not "user passed in the wrong input" errors.)

Other than this, this PR looks good to me! Thank you for contributing :)

@alexfanqi alexfanqi force-pushed the main branch 3 times, most recently from ce67283 to 21609c3 Compare June 13, 2024 11:56
@alexfanqi
Copy link
Contributor Author

sorry, this took so long. How does this look?

@patrick-kidger patrick-kidger merged commit ad95c10 into patrick-kidger:main Jun 13, 2024
1 check passed
@patrick-kidger
Copy link
Owner

LGTM! Thank you for the contribution :)

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.

numpy structured dtype support
2 participants