python config_error ai_generated true

DeprecationWarning: @pytest.yield_fixture 已弃用。请改用 @pytest.fixture;两者相同。

DeprecationWarning: @pytest.yield_fixture is deprecated. Use @pytest.fixture instead; they are the same.

ID: python/pytest-deprecation-yield-fixture

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

版本兼容性

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

根因分析

自 pytest 3.0 起,yield_fixture 是 fixture 的别名。在 pytest 7+ 中该别名会发出 DeprecationWarning,并将在未来移除。

English

Since pytest 3.0, yield_fixture is an alias of fixture. In pytest 7+ the alias emits a DeprecationWarning and will be removed.

generic

解决方案

  1. 98% 成功率
    # before
    @pytest.yield_fixture
    def client(): ...
    
    # after
    @pytest.fixture
    def client(): ...
  2. 90% 成功率
    pyupgrade --py38-plus tests/**/*.py

无效尝试

常见但无效的做法:

  1. 70% 失败

    Hides the warning but leaves the deprecated API in place; the fixture will break on the next major release.

  2. 65% 失败

    Blocks upgrades and security patches across the whole test toolchain.