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

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2025-04-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success
    @pytest.mark.parametrize('arr', [np.array([1,2]).tolist()])
  2. 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:

  1. 80% fail

    Pytest may still try to evaluate the array's truth value.

  2. 60% fail

    This changes the data type and may break the test logic.