python
config_error
ai_generated
true
DeprecationWarning: datetime.datetime.utcnow() 已弃用 FAILED tests/test_time.py::test_now - DeprecationWarning: ...
DeprecationWarning: datetime.datetime.utcnow() is deprecated FAILED tests/test_time.py::test_now - DeprecationWarning: ...
ID: python/pytest-warnings-as-errors-failing-ci
80%修复率
85%置信度
0证据数
2024-06-11首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.12 | active | — | — | — |
| 3.13 | active | — | — | — |
根因分析
CI 使用 `-W error`(或配置中的 `filterwarnings = error`)运行 pytest,因此测试执行期间抛出的任何 DeprecationWarning 都会变成失败。这通常在 Python 小版本升级后出现。
English
CI runs pytest with `-W error` (or `filterwarnings = error` in config), so any DeprecationWarning raised during test execution becomes a failure. This often appears after a Python minor upgrade.
解决方案
-
90% 成功率
Ignore specific known-safe warnings via pytest config: # pyproject.toml [tool.pytest.ini_options] filterwarnings = [ "error", "ignore:datetime.datetime.utcnow\(\) is deprecated:DeprecationWarning", ]
-
98% 成功率
Fix the deprecated call site: from datetime import datetime, timezone datetime.now(timezone.utc) # replaces utcnow()
无效尝试
常见但无效的做法:
-
60% 失败
Hides all warnings, including ones the team intentionally turned into errors to catch regressions.
-
70% 失败
Only suppresses in that scope; other tests using the same deprecated API still fail.