Better error message when accidentally use an array instead of tuple for into_shape?
まだ誰も着手していません。
評価
調査の方向性
Start by reading the into_shape() documentation and the “ndarray for numpy users” section mentioned in the issue. Clarify the array-versus-tuple shape distinction and the different shape representations; done means the relevant documentation explains this footgun clearly, unless maintainers choose an API-level improvement instead.
索引モデルが issue の本文から書いたものです。
説明
I just lost an hour to the following incorrect code:
#[test]
fn test_ndarray_eq()
{
let a = ndarray::arr3(&[[[true]]]);
let a2 = a.into_shape([1,1]).unwrap();
let a3 = a2.into_shape([1,1,1]).unwrap();
assert!(a == a3,"Arrays are not equal!");
}
error[E0308]: mismatched types
--> src/lib.rs:156:20
|
156 | assert!(a==a2, "Arrays do not match!");
| ^^ expected array of 3 elements, found structndarray::IxDynImpl
|
= note: expected typendarray::ArrayBase<_, ndarray::Dim<[usize; 3]>>
found typendarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<ndarray::IxDynImpl>>
The source of the problem has nothing to do with "==", nothing to do with IxDynImpl, and isn't even on the line the error is thrown. If you print all of the arrays using println!, they all look like they have the correct shape, stride, and contained values.
This happened in my original code because I passed the output of .shape() into into_shape. It happened n the above minimized example because I passed the dimensions as arrays "[...]" instead of as tuples "(...)".
If there's anything that can be done to smooth out this sharp edge in the API, consider this a feature request. Failing that, maybe throw big warnings about this footgun in the into_shape() documentation (which says nothing about the type of the input since it's generic over E), and also in the section of the "ndarray for numpy users" section where you at least mention the ugly existence of three different representations of the shape of an array, with different methods to get them.
By the way, thanks for writing ndarray; I would not have even considered Rust without it.
- 主要言語
- Rust
- スター
- 4.3k
- フォーク
- 391
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rust-ndarray/ndarray のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
rust-ndarray/ndarray#1612 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
rust-ndarray/ndarray#1617 · コメント 1 件 ·
-
bug good first issue
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
rust-ndarray/ndarray#1615 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
rust-ndarray/ndarray#1610 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
rust-ndarray/ndarray#1609 ·