python
config_error
ai_generated
true
PytestReturnNotNoneWarning: 期望返回 None,但 tests/test_x.py::test_foo 返回了 1,这将在未来版本的 pytest 中成为错误
PytestReturnNotNoneWarning: Expected None, but tests/test_x.py::test_foo returned 1, which will be an error in a future version of pytest
ID: python/pytest-return-not-none
80%修复率
88%置信度
0证据数
2024-09-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
测试函数返回值而非使用 assert。常见于移植 unittest 风格测试,或辅助函数返回了本意作为断言的真值。
English
A test function returns a value instead of using assert. Common when porting unittest-style tests or when a helper returns a truthy value that was intended as an assertion.
解决方案
-
98% 成功率
# before def test_foo(): return compute() == 1 # after def test_foo(): assert compute() == 1 -
90% 成功率
[tool.pytest.ini_options] filterwarnings = ["error::pytest.PytestReturnNotNoneWarning"]
无效尝试
常见但无效的做法:
-
70% 失败
Silences the warning but the test may pass even when the intended condition is false, because no assertion occurs.
-
85% 失败
Hides a real bug: the test is not actually asserting anything.