Skip to content

Commit 8eb6555

Browse files
authored
Merge pull request numpy#19800 from honno/xp-entry-point
ENH: Add entry point for Array API implementation
2 parents cb34084 + 5702b51 commit 8eb6555

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

numpy/tests/test_public_api.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import sysconfig
23
import subprocess
34
import pkgutil
45
import types
@@ -458,3 +459,40 @@ def check_importable(module_name):
458459
raise AssertionError("Modules that are not really public but looked "
459460
"public and can not be imported: "
460461
"{}".format(module_names))
462+
463+
464+
@pytest.mark.xfail(
465+
sysconfig.get_config_var("Py_DEBUG") is not None,
466+
reason=(
467+
"NumPy possibly built with `USE_DEBUG=True ./tools/travis-test.sh`, "
468+
"which does not expose the `array_api` entry point. "
469+
"See https://github.com/numpy/numpy/pull/19800"
470+
),
471+
)
472+
def test_array_api_entry_point():
473+
"""
474+
Entry point for Array API implementation can be found with importlib and
475+
returns the numpy.array_api namespace.
476+
"""
477+
eps = importlib.metadata.entry_points()
478+
try:
479+
xp_eps = eps.select(group="array_api")
480+
except AttributeError:
481+
# The select interface for entry_points was introduced in py3.10,
482+
# deprecating its dict interface. We fallback to dict keys for finding
483+
# Array API entry points so that running this test in <=3.9 will
484+
# still work - see https://github.com/numpy/numpy/pull/19800.
485+
xp_eps = eps.get("array_api", [])
486+
assert len(xp_eps) > 0, "No entry points for 'array_api' found"
487+
488+
try:
489+
ep = next(ep for ep in xp_eps if ep.name == "numpy")
490+
except StopIteration:
491+
raise AssertionError("'numpy' not in array_api entry points") from None
492+
493+
xp = ep.load()
494+
msg = (
495+
f"numpy entry point value '{ep.value}' "
496+
"does not point to our Array API implementation"
497+
)
498+
assert xp is numpy.array_api, msg

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ def setup_package():
411411
python_requires='>=3.8',
412412
zip_safe=False,
413413
entry_points={
414-
'console_scripts': f2py_cmds
414+
'console_scripts': f2py_cmds,
415+
'array_api': ['numpy = numpy.array_api'],
415416
},
416417
)
417418

0 commit comments

Comments
 (0)