You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don’t think this can be fixed other than actually special-casing numpy in the type stubs, unless numpy starts importing your ArrayNamespace and using it as return annotation.
You should run a type checker in CI to see if array_namespace actually typechecks with all the things it supports.
Maybe -compat should type it something like Enum | int?
In [1]: import numpy as np
In [3]: np.ones(3).__dlpack_device__()
Out[3]: (1, 0)
In [4]: import torch
In [5]: torch.ones(3).__dlpack_device__()
Out[5]: (<DLDeviceType.kDLCPU: 1>, 0)
In [6]: import jax.numpy as jnp
In [7]: jnp.ones(3).__dlpack_device__()
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
Out[7]: (<DLDeviceType.kDLCPU: 1>, 0)
In [8]: type(jnp.ones(3).__dlpack_device__()[0])
Out[8]: <enum 'DLDeviceType'>
In [9]: type(torch.ones(3).__dlpack_device__()[0])
Out[9]: <enum 'DLDeviceType'>
In [11]: type(cupy.ones(3).__dlpack_device__()[0])
Out[11]: int
Activity
lucascolley commentedon Oct 26, 2026
cc @jorenham
jorenham commentedon Oct 26, 2026
Hmm, the
Enumdoesn't look right, because that's incompatible withint.flying-sheep commentedon Oct 26, 2026
Yes, but that matters only in general, not for numpy.
Since numpy will probably never accurately type the return type of
__array_namespace__, it needs special handling I think./edit: oops clicked wrong.
jorenham commentedon Oct 26, 2026
Is making the
__array_namespace__return type generic an option?ev-br commentedon Oct 26, 2026
FWIW (and I officially know nothing about type annotations),
Enumcomes straight from the Array API spec:https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack_device__.html
and if anything,
-compatshould follow the spec. What should be updated, the spec, numpy or -compat layer, I've no idea per the first line above :-).jorenham commentedon Oct 26, 2026
For numpy, at least, it's a bare int. So is NumPy wrong, or should that "enum" in the spec be interpreted as a
Literal[1,2,...]?ev-br commentedon Oct 26, 2026
Maybe
-compatshould type it something likeEnum | int?jorenham commentedon Oct 26, 2026
-> intwould also acceptIntEnum(IntEnumsubclasses bothEnumandint), which is what torch uses: https://github.com/pytorch/pytorch/blob/a97e076079700e9e7e595d59a3e911c37613d8c5/torch/_tensor.py#L1706ev-br commentedon Oct 26, 2026
PR welcome :-).
Boundary conditions:
-compatneeds to support numpy 1.x;jorenham commentedon Oct 26, 2026
Don't you follow SPEC 0 here?
ev-br commentedon Oct 26, 2026
-compatlayer follows whatever active users follow, and some of them are more conservative than SPEC 0 (looking at you scikit-learn).jorenham commentedon Oct 26, 2026
Ah, luckily the
ndarray.__dlpack_device__return type has been correct since at least 1.22 (e.g.), so-> tuple[int, int]should be fine.