Skip to content

Commit

Permalink
Prevent instantiation of jaxtyping annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-kidger committed Feb 15, 2025
1 parent d3c6b0f commit 759b50d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jaxtyping/_array_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ class for `Float32[Array, "foo"]`.
dims: tuple[_AbstractDimOrVariadicDim, ...]
index_variadic: Optional[int]

def __new__(cls, *args, **kwargs):
raise RuntimeError(
"jaxtyping annotations cannot be instantiated -- they should be used for "
"type hints only."
)


_not_made = object()

Expand Down
5 changes: 5 additions & 0 deletions test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,8 @@ def f(x: Float[array_type, "foo bar"]):
# Not an array
with pytest.raises(ParamError):
f(1)


def test_non_instantiation():
with pytest.raises(RuntimeError, match="cannot be instantiated"):
Float[Array, ""]() # pyright: ignore[reportCallIssue]

0 comments on commit 759b50d

Please sign in to comment.