python
data_error
ai_generated
true
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ID: python/pytest-parametrize-list-mutation
80%Fix Rate
82%Confidence
0Evidence
2025-04-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Passing a numpy array as a parameter to a parametrized test without converting it to a list, causing ambiguity in truth testing.
generic中文
将 numpy 数组作为参数传递给参数化测试,而未将其转换为列表,导致真值测试不明确。
Workarounds
-
95% success
@pytest.mark.parametrize('arr', [np.array([1,2]).tolist()]) -
90% success
def id_func(arr): return f'arr_{len(arr)}' @pytest.mark.parametrize('arr', [np.array([1,2])], ids=id_func)
Dead Ends
Common approaches that don't work:
-
80% fail
Pytest may still try to evaluate the array's truth value.
-
60% fail
This changes the data type and may break the test logic.