Improved typing for __array__#138
Conversation
|
numpy is not a project dependency. It's currently an optional dependency for "test". Is there a way to make numpy types optional? |
|
@kylebarron thank you, but this kind of complicated typing makes me depressed. Instead, I'm inclined to deprecate >>> import array
>>> class Foo:
... __array_interface__ = {"shape": (2, 2), "typestr": "d", "data": array.array("d", [1,2,3,4]), "version": 3}
...
>>> import numpy
>>> numpy.array(Foo())
array([[1., 2.],
[3., 4.]])The |
|
I pushed another option, which hard-codes
So it's a viable option if you don't expect users to call
To be clear, this is pretty low value for me, so it's totally fine if you want to reject it for complexity. (I think #137 is much higher value, since matrix multiplication is probably much more common, and the typing pain of the currently-inferred union is much worse)
I don't know enough about pros and cons of I don't think it's possible to type that array interface because the type checker isn't able to statically match the |
|
For reference, #108 added I added this method to help cross-compare matrix algebra results from this package with numpy, e.g. several examples in test_numpy.py. This includes matrix-matrix and matrix-vector combinations. |
|
Regardless of how you feel about this type hinting, adding IIRC, I believe that |
|
@mwtoews I'm going to trade |
|
Just my two cents: I'd only expect |
|
@kylebarron |
|
superseded by #141. |


Similar in spirit to #137 for for the
__array__conversion to numpy.This one is less important than #137 so it's ok if you reject it for complexity:
Before:
After:
np.asarraywhen no explicitdtypeis provided (assuming users callnp.asarrayand notAffine.__array__)float64array specifically.