python runtime_error ai_generated true

RecursionError: maximum recursion depth exceeded while calling a Python object

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

A fixture directly or indirectly depends on itself, causing infinite recursion during fixture resolution. This often happens when a fixture has the same name as a parameter it requests.

generic

中文

夹具直接或间接依赖自身,导致夹具解析期间无限递归。这通常发生在夹具名称与其请求的参数名称相同时。

Workarounds

  1. 95% success
    @pytest.fixture
    def db_connection():
        return create_connection()
    
    @pytest.fixture
    def db_session(db_connection):  # Different name
        return Session(db_connection)
  2. 85% success
    @pytest.fixture
    def my_fixture(request):
        other = request.getfixturevalue('other_fixture')
        return process(other)

Dead Ends

Common approaches that don't work:

  1. 90% fail

    This delays the inevitable crash and may cause a stack overflow, potentially crashing the Python interpreter.

  2. 95% fail

    RecursionError occurs during fixture resolution, before the fixture body executes. The try/except inside the fixture never catches it.