Skip to content

Commit e216d89

Browse files
committed
Instance check constants with SupportsFloat
1 parent 5daa5d0 commit e216d89

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

array_api_tests/test_constants.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from typing import Any, SupportsFloat
23

34
import pytest
45

@@ -7,6 +8,10 @@
78
from .typing import Array
89

910

11+
def assert_scalar_float(name: str, c: Any):
12+
assert isinstance(c, SupportsFloat), f"{name}={c!r} does not look like a float"
13+
14+
1015
def assert_0d_float(name: str, x: Array):
1116
assert dh.is_float_dtype(
1217
x.dtype
@@ -17,16 +22,18 @@ def assert_0d_float(name: str, x: Array):
1722
def test_irrational(name, n):
1823
assert hasattr(xp, name)
1924
c = getattr(xp, name)
25+
assert_scalar_float(name, c)
2026
floor = math.floor(n)
21-
assert c > floor, f"xp.{name}={c} <= {floor}"
27+
assert c > floor, f"xp.{name}={c!r} <= {floor}"
2228
ceil = math.ceil(n)
23-
assert c < ceil, f"xp.{name}={c} >= {ceil}"
29+
assert c < ceil, f"xp.{name}={c!r} >= {ceil}"
2430
x = xp.asarray(c)
2531
assert_0d_float("name", x)
2632

2733

2834
def test_inf():
2935
assert hasattr(xp, "inf")
36+
assert_scalar_float("inf", xp.inf)
3037
assert math.isinf(xp.inf)
3138
assert xp.inf > 0, "xp.inf not greater than 0"
3239
x = xp.asarray(xp.inf)
@@ -36,6 +43,7 @@ def test_inf():
3643

3744
def test_nan():
3845
assert hasattr(xp, "nan")
46+
assert_scalar_float("nan", xp.nan)
3947
assert math.isnan(xp.nan)
4048
assert xp.nan != xp.nan, "xp.nan should not have equality with itself"
4149
x = xp.asarray(xp.nan)

0 commit comments

Comments
 (0)