Description
Over in HypothesisWorks/hypothesis#3065, we're working on test tooling that takes an array-api implementation (e.g. numpy.array_api
, etc) and returns test helpers which use that implementation. For ergonomics we'd also like to automatically find the array-api implementation, if there is one, for a given module - but as I understand it there's no way to discover that mapping. Yet!
I therefore propose that as part of the Array API standard, packages implementing the API should register an entry point declaring which (sub)module contains the implementation. For example, Numpy would register:
entry_points={
'console_scripts': f2py_cmds,
'array_api': ['numpy = numpy.array_api'], # new: maps top-level name to array api implementation
},
Implementations can then be found using importlib.metadata
(see here for < py3.7) as follows:
import numpy.array_api
from importlib.metadata import entry_points
try:
eps = entry_points(group="array_api")
except TypeError:
# Load-time selection requires Python >= 3.10 or importlib_metadata >= 3.6, so fallback:
eps = importlib_metadata.entry_points().get("array_api", [])
implementations = {entry.name: entry for entry in eps}
npa = implementations["numpy"].load()
assert npa is numpy.array_api
This allows us to avoid hard-coded lists of known libraries and locations, which will inevitably be both incomplete and out of date as the ecosystem evolves. It also makes it easy to find all installed array-api implementations, supporting a much wider range of purposes than testing 🎉