Skip to content

Commit 1fad293

Browse files
committed
v5
1 parent 59e03ee commit 1fad293

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

array_api_compat/_internal.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ def clone_module(mod_name: str, globals_: dict[str, object]) -> list[str]:
5858
Returns __all__.
5959
"""
6060
mod = importlib.import_module(mod_name)
61-
all_ = []
61+
# Neither of these two methods is sufficient by itself,
62+
# depending on various idiosyncrasies of the libraries we're wrapping.
63+
objs = {}
64+
exec(f"from {mod.__name__} import *", objs)
65+
6266
for n in dir(mod):
6367
if not n.startswith("_") and hasattr(mod, n):
64-
all_.append(n)
65-
globals_[n] = getattr(mod, n)
66-
return all_
68+
objs[n] = getattr(mod, n)
69+
70+
globals_.update(objs)
71+
return list(objs)
6772

6873

6974
__all__ = ["get_xp", "clone_module"]

array_api_compat/numpy/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
from .._internal import clone_module
55

6+
# This needs to be loaded explicitly before cloning
7+
import numpy.typing # noqa: F401
8+
69
__all__ = clone_module("numpy", globals())
710

811
# These imports may overwrite names from the import * above.

tests/test_all.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
from array_api_compat._internal import clone_module
9+
810
from ._helpers import wrapped_libraries
911

1012
NAMES = {
@@ -241,10 +243,7 @@
241243
def all_names(mod):
242244
"""Return all names available in a module."""
243245
objs = {}
244-
exec(f"from {mod.__name__} import *", objs)
245-
for n in dir(mod):
246-
if not n.startswith("_") and hasattr(mod, n):
247-
objs[n] = getattr(mod, n)
246+
clone_module(mod.__name__, objs)
248247
return set(objs)
249248

250249

0 commit comments

Comments
 (0)