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

ENH: Better type-annotations for scalars & Jax arrays #107

Open
2 tasks
timmens opened this issue Feb 19, 2025 · 0 comments
Open
2 tasks

ENH: Better type-annotations for scalars & Jax arrays #107

timmens opened this issue Feb 19, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@timmens
Copy link
Member

timmens commented Feb 19, 2025

Problem

We use the type-annotation Scalar = int | float | jax.Array frequently in the code-base. The jax.Array is included because certain functions receive floats and return zero-dimensional jax.Array's.

This is not ideal, since

  1. We would like the type-checker to be able to tell the difference between scalars and arrays with at least one dimension, and
  2. It can be confusing for people reading the code.

Second issue is that Array types do not include dtype information, this would be helpful in many cases (e.g., for grids, distinguishing between indices and values). See here.

ToDo's:

  • Create list of code-blocks that actually require Scalar to contain jax.Array
  • Add dtype info to jax.Array

Solution ideas

jaxtyping

Could then do:

from jaxtyping import Array, Float, Int

Scalar = int | float | Float[Array, ""] | Float[Int, ""]

preventing use of Scalar when we really need a 1+ dimensional array?

jaxtyping would also allow annotating Arrays with dtype info.

@timmens timmens added the enhancement New feature or request label Feb 19, 2025
@hmgaudecker hmgaudecker changed the title ENH: Better type-annotations for scalars ENH: Better type-annotations for scalars & Jax arrays Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant