Skip to content

Commit 1751356

Browse files
committed
Require the namespace argument to asarray() when it's ambiguous
1 parent e9c52c4 commit 1751356

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

numpy_array_api_compat/_aliases.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import NamedTuple
1313
from types import ModuleType
1414

15-
from ._helpers import _is_numpy_array
15+
from ._helpers import _is_numpy_array, get_namespace
1616
from ._internal import get_xp
1717

1818
# Basic renames
@@ -208,10 +208,15 @@ def asarray(
208208
dtype: Optional[Dtype] = None,
209209
device: Optional[Device] = None,
210210
copy: "Optional[Union[bool, np._CopyMode]]" = None,
211-
namespace = 'numpy',
211+
namespace = None,
212212
) -> ndarray:
213-
214-
if isinstance(namespace, ModuleType):
213+
if namespace is None:
214+
try:
215+
xp = get_namespace(obj, _use_compat=False)
216+
except ValueError:
217+
# TODO: What about lists of arrays?
218+
raise ValueError("A namespace must be specified for asarray() with non-array input")
219+
elif isinstance(namespace, ModuleType):
215220
xp = namespace
216221
elif namespace == 'numpy':
217222
import numpy as xp

0 commit comments

Comments
 (0)