python runtime_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-07-26First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
6.x active — — —
7.x active — — —
8.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 95% fail

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