Skip to content

Commit d7df6ae

Browse files
authored
Add guidance concerning how to check for an array API compliant object (#309)
1 parent 2e17ca1 commit d7df6ae

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

spec/purpose_and_scope.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,20 @@ The `xp` namespace must contain all functionality specified in
354354
including additional functionality is not recommended as doing so may hinder
355355
portability and inter-operation of array libraries within user code.
356356

357+
### Checking for Compliance
358+
359+
Array-consuming libraries are likely to want a mechanism for determining whether a provided array is specification compliant. The recommended approach to check for compliance is by checking whether an array object has an `__array_namespace__` attribute, as this is the one distinguishing feature of an array-compliant object.
360+
361+
Checking for an `__array_namespace__` attribute can be implemented as a small utility function similar to the following.
362+
363+
```python
364+
def is_array_api_obj(x):
365+
return hasattr(x, '__array_namespace__')
366+
```
367+
368+
```{note}
369+
Providing a "reference library" on which people depend is out-of-scope; hence, the standard cannot, e.g., provide an array ABC from which libraries can inherit to enable an `isinstance` check.
370+
```
357371

358372
### Discoverability
359373

0 commit comments

Comments
 (0)