python runtime_error ai_generated true

RecursionError: 超出最大递归深度

RecursionError: maximum recursion depth exceeded

ID: python/pytest-recursion-error-fixture

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

版本兼容性

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

根因分析

fixture 直接或间接依赖自身(例如 `def a(b): ...` 和 `def b(a): ...`),导致 fixture 解析期间无限递归。

English

A fixture depends on itself directly or indirectly (e.g., `def a(b): ...` and `def b(a): ...`), causing infinite recursion during fixture resolution.

generic

解决方案

  1. 85% 成功率
    @pytest.fixture
    def a(request):
        b = request.getfixturevalue('b')
        ...
  2. 95% 成功率
    def _common_setup(): ...
    
    @pytest.fixture
    def a():
        return _common_setup()
    
    @pytest.fixture
    def b():
        return _common_setup()

无效尝试

常见但无效的做法:

  1. 90% 失败

    Delays the crash and may cause a segfault; does not fix the circular dependency.

  2. 60% 失败

    If the dependency is intentional, renaming breaks the chain; if not, it may still be circular.