From 50ed0e9711a087849769586c1e043d108bc0a7cb Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Mon, 30 Dec 2024 17:35:32 +0000 Subject: [PATCH 1/6] feat: `ARRAY_API_TESTS_MODULE` for runtime-defined xp --- array_api_tests/__init__.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/array_api_tests/__init__.py b/array_api_tests/__init__.py index 4e0c340f..89b81872 100644 --- a/array_api_tests/__init__.py +++ b/array_api_tests/__init__.py @@ -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[5:][:-1] + namespace = {} + exec(script, namespace) + xp = namespace["xp"] + xp_name = xp.__name__ + else: + 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) else: raise RuntimeError( "No array module specified - either edit __init__.py or set the " From b80a558e97995ae8c8b21feb80e78b335b312314 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Mon, 30 Dec 2024 17:36:20 +0000 Subject: [PATCH 2/6] nit --- array_api_tests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_tests/__init__.py b/array_api_tests/__init__.py index 89b81872..d492da3f 100644 --- a/array_api_tests/__init__.py +++ b/array_api_tests/__init__.py @@ -21,7 +21,7 @@ xp = namespace["xp"] xp_name = xp.__name__ else: - xp_name = os.environ["ARRAY_API_TESTS_MODULE"] + xp_name = env_var _module, _sub = xp_name, None if "." in xp_name: _module, _sub = xp_name.split(".", 1) From 4244965fe1cdf1394143f089beac587307f375fa Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Mon, 30 Dec 2024 17:58:56 +0000 Subject: [PATCH 3/6] exclude quote marks --- array_api_tests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_tests/__init__.py b/array_api_tests/__init__.py index d492da3f..e70d2b8b 100644 --- a/array_api_tests/__init__.py +++ b/array_api_tests/__init__.py @@ -15,7 +15,7 @@ if "ARRAY_API_TESTS_MODULE" in os.environ: env_var = os.environ["ARRAY_API_TESTS_MODULE"] if env_var.startswith("exec(") and env_var.endswith(")"): - script = env_var[5:][:-1] + script = env_var[6:][:-2] namespace = {} exec(script, namespace) xp = namespace["xp"] From 85f929f76547fcad2d7659cce8a3b4e1c4bfddf8 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Mon, 30 Dec 2024 18:39:44 +0000 Subject: [PATCH 4/6] docs --- README.md | 6 ++++++ array_api_tests/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 928a6771..72e1d1ea 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/array_api_tests/__init__.py b/array_api_tests/__init__.py index e70d2b8b..b58faf66 100644 --- a/array_api_tests/__init__.py +++ b/array_api_tests/__init__.py @@ -14,7 +14,7 @@ # you want to test, e.g. `import array_api_strict as xp`. if "ARRAY_API_TESTS_MODULE" in os.environ: env_var = os.environ["ARRAY_API_TESTS_MODULE"] - if env_var.startswith("exec(") and env_var.endswith(")"): + if env_var.startswith("exec('") and env_var.endswith("')"): script = env_var[6:][:-2] namespace = {} exec(script, namespace) From c71d5db5eba6f4fa57f86537f102d23c4e6a1f8f Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Fri, 24 Jan 2025 01:17:34 +0000 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 72e1d1ea..17131560 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ $ 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)') +$ export ARRAY_API_TESTS_MODULE=exec('import quantity_array, numpy; xp = quantity_array.quantity_namespace(numpy)') ``` Alternately, import/define the `xp` variable in `array_api_tests/__init__.py`. From 845479b7168220c80e716d1ccd8e34926dd3d8b8 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Fri, 24 Jan 2025 11:45:07 +0000 Subject: [PATCH 6/6] typo --- array_api_tests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_tests/__init__.py b/array_api_tests/__init__.py index b58faf66..f3805b56 100644 --- a/array_api_tests/__init__.py +++ b/array_api_tests/__init__.py @@ -30,7 +30,7 @@ try: xp = getattr(xp, _sub) except AttributeError: - # _sub may be a submodule that needs to be imported. WE can't + # _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)