id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 1936840711,PR_kwDOAMm_X85cdvXs,8294,Use shape and dtype as typevars in NamedArray,14371165,closed,0,,,9,2023-10-11T05:08:32Z,2023-12-12T20:41:29Z,2023-10-18T06:22:52Z,MEMBER,,0,pydata/xarray/pulls/8294,"Using a different TypeVar strategy compared to #8281. The idea here is to typevar shape and dtype instead, just like numpy does. Previously I tried to use the _data array as the TypeVar but that causes all kinds of issues since TypeVar is usually invariant and can't be updated to a new type. Since the dtype changes very frequently when doing array operations it quickly gets difficult to pass along the correct typing. * This PR adds a from_array function. The intention is to use that function to create NamedArrays when you are passing around ArrayLikes. The init for NamedArray will now just assume the input data is correct. At runtime at least, mypy will catch any non-supported array types. There's some precedent to this: * numpy.array_api.Array forces to use xp.asarray. * [Cubed](https://github.com/tomwhite/cubed/blob/main/cubed/array_api/array_object.py) assumes the inputs are correct. Has a xp.asarray and a from_array function. * The ugly `fastpath` argument is therefore not needed. * Adds a bunch of type hint classes, `duckarray[ShapeType, DType]` (corresponding to `np.ndarray`) or `DuckArray[ScalarType]` (corresponding to `np.typing.NDArray`) are the recommended ones. * It's better to use these kinds of classes over creating `is_duck_array` functions with typeguards because `isinstance` also works on the `else` clause. * This PR adds some array_api functions, the idea here is that NamedArray could also be array_api compliant. - [x] Tests added - [x] Closes #8291 References: https://github.com/tomwhite/cubed/blob/ea885193dd37d27917a24878b51bb086aaef5fb1/cubed/core/ops.py#L34 https://stackoverflow.com/questions/74633074/how-to-type-hint-a-generic-numpy-array https://numpy.org/doc/stable/reference/arrays.scalars.html#scalars https://github.com/numpy/numpy/blob/040ed2dc9847265c581a342301dd87d2b518a3c2/numpy/__init__.pyi#L1423 https://github.com/numpy/numpy/blob/040ed2dc9847265c581a342301dd87d2b518a3c2/numpy/_typing/_array_like.py#L32 https://stackoverflow.com/questions/69186176/determine-if-subclass-has-a-base-classs-method-implemented-in-python","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/8294/reactions"", ""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,pull