Skip to content

feat: ARRAY_API_TESTS_MODULE for runtime-defined xp #334

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ You need to specify the array library to test. It can be specified via the
$ export ARRAY_API_TESTS_MODULE=array_api_strict
```

To specify a runtime-defined module, define `xp` using the `exec('...')` syntax:

```bash
$ export ARRAY_API_TESTS_MODUle=exec('import pint_array, numpy; xp = pint_array.pint_namespace(numpy)')
```

Alternately, import/define the `xp` variable in `array_api_tests/__init__.py`.

### Specifying the API version
Expand Down
34 changes: 21 additions & 13 deletions array_api_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@
# You can comment the following out and instead import the specific array module
# you want to test, e.g. `import array_api_strict as xp`.
if "ARRAY_API_TESTS_MODULE" in os.environ:
xp_name = os.environ["ARRAY_API_TESTS_MODULE"]
_module, _sub = xp_name, None
if "." in xp_name:
_module, _sub = xp_name.split(".", 1)
xp = import_module(_module)
if _sub:
try:
xp = getattr(xp, _sub)
except AttributeError:
# _sub may be a submodule that needs to be imported. WE can't
# do this in every case because some array modules are not
# submodules that can be imported (like mxnet.nd).
xp = import_module(xp_name)
env_var = os.environ["ARRAY_API_TESTS_MODULE"]
if env_var.startswith("exec('") and env_var.endswith("')"):
script = env_var[6:][:-2]
namespace = {}
exec(script, namespace)
xp = namespace["xp"]
xp_name = xp.__name__
else:
xp_name = env_var
_module, _sub = xp_name, None
if "." in xp_name:
_module, _sub = xp_name.split(".", 1)
xp = import_module(_module)
if _sub:
try:
xp = getattr(xp, _sub)
except AttributeError:
# _sub may be a submodule that needs to be imported. WE can't
# do this in every case because some array modules are not
# submodules that can be imported (like mxnet.nd).
xp = import_module(xp_name)
else:
raise RuntimeError(
"No array module specified - either edit __init__.py or set the "
Expand Down
Loading