python data_error ai_generated true

值错误:具有多个元素的数组的真值不明确。请使用 a.any() 或 a.all()

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

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2025-04-01首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

将 numpy 数组作为参数传递给参数化测试,而未将其转换为列表,导致真值测试不明确。

English

Passing a numpy array as a parameter to a parametrized test without converting it to a list, causing ambiguity in truth testing.

generic

解决方案

  1. 95% 成功率
    @pytest.mark.parametrize('arr', [np.array([1,2]).tolist()])
  2. 90% 成功率
    def id_func(arr):
        return f'arr_{len(arr)}'
    @pytest.mark.parametrize('arr', [np.array([1,2])], ids=id_func)

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 60% 失败

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