Skip to content

test_isdtype #174

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

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion array_api_tests/_array_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __repr__(self):
]
_constants = ["e", "inf", "nan", "pi"]
_funcs = [f.__name__ for funcs in stubs.category_to_funcs.values() for f in funcs]
_funcs += ["take"] # TODO: bump spec and update array-api-tests to new spec layout
_funcs += ["take", "isdtype"] # TODO: bump spec and update array-api-tests to new spec layout
_top_level_attrs = _dtypes + _constants + _funcs + stubs.EXTENSIONS

for attr in _top_level_attrs:
Expand Down
12 changes: 12 additions & 0 deletions array_api_tests/dtype_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"bool_and_all_int_dtypes",
"dtype_to_name",
"dtype_to_scalars",
"kind_to_dtypes",
"is_int_dtype",
"is_float_dtype",
"get_scalar_type",
Expand Down Expand Up @@ -139,6 +140,17 @@ def _filter_stubs(*args):
)


kind_to_dtypes = {
"bool": [xp.bool],
"signed integer": int_dtypes,
"unsigned integer": uint_dtypes,
"integral": all_int_dtypes,
"real floating": float_dtypes,
"complex floating": complex_dtypes,
"numeric": numeric_dtypes,
}


def is_int_dtype(dtype):
return dtype in all_int_dtypes

Expand Down
27 changes: 27 additions & 0 deletions array_api_tests/test_data_type_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ def test_iinfo(dtype):
# TODO: test values


def atomic_kinds() -> st.SearchStrategy[Union[DataType, str]]:
return xps.scalar_dtypes() | st.sampled_from(list(dh.kind_to_dtypes.keys()))


@pytest.mark.min_version("2022.12")
@given(
dtype=xps.scalar_dtypes(),
kind=atomic_kinds() | st.lists(atomic_kinds(), min_size=1).map(tuple),
)
def test_isdtype(dtype, kind):
out = xp.isdtype(dtype, kind)

assert isinstance(out, bool), f"{type(out)=}, but should be bool [isdtype()]"
_kinds = kind if isinstance(kind, tuple) else (kind,)
expected = False
for _kind in _kinds:
if isinstance(_kind, str):
if dtype in dh.kind_to_dtypes[_kind]:
expected = True
break
else:
if dtype == _kind:
expected = True
break
assert out == expected, f"{out=}, but should be {expected} [isdtype()]"


@given(hh.mutually_promotable_dtypes(None))
def test_result_type(dtypes):
out = xp.result_type(*dtypes)
Expand Down