python runtime_error ai_generated true

RecursionError: 调用 Python 对象时超出最大递归深度 ... conftest.py:10: in db request.getfixturevalue('db') conftest.py:10: in db request.getfixturevalue('db') ...

RecursionError: maximum recursion depth exceeded while calling a Python object ... conftest.py:10: in db request.getfixturevalue('db') conftest.py:10: in db request.getfixturevalue('db') ...

ID: python/pytest-recursion-error-fixture-cycle

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

版本兼容性

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

根因分析

某个 fixture 调用 request.getfixturevalue() 时传入了自身的名称,导致无限递归。这通常发生在重构 fixture 时意外引用了错误的名称。

English

A fixture calls request.getfixturevalue() with its own name, creating an infinite recursion. This often happens when refactoring fixtures and accidentally referencing the wrong name.

generic

解决方案

  1. 98% 成功率
    @pytest.fixture
    def db(request):
        engine = request.getfixturevalue('engine')  # not 'db'
        return engine.connect()
  2. 95% 成功率
    @pytest.fixture
    def db(engine):  # pytest injects engine directly
        return engine.connect()

无效尝试

常见但无效的做法:

  1. 90% 失败

    Delays the crash but the infinite recursion still exhausts memory and eventually raises RecursionError or crashes the interpreter.

  2. 95% 失败

    The recursion happens during fixture resolution before the try block can catch it in a useful way.