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

Revamped signature tests #110

Merged
merged 16 commits into from
Apr 28, 2022
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
101 changes: 39 additions & 62 deletions array_api_tests/dtype_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import re
from collections.abc import Mapping
from functools import lru_cache
from typing import Any, NamedTuple, Sequence, Tuple, Union
from inspect import signature
from typing import Any, Dict, NamedTuple, Sequence, Tuple, Union
from warnings import warn

from . import _array_module as xp
from ._array_module import _UndefinedStub
from .stubs import name_to_func
from .typing import DataType, ScalarType

__all__ = [
Expand Down Expand Up @@ -242,67 +245,31 @@ def result_type(*dtypes: DataType):
return result


func_in_dtypes = {
# elementwise
"abs": numeric_dtypes,
"acos": float_dtypes,
"acosh": float_dtypes,
"add": numeric_dtypes,
"asin": float_dtypes,
"asinh": float_dtypes,
"atan": float_dtypes,
"atan2": float_dtypes,
"atanh": float_dtypes,
"bitwise_and": bool_and_all_int_dtypes,
"bitwise_invert": bool_and_all_int_dtypes,
"bitwise_left_shift": all_int_dtypes,
"bitwise_or": bool_and_all_int_dtypes,
"bitwise_right_shift": all_int_dtypes,
"bitwise_xor": bool_and_all_int_dtypes,
"ceil": numeric_dtypes,
"cos": float_dtypes,
"cosh": float_dtypes,
"divide": float_dtypes,
"equal": all_dtypes,
"exp": float_dtypes,
"expm1": float_dtypes,
"floor": numeric_dtypes,
"floor_divide": numeric_dtypes,
"greater": numeric_dtypes,
"greater_equal": numeric_dtypes,
"isfinite": numeric_dtypes,
"isinf": numeric_dtypes,
"isnan": numeric_dtypes,
"less": numeric_dtypes,
"less_equal": numeric_dtypes,
"log": float_dtypes,
"logaddexp": float_dtypes,
"log10": float_dtypes,
"log1p": float_dtypes,
"log2": float_dtypes,
"logical_and": (xp.bool,),
"logical_not": (xp.bool,),
"logical_or": (xp.bool,),
"logical_xor": (xp.bool,),
"multiply": numeric_dtypes,
"negative": numeric_dtypes,
"not_equal": all_dtypes,
"positive": numeric_dtypes,
"pow": numeric_dtypes,
"remainder": numeric_dtypes,
"round": numeric_dtypes,
"sign": numeric_dtypes,
"sin": float_dtypes,
"sinh": float_dtypes,
"sqrt": float_dtypes,
"square": numeric_dtypes,
"subtract": numeric_dtypes,
"tan": float_dtypes,
"tanh": float_dtypes,
"trunc": numeric_dtypes,
# searching
"where": all_dtypes,
r_alias = re.compile("[aA]lias")
r_in_dtypes = re.compile("x1?: array\n.+have an? (.+) data type.")
r_int_note = re.compile(
"If one or both of the input arrays have integer data types, "
"the result is implementation-dependent"
)
category_to_dtypes = {
"boolean": (xp.bool,),
"integer": all_int_dtypes,
"floating-point": float_dtypes,
"numeric": numeric_dtypes,
"integer or boolean": bool_and_all_int_dtypes,
}
func_in_dtypes: Dict[str, Tuple[DataType, ...]] = {}
for name, func in name_to_func.items():
if m := r_in_dtypes.search(func.__doc__):
dtype_category = m.group(1)
if dtype_category == "numeric" and r_int_note.search(func.__doc__):
dtype_category = "floating-point"
dtypes = category_to_dtypes[dtype_category]
func_in_dtypes[name] = dtypes
elif any("x" in name for name in signature(func).parameters.keys()):
func_in_dtypes[name] = all_dtypes
# See https://github.com/data-apis/array-api/pull/413
func_in_dtypes["expm1"] = float_dtypes


func_returns_bool = {
Expand Down Expand Up @@ -365,6 +332,8 @@ def result_type(*dtypes: DataType):
"trunc": False,
# searching
"where": False,
# linalg
"matmul": False,
}


Expand Down Expand Up @@ -408,7 +377,7 @@ def result_type(*dtypes: DataType):
"__gt__": "greater",
"__le__": "less_equal",
"__lt__": "less",
# '__matmul__': 'matmul', # TODO: support matmul
"__matmul__": "matmul",
"__mod__": "remainder",
"__mul__": "multiply",
"__ne__": "not_equal",
Expand Down Expand Up @@ -440,6 +409,14 @@ def result_type(*dtypes: DataType):
func_returns_bool[iop] = func_returns_bool[op]


func_in_dtypes["__bool__"] = (xp.bool,)
func_in_dtypes["__int__"] = all_int_dtypes
func_in_dtypes["__index__"] = all_int_dtypes
func_in_dtypes["__float__"] = float_dtypes
func_in_dtypes["from_dlpack"] = numeric_dtypes
func_in_dtypes["__dlpack__"] = numeric_dtypes


@lru_cache
def fmt_types(types: Tuple[Union[DataType, ScalarType], ...]) -> str:
f_types = []
Expand Down
67 changes: 67 additions & 0 deletions array_api_tests/meta/test_signatures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from inspect import Parameter, Signature, signature

import pytest

from ..test_signatures import _test_inspectable_func


def stub(foo, /, bar=None, *, baz=None):
pass


stub_sig = signature(stub)


@pytest.mark.parametrize(
"sig",
[
Signature(
[
Parameter("foo", Parameter.POSITIONAL_ONLY),
Parameter("bar", Parameter.POSITIONAL_OR_KEYWORD),
Parameter("baz", Parameter.KEYWORD_ONLY),
]
),
Signature(
[
Parameter("foo", Parameter.POSITIONAL_ONLY),
Parameter("bar", Parameter.POSITIONAL_OR_KEYWORD),
Parameter("baz", Parameter.POSITIONAL_OR_KEYWORD),
]
),
Signature(
[
Parameter("foo", Parameter.POSITIONAL_ONLY),
Parameter("bar", Parameter.POSITIONAL_OR_KEYWORD),
Parameter("qux", Parameter.KEYWORD_ONLY),
Parameter("baz", Parameter.KEYWORD_ONLY),
]
),
],
)
def test_good_sig_passes(sig):
_test_inspectable_func(sig, stub_sig)


@pytest.mark.parametrize(
"sig",
[
Signature(
[
Parameter("foo", Parameter.POSITIONAL_ONLY),
Parameter("bar", Parameter.POSITIONAL_ONLY),
Parameter("baz", Parameter.KEYWORD_ONLY),
]
),
Signature(
[
Parameter("foo", Parameter.POSITIONAL_ONLY),
Parameter("bar", Parameter.KEYWORD_ONLY),
Parameter("baz", Parameter.KEYWORD_ONLY),
]
),
],
)
def test_raises_on_bad_sig(sig):
with pytest.raises(AssertionError):
_test_inspectable_func(sig, stub_sig)
25 changes: 18 additions & 7 deletions array_api_tests/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,29 @@
if name.endswith("_functions"):
category = name.replace("_functions", "")
objects = [getattr(mod, name) for name in mod.__all__]
assert all(isinstance(o, FunctionType) for o in objects)
assert all(isinstance(o, FunctionType) for o in objects) # sanity check
category_to_funcs[category] = objects

all_funcs = []
for funcs in [array_methods, *category_to_funcs.values()]:
all_funcs.extend(funcs)
name_to_func: Dict[str, FunctionType] = {f.__name__: f for f in all_funcs}

EXTENSIONS: str = ["linalg"]
extension_to_funcs: Dict[str, List[FunctionType]] = {}
for ext in EXTENSIONS:
mod = name_to_mod[ext]
objects = [getattr(mod, name) for name in mod.__all__]
assert all(isinstance(o, FunctionType) for o in objects)
extension_to_funcs[ext] = objects
assert all(isinstance(o, FunctionType) for o in objects) # sanity check
funcs = []
for func in objects:
if "Alias" in func.__doc__:
funcs.append(name_to_func[func.__name__])
else:
funcs.append(func)
extension_to_funcs[ext] = funcs

all_funcs = []
for funcs in [array_methods, *category_to_funcs.values(), *extension_to_funcs.values()]:
all_funcs.extend(funcs)
name_to_func: Dict[str, FunctionType] = {f.__name__: f for f in all_funcs}
for funcs in extension_to_funcs.values():
for func in funcs:
if func.__name__ not in name_to_func.keys():
name_to_func[func.__name__] = func
Loading