python runtime_error ai_generated true

RecursionError: maximum recursion depth exceeded

ID: python/pytest-recursion-error-fixture

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-11-12First Seen

Version Compatibility

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

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 60% fail

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